Class: Vop::Entity
- Inherits:
-
Object
- Object
- Vop::Entity
- Defined in:
- lib/vop/objects/entity.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#entity_commands ⇒ Object
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?).
- #id ⇒ Object
-
#initialize(op, type, key, data) ⇒ Entity
constructor
A new instance of Entity.
- #make_method_for_id ⇒ Object
- #make_methods_for_commands ⇒ Object
- #plugin ⇒ Object
- #to_s ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/vop/objects/entity.rb', line 5 def data @data end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/vop/objects/entity.rb', line 5 def key @key end |
#type ⇒ Object (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_commands ⇒ Object
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 |
#id ⇒ Object
21 22 23 |
# File 'lib/vop/objects/entity.rb', line 21 def id @data[@key] end |
#make_method_for_id ⇒ Object
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_commands ⇒ Object
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 |
#plugin ⇒ Object
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_s ⇒ Object
69 70 71 |
# File 'lib/vop/objects/entity.rb', line 69 def to_s "Vop::Entity (#{@type})" end |