Top Level Namespace

Defined Under Namespace

Modules: Vop Classes: Backend, BaseShell, Loader, VopShellBackend

Instance Method Summary collapse

Instance Method Details

#accept_contributionsObject

TODO remove?



2
3
4
5
6
# File 'lib/vop/plugins/core/helpers/command_loader/contributions.rb', line 2

def accept_contributions
  #puts "#{@command.name} accepts contributions"
  # TODO this init should happen earlier
  #@op.plugins['core'].state[:contributions] ||= Hash.new { |h,k| h[k] = [] }
end

#config(sym) ⇒ Object



1
2
3
# File 'lib/vop/plugins/core/helpers/command_loader/command_syntax.rb', line 1

def config(sym)
  @plugin.config[sym]
end

#contribute(options = {}, &block) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/vop/plugins/core/helpers/command_loader/contributions.rb', line 8

def contribute(options = {}, &block)
  target = options[:to] || @command.short_name
  #puts "#{@command.name} contributes to #{target}"
  # TODO check that the command exists
  @op.plugins['core'].state[:contributions][target] << @command.name
  @command.block = block
end

#default_entity_block(params) ⇒ Object

if an entity doesn’t define a block, by default it just passes through all contributions



6
7
8
# File 'lib/vop/plugins/core/helpers/command_loader/entities.rb', line 6

def default_entity_block(params)
  params['contributions']
end

#define_entity(name, key, options) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/vop/plugins/core/helpers/command_loader/entities.rb', line 10

def define_entity(name, key, options)
  @op.plugins['core'].state[:entities] << {
    name: name,
    key: key,
    options: options
  }
end

#description(s) ⇒ Object



5
6
7
# File 'lib/vop/plugins/core/helpers/command_loader/command_syntax.rb', line 5

def description(s)
  @command.description = s
end

#entity(key, options = {}, &block) ⇒ Object



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
55
56
57
# File 'lib/vop/plugins/core/helpers/command_loader/entities.rb', line 18

def entity(key, options = {}, &block)
  command_name = @command.short_name

  # the entity command gets a mandatory param automatically, using the
  # block defined by the entity as lookup
  # TODO make the default block work
  #list_block = block || default_entity_block
  list_block = block

  if options[:on]
    param! options[:on]
  end

  param! key, :lookup => lambda { |params|
    collected = @op.collect_contributions('name' => command_name, 'raw_params' => params)
    params_with_contributions = params.merge(:contributions => collected)
    the_list = list_block.call(params_with_contributions)
    the_list
  }

  define_entity(command_name, key, options)

  # entities generally accept contributions...
  accept_contributions

  # ...and they have a special run block that filters the correct row from
  # the lookup list and returns an entity populated from that row
  @command.block = lambda do |params|
    collected = @op.collect_contributions('name' => command_name, 'raw_params' => params)
    params_with_contributions = params.merge(:contributions => collected)
    list = list_block.call(params_with_contributions)

    found = list.select { |x| x[key.to_sym] == params[key] }
    if found && found.size > 0
      Entity.new(@op, command_name, key, found.first)
    else
      raise "no such entity : #{params[key]} [#{command_name}]"
    end
  end
end

#fooObject



1
2
3
# File 'lib/vop/plugins/core/helpers/helper.rb', line 1

def foo
  puts "call for mr. beeblebrox!"
end

#format_output(command, data) ⇒ Object



4
5
6
7
8
9
10
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
# File 'lib/vop/shell/formatter.rb', line 4

def format_output(command, data)
  if data.is_a? Array
    first_row = data.first
    if first_row.is_a? Hash
      # show all columns unless defined otherwise in the command
      columns_to_display = first_row.keys
      if command.show_options.include? :columns
        columns_to_display = command.show_options[:columns]
      end
      column_headers = columns_to_display

      rearranged = [] # array of hashes -> array of arrays
      data.each do |row|
        values = []
        columns_to_display.each do |key|
          values << row[key]
        end
        rearranged << values
      end

      begin
        rearranged.sort_by! { |row| row.first }
      rescue
        puts "[WARN] ran into trouble sorting the result (by the first column); results may be not quite sorted."
        begin
          rearranged.sort_by! { |row| row.first || "zaphod" }
        rescue
          puts "[SHRUG] could not sort even when accounting for potential nil values, giving up."
        end
      end

      puts Terminal::Table.new rows: rearranged, headings: column_headers
    else
      puts data.join("\n")
    end
  elsif data.is_a? Hash
    data.each do |k,v|
      puts "#{k} : #{v}"
    end
  else
    puts data
  end
end

#param(name, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vop/plugins/core/helpers/command_loader/command_syntax.rb', line 9

def param(name, options = {})
  p = {
    :name => name.to_s,
    :multi => false,
    :mandatory => false,
    :default_param => false
  }

  if name.is_a? Symbol
    # parameters whose names are symbols are resolved into entities
    op = @plugin.op

    entity_names = op.core.state[:entities].map { |entity| entity[:name] }
    if entity_names.include? name.to_s
      list_command_name = "list_#{name.to_s.pluralize(42)}"
      p[:lookup] = lambda do |params|
        # TODO :name is probably specific to the entity (key?)
        op.send(list_command_name.to_sym).map { |x| x[:name] }
      end
    end
  end

  @command.params << p.merge(options)
end

#param!(name, options = {}) ⇒ Object



34
35
36
37
# File 'lib/vop/plugins/core/helpers/command_loader/command_syntax.rb', line 34

def param!(name, options = {})
  options.merge! :mandatory => true
  param(name, options)
end

#show(options = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/vop/plugins/core/helpers/command_loader/command_syntax.rb', line 39

def show(options = {})
  column_options = options.delete(:columns)

  raise "unknown keyword #{options.keys.first}" if options.keys.length > 0

  @command.show_options[:columns] = column_options
end

#with_contributions(options = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vop/plugins/core/helpers/command_loader/contributions.rb', line 16

def with_contributions(options = {}, &block)
  raise "untested"
  collected = @op.collect_contributions('name' => command_name, 'raw_params' => params)
  params_with_contributions = params.merge(:contributions => collected)
  list = block.call(params_with_contributions)

  found = list.select { |x| x[key.to_sym] == params[key] }
  if found && found.size > 0
    Entity.new(@op, command_name, key, found.first)
  else
    raise "no such entity : #{params[key]} [#{command_name}]"
  end
end