Module: Capy

Defined in:
lib/capy.rb,
lib/capy/version.rb,
lib/capy/evaluator.rb

Defined Under Namespace

Classes: Evaluator

Constant Summary collapse

EXIT_COMMANDS =
%w(exit quit)
VERSION =
"1.6.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/capy.rb', line 10

def mode
  @mode
end

.optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/capy.rb', line 10

def opts
  @opts
end

Class Method Details

.run(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capy.rb', line 12

def run(args)
  @opts = Slop.parse!(args, :help => true, :strict => true) do
    banner "capy [script.capy]\n"
    on :b, :browser=, 'chrome, firefox', :default => 'chrome'
    on :j, :js, 'eval script as javascript with -a option'
    on :a, :'app-host=', 'app host'
    on :s, :'stop', 'stop after eval script'
    on :w, :webkit, 'use capybara webkit'
  end
  return 1 if opts.help?

  trap('INT') { exit }

  setup_capybara

  @mode = opts.js? ? :javascript : :capybara

  evaluator = Evaluator.new

  evaluator.visit Capybara.app_host if Capybara.app_host

  if args.empty?
    start_shell evaluator
  else
    script_file = args.shift
    unless File.exists?(script_file)
      puts "No such file: #{script_file}".red
      return 1
    end
    result = evaluator.eval_script File.read(script_file), mode, script_file
    puts "=> #{result.inspect}".cyan
    start_shell evaluator if opts.stop?
  end

  0
rescue Slop::InvalidOptionError => e
  puts e.message.red
  1
rescue => e
  error e
  1
end

.setup_capybaraObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capy.rb', line 55

def setup_capybara
  if opts.webkit?
    require 'capybara-webkit'
    Capybara.register_driver :webkit do |app|
      Capybara::Driver::Webkit.new(app, :browser => Capybara::Driver::Webkit::Browser.new)
    end
    Capybara.current_driver = :webkit
  else
    Capybara.register_driver :selenium do |app|
      Capybara::Selenium::Driver.new(app, :browser => opts[:browser].to_sym)
    end
    Capybara.current_driver = :selenium
  end
  Capybara.app_host = opts[:'app-host']
end

.start_shell(evaluator) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/capy.rb', line 73

def start_shell(evaluator)
  return if @_start_shell
  @_start_shell = true

  Readline.completion_proc = lambda do |text|
    (Evaluator.instance_methods - Object.methods + EXIT_COMMANDS).grep(/^#{Regexp.quote(text.strip)}/)
  end

  history_file = File.expand_path('~/.capy_history')
  if File.exists?(history_file)
    File.read(history_file, :encoding => "BINARY").
      encode!(:invalid => :replace, :undef => :replace).
      split(/\n/).
      each { |line| Readline::HISTORY << line }
  end

  while buf = Readline.readline('> ', true)
    unless Readline::HISTORY.count == 1
      Readline::HISTORY.pop if buf.empty? || Readline::HISTORY[-1] == Readline::HISTORY[-2]
    end

    case buf.strip
    when *EXIT_COMMANDS
      File.open(history_file, 'w') do |file|
        lines = Readline::HISTORY.to_a[([Readline::HISTORY.size - 1000, 0].max)..-1]
        file.print(lines.join("\n"))
      end
      return
    else
      begin
        result = evaluator.eval_script(buf, mode)
        puts "=> #{result.inspect}".cyan
      rescue Exception => e
        error e
      end
    end
  end

  @_start_shell = false
end