Class: Vop::Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, definition, data) ⇒ Entity

Returns a new instance of Entity.



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

def initialize(op, definition, data)
  @op = op
  @type = definition.short_name
  @key = definition.key
  @definition = definition
  @data = data

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

  make_methods_for_commands
  make_methods_for_data
  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

Class Method Details

.from_json(op, json_data) ⇒ Object



103
104
105
106
107
108
# File 'lib/vop/objects/entity.rb', line 103

def self.from_json(op, json_data)
  parsed = JSON.parse(json_data)
  entity_name = parsed["entity"]
  definition = op.entities[entity_name]
  new(op, definition, parsed["data"])
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/vop/objects/entity.rb', line 27

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

#ancestor_namesObject



37
38
39
40
# File 'lib/vop/objects/entity.rb', line 37

def ancestor_names
  # TODO : would be nice if this operation was transitive
  @op.list_contribution_targets(source_command: @definition.name)
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?)



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vop/objects/entity.rb', line 44

def entity_commands
  namified = ancestor_names.map { |x| x.carefully_singularize }
  similar_names = [ @type.to_s ] + namified
  # TODO [performance] this is probably expensive, move into definition time?
  @op.commands.values.flat_map do |command|
    similar_names.map do |similar_name|
      # TODO we might also want to check if param.entity?
      next unless command.params.any? { |param| param.name == similar_name }
      [command.short_name, similar_name]
    end.compact
  end.to_h
end

#idObject



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

def id
  @data[@key]
end

#make_method_for_idObject



89
90
91
92
93
# File 'lib/vop/objects/entity.rb', line 89

def make_method_for_id
  define_singleton_method @key.to_sym do |*args|
    id
  end
end

#make_methods_for_commandsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vop/objects/entity.rb', line 57

def make_methods_for_commands
  entity_commands.each do |command_name, similar_name|
    # TODO this used to be very similar to code in Vop.<<
    define_singleton_method command_name.to_sym do |*args, &block|
      $logger.debug "[#{@type}:#{id}] #{command_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
      extra = { similar_name => id }
      if @definition.on
        if @data[@definition.on.to_s]
          extra[@definition.on.to_s] = @data[@definition.on.to_s]
        else
          $logger.warn "entity #{id} does not seem to have data with key #{@definition.on}, though that's required through the 'on' keyword"
        end
      end
      @op.execute(command_name, ruby_args, extra)
    end
  end
end

#make_methods_for_dataObject



81
82
83
84
85
86
87
# File 'lib/vop/objects/entity.rb', line 81

def make_methods_for_data
  @data.each do |k,v|
    define_singleton_method k.to_sym do |*args|
      v
    end
  end
end

#pluginObject



31
32
33
34
35
# File 'lib/vop/objects/entity.rb', line 31

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

#to_json(options = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/vop/objects/entity.rb', line 95

def to_json(options = nil)
  {
      entity: type,
      key: key,
      data: data
  }.to_json(options)
end

#to_sObject



110
111
112
# File 'lib/vop/objects/entity.rb', line 110

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