class Guard::KonachaRails::Runner

Constants

DEFAULT_OPTIONS

Attributes

formatter[R]
options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 12
def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)

  # Require project's rails environment file to load Konacha configuration.
  require_rails_environment
  raise 'Konacha not loaded' unless defined? ::Konacha

  # Custom formatter to handle multiple runs.
  @formatter = Formatter.new
  ::Konacha.config.formatters = [@formatter]

  # Reusable session to increase performance.
  @session = Capybara::Session.new(::Konacha.driver, Server.new)

  ::Konacha.mode = :runner

  UI.info 'Guard::KonachaRails Initialized'
end

Public Instance Methods

run(paths = ['']) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 35
def run(paths = [''])
  formatter.reset

  paths.each do |path|
    if path.empty? or File.exist? real_path path
      UI.info "Guard::KonachaRails running #{specs_description(path)}"
      runner.run konacha_path(path)
    end
  end

  formatter.write_summary
  notify
rescue => e
  UI.error(e)
  UI.info(e.message)
end
start() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 31
def start
  run if options[:run_all_on_start]
end

Private Instance Methods

all_specs_path() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 87
def all_specs_path
  "#{::Rails.root.join(::Konacha.config[:spec_dir])}#{konacha_path('')}"
end
konacha_path(path) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 79
def konacha_path(path)
  '/' + path.gsub(/^#{::Konacha.config[:spec_dir]}\/?/, '').gsub(/\.coffee$/, '').gsub(/\.js$/, '')
end
notify() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 99
def notify
  if options[:notification]
    image = @formatter.success? ? :success : :failed
    ::Guard::Notifier.notify(@formatter.summary_line, title: 'Konacha Specs', image: image)
  end
end
real_path(path) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 83
def real_path(path)
  path.empty? ? all_specs_path : specific_path(path)
end
require_rails_environment() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 54
def require_rails_environment
  if @options[:rails_environment_file]
    require @options[:rails_environment_file]
  else
    dir = '.'
    while File.expand_path(dir) != '/' do
      env_file = File.join(dir, 'config/environment.rb')
      if File.exist?(env_file)
        require File.expand_path(env_file)

        break
      end
      dir = File.join(dir, '..')
    end
  end
end
runner() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 75
def runner
  ::Konacha::Runner.new(@session)
end
specific_path(path) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 91
def specific_path(path)
  ::Rails.root.join(path).to_s
end
specs_description(path) click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 71
def specs_description(path)
  path.empty? ? "all specs" : path
end
unique_id() click to toggle source
# File lib/guard/konacha-rails/runner.rb, line 95
def unique_id
  "#{Time.now.to_i}#{rand(100)}"
end