Class: Legion::Cli::Lex::Runner

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/legion/cli/lex/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



7
8
9
# File 'lib/legion/cli/lex/runner.rb', line 7

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#add_function(name, function, args = nil) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/lex/runner.rb', line 30

def add_function(name, function, args = nil)
  @arg_keys = []

  if args.nil?
    args = '**'
  else
    option_args = ''
    required_args = ''
    args.split(',').each do |arg|
      key, value = arg.split('=')
      @arg_keys.push key.to_s
      if value.nil? || value.empty?
        required_args.concat("#{key}:, ")
      else
        option_args.concat("#{key}: '#{value}', ")
      end
    end
    args = required_args.concat(option_args, '**')
  end
  insert_into_file "lib/legion/extensions/#{lex}/runners/#{name}.rb",
                   after: "extend Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined? 'Helpers::Lex'\n" do
    "
  def #{function}(#{args})
    { success: true }
  end\n"
  end

  insert_into_file("spec/runners/#{name}_spec.rb", after: "it { should be_a Module }\n") do
    result = "  it { is_expected.to respond_to(:#{function}).with_any_keywords }\n"
    result.concat "  it { is_expected.to respond_to(:#{function}).with_keywords(:#{@arg_keys.join(', :')}) }\n" if @arg_keys.count.positive?
    result
  end

  insert_into_file("spec/runners/#{name}_spec.rb", before: "  end\n") do
    "    it('#{function} returns a success') { expect(test_class.#{function}[:success]).to eq true }\n"
  end
end

#create(name) ⇒ Object



24
25
26
27
# File 'lib/legion/cli/lex/runner.rb', line 24

def create(name)
  template('templates/runner.erb', "lib/legion/extensions/#{lex}/runners/#{name}.rb", { name: name, lex: lex })
  template('templates/runner_spec.erb', "spec/runners/#{name}_spec.rb", { name: name, lex: lex })
end

#delete(name) ⇒ Object



18
19
20
21
# File 'lib/legion/cli/lex/runner.rb', line 18

def delete(name)
  remove_file("lib/legion/extensions/#{lex}/runners/#{name}.rb")
  remove_file("spec/runners/#{name}_spec.rb")
end