class Cucumber::ThinkingSphinx::InternalWorld

Attributes

adapter[RW]
database[RW]
database_file[RW]
fixtures_directory[RW]
host[RW]
migrations_directory[RW]
models_directory[RW]
password[RW]
temporary_directory[RW]
username[RW]

Public Class Methods

new() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 11
def initialize
  pwd = Dir.pwd
  @temporary_directory  = "#{pwd}/tmp"
  @migrations_directory = "#{pwd}/features/thinking_sphinx/db/migrations"
  @models_directory     = "#{pwd}/features/thinking_sphinx/models"
  @fixtures_directory   = "#{pwd}/features/thinking_sphinx/db/fixtures"
  @database_file        = "#{pwd}/features/thinking_sphinx/database.yml"

  @adapter  = (ENV['DATABASE'] || 'mysql').gsub /^mysql$/, 'mysql2'
  @database = 'thinking_sphinx'
  @username = ENV['USER']
  @host     = 'localhost'

  if @adapter[/mysql/]
    @username = 'root'
  elsif ENV['TRAVIS']
    @username = 'postgres'
  end
end

Public Instance Methods

configure_database() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 44
def configure_database
  ActiveRecord::Base.establish_connection database_settings
  self
end
setup() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 31
def setup
  make_temporary_directory

  configure_cleanup
  configure_thinking_sphinx
  configure_active_record

  prepare_data
  setup_sphinx

  self
end

Private Instance Methods

config() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 51
def config
  @config ||= ::ThinkingSphinx::Configuration.instance
end
configure_active_record() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 97
def configure_active_record
  ::ActiveRecord::Base.logger = Logger.new(
    open("#{temporary_directory}/active_record.log", "a")
  )

  ::ThinkingSphinx::ActiveRecord::LogSubscriber.logger = Logger.new(
    open("#{temporary_directory}/active_record.log", "a")
  )

  ActiveRecord::Base.connection.class.send(
    :include, Cucumber::ThinkingSphinx::SqlLogger
  )
end
configure_cleanup() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 72
def configure_cleanup
  Kernel.at_exit do
    ::ThinkingSphinx::Configuration.instance.controller.stop
    sleep(0.5) # Ensure Sphinx has shut down completely
    ::ThinkingSphinx::ActiveRecord::LogSubscriber.logger.close
    ::ActiveRecord::Base.logger.close
  end
end
configure_thinking_sphinx() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 62
def configure_thinking_sphinx
  config.config_file        = "#{temporary_directory}/sphinx.conf"
  config.searchd_log_file   = "#{temporary_directory}/searchd.log"
  config.query_log_file     = "#{temporary_directory}/searchd.query.log"
  config.pid_file           = "#{temporary_directory}/searchd.pid"
  config.searchd_file_path  = "#{temporary_directory}/indexes/"

  ::ThinkingSphinx.suppress_delta_output = true
end
database_settings() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 87
def database_settings
  {
    'adapter'  => @adapter,
    'database' => @database,
    'username' => @username,
    'password' => @password,
    'host'     => @host
  }.merge yaml_database_settings
end
load_files(path) click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 121
def load_files(path)
  files = Dir["#{path}/*.rb"].sort!
  files.each do |file|
    require file.gsub(/\.rb$/, '')
  end
end
make_temporary_directory() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 55
def make_temporary_directory
  FileUtils.mkdir_p temporary_directory
  Dir["#{temporary_directory}/*"].each do |file|
    FileUtils.rm_rf file
  end
end
prepare_data() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 111
def prepare_data
  ::ThinkingSphinx.deltas_enabled = false

  load_files migrations_directory
  load_files models_directory
  load_files fixtures_directory

  ::ThinkingSphinx.deltas_enabled = true
end
setup_sphinx() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 128
def setup_sphinx
  FileUtils.mkdir_p config.searchd_file_path

  config.build
  config.controller.index
  config.controller.start
end
yaml_database_settings() click to toggle source
# File lib/cucumber/thinking_sphinx/internal_world.rb, line 81
def yaml_database_settings
  return {} unless File.exist?(@database_file)

  YAML.load open(@database_file)
end