Class: Vop::Entity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, type, key, data) ⇒ Entity

Returns a new instance of Entity.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vop/objects/entity.rb', line 7

def initialize(op, type, key, data)
  @op = op
  @type = type
  @key = key
  @data = data

  unless @data.has_key? @key
    raise "key #{key} not found in data : #{data.keys.sort}"
  end

  make_methods_for_commands
  make_method_for_id
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/vop/objects/entity.rb', line 5

def data
  @data
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/vop/objects/entity.rb', line 5

def key
  @key
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/vop/objects/entity.rb', line 5

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @data[key]
end

#entity_commandsObject

all commands that have a parameter with the same name as the entity are considered eligible for this entity (TODO that’s too broad, isn’t it?)



37
38
39
40
41
42
43
44
45
# File 'lib/vop/objects/entity.rb', line 37

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

#idObject



21
22
23
# File 'lib/vop/objects/entity.rb', line 21

def id
  @data[@key]
end

#make_method_for_idObject



63
64
65
66
67
# File 'lib/vop/objects/entity.rb', line 63

def make_method_for_id
  self.class.send(:define_method, @key) do |*args|
    id
  end
end

#make_methods_for_commandsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vop/objects/entity.rb', line 47

def make_methods_for_commands
  entity_commands.each do |command|
    # TODO this is very similar to code in Vop.<<
    self.class.send(:define_method, command.short_name) do |*args, &block|
      $logger.debug "[#{@type}:#{id}] #{command.short_name} (#{args.pretty_inspect}, block? #{block_given?})"
      ruby_args = args.length > 0 ? args[0] : {}
      # TODO we might want to do this only if there's a block param defined
      # TODO this does not work if *args comes with a scalar default param
      if block
        ruby_args["block"] = block
      end
      @op.execute(command.short_name, ruby_args, { @type.to_s => id })
    end
  end
end

#pluginObject



29
30
31
32
33
# File 'lib/vop/objects/entity.rb', line 29

def plugin
  if @data.has_key? "plugin_name"
    @op.plugin(@data["plugin_name"])
  end
end

#to_sObject



69
70
71
# File 'lib/vop/objects/entity.rb', line 69

def to_s
  "Vop::Entity (#{@type})"
end