Module: Capy

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

Defined Under Namespace

Classes: Evaluater

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

.optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Class Method Details

.run(args) ⇒ Object



11
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
54
# File 'lib/capy.rb', line 11

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

  evaluater = Evaluater.new

  evaluater.visit Capybara.app_host if Capybara.app_host

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

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

.setup_capybaraObject



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

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(evaluater) ⇒ Object



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
113
# File 'lib/capy.rb', line 74

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

  Readline.completion_proc = lambda do |text|
    (Evaluater.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 = evaluater.eval_script(buf, mode)
        puts "=> #{result.inspect}".cyan
      rescue Exception => e
        error e
      end
    end
  end

  @_start_shell = false
end