Class: Vop::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/entity.rb

Overview

An entity is something that is identifiable through a key, e.g. a name. Also, it groups commands that accept the same parameter: When a command is called on an entity, the parameter with the same name is filled, so

@op.machine("localhost").processes()

is equivalent to

@op.processes(machine: "localhost")

Instance Method Summary collapse

Constructor Details

#initialize(op, name, key, hash) ⇒ Entity

Returns a new instance of Entity.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vop/entity.rb', line 11

def initialize(op, name, key, hash)
  @op = op
  @name = name
  @key = key
  @data = {}
  hash.each do |k,v|
    @data[k.to_s] = v
  end

  make_methods_for_hash_keys
  make_methods_for_commands
end

Instance Method Details

#entity_commandsObject



39
40
41
42
43
44
45
46
47
# File 'lib/vop/entity.rb', line 39

def entity_commands
  result = @op.commands.values.select do |command|
    command.params.select do |param|
      param[:name] == @name
    end.count > 0
  end
  @command_count = result.count
  result
end

#inspectObject



24
25
26
# File 'lib/vop/entity.rb', line 24

def inspect
  "Vop::Entity #{@name} (#{@command_count} commands)"
end

#make_methods_for_commandsObject



49
50
51
52
53
54
55
56
57
# File 'lib/vop/entity.rb', line 49

def make_methods_for_commands
  entity_commands.each do |command|
    value = @data[@key]
    self.class.send(:define_method, command.short_name) do |*args|
      ruby_args = args.length > 0 ? args[0] : {}
      @op.execute(command.short_name, ruby_args, { @name => value })
    end
  end
end

#make_methods_for_hash_keysObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/vop/entity.rb', line 28

def make_methods_for_hash_keys
  @data.keys.each do |key|
    #next if ['name', 'key'].include? key.to_s
    next if ['key'].include? key.to_s
    self.class.send(:define_method, key) do |*args|
      ruby_args = args.length > 0 ? args[0] : {}
      @data[key]
    end
  end
end