Module: Cosmos::Entity

Overview

Base Cosmos Entity Assumes PARENT_TYPE, CHILD_TYPES, and valid? are defined on module including this

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Array of children which reside under parent


46
47
48
# File 'lib/cosmos/entity.rb', line 46

def children
  @children
end

#idObject

Unique id of the entity


12
13
14
# File 'lib/cosmos/entity.rb', line 12

def id
  @id
end

#locationObject

Motel::Location in location which entity resides under the parent


18
19
20
# File 'lib/cosmos/entity.rb', line 18

def location
  @location
end

#metadataObject

Additional metadata associated with entity,


49
50
51
# File 'lib/cosmos/entity.rb', line 49

def 
  
end

#nameObject

Human friendly name of entity


15
16
17
# File 'lib/cosmos/entity.rb', line 15

def name
  @name
end

#parentObject

Parent to which entity belongs


33
34
35
# File 'lib/cosmos/entity.rb', line 33

def parent
  @parent
end

#parent_idObject

ID of parent to which entity belongs


30
31
32
# File 'lib/cosmos/entity.rb', line 30

def parent_id
  @parent_id
end

#proxy_toObject

If set to remote node, server will proxy operations relavent to this entity to the specified node


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

def proxy_to
  @proxy_to
end

Instance Method Details

#accepts_resource?(res) ⇒ Boolean

By default cosmos entities do not accept resources

(overridden in certain subclasses)

Returns:

  • (Boolean)

157
158
159
# File 'lib/cosmos/entity.rb', line 157

def accepts_resource?(res)
  false
end

#add_child(child) ⇒ Object Also known as: <<

Add child to entity, ensures it is not present and is valid before adding

Raises:

  • (ArgumentError)

118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cosmos/entity.rb', line 118

def add_child(child)
  raise ArgumentError, child unless self.class::CHILD_TYPES.
                                    include?(child.class.to_s.demodulize) &&
                                    child.valid? && !has_child?(child)

  # ensure child of valid type
  child.location.parent_id = location.id
  child.parent = self
  children << child
  child
end

#each_child(&bl) ⇒ Object

Iterate over children calling block w/ self and each child before calling each_child on children


148
149
150
151
152
153
# File 'lib/cosmos/entity.rb', line 148

def each_child(&bl)
  children.each { |sys|
    bl.call self, sys
    sys.each_child &bl
  }
end

#entity_jsonObject

Return entity json attributes


167
168
169
170
171
172
173
174
175
176
# File 'lib/cosmos/entity.rb', line 167

def entity_json
  {:id        => @id,
   :name      => @name,
   :location  => @location,
   :children  => @children,
   :metadata  => ,
   :parent_id => @parent_id,
   :proxy_to  => @proxy_to
  }
end

#entity_valid?Boolean

Return boolean indicating if entity is valid

Currently tests

  • id is set to a valid (non-empty) string

  • name is set to a valid (non-empty) string

  • location is set to a valid Motel::Location

  • parent_id is set if required

  • parent is nil or instance of parent type

  • children is an array of valid entities of child types

From default initialization the following needs to be set to valid values to form a valid entity:

  • id

  • name

  • location

  • parent_id

TODO implement a centralized 'errors' mechanism so invoker can quickly lookup what is wrong w/ the validation

Returns:

  • (Boolean)

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cosmos/entity.rb', line 98

def entity_valid?
  ch = children

  !@id.nil? && @id.is_a?(String) && @id   != "" &&
  !@name.nil? && @name.is_a?(String) && @name != "" &&

  (self.class::PARENT_TYPE == 'NilClass' ||
     !@proxy_to.nil? || !@parent_id.nil?   ) &&
  # TODO also verify parenti_id and proxy_to aren't both set?

  (@parent.nil? || @parent.class.to_s.demodulize == self.class::PARENT_TYPE) &&
  !@location.nil? && @location.is_a?(Motel::Location) && @location.valid? &&
   ch.is_a?(Array) &&
   ch.all?{ |c|
     self.class::CHILD_TYPES.include?(c.class.to_s.demodulize) &&
     c.valid?
   }
end

#has_child?(child) ⇒ Boolean

Return bool indicating if entity has child

Returns:

  • (Boolean)

142
143
144
# File 'lib/cosmos/entity.rb', line 142

def has_child?(child)
  !children.find { |c| c.id == (child.is_a?(String) ? child : child.id) }.nil?
end

#has_children?Boolean

Return bool indicating if entity has children

Returns:

  • (Boolean)

137
138
139
# File 'lib/cosmos/entity.rb', line 137

def has_children?
  children.size > 0
end

#init_entity(args = {}) ⇒ Object

Cosmos::Entity intializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize entity with

Options Hash (args):

  • :id,'id' (String)

    unqiue id to assign to the entity

  • :name,'name' (String)

    name to assign to the entity

  • :location,'location' (Motel::Location)

    location of the entity, if not specified will automatically be created with coordinates (0,0,0)


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

def init_entity(args={})
  attr_from_args args, :id            => nil,
                       :name          => nil,
                       :location      => nil,
                       :proxy_to      => nil,
                       :parent_id     => nil,
                       :parent        => nil,
                       :children      =>  [],
                       :metadata      =>  {}

  @location = args[:loc] if args.has_key?(:loc)
  @location = Motel::Location.new :coordinates => [0,0,0],
                                  :orientation => [0,0,1] if @location.nil?

  @location.orientation = [0,0,1] if @location.orientation == [nil,nil,nil]

  @location.movement_strategy =
    args[:movement_strategy] if args.has_key?(:movement_strategy)
  @location.movement_strategy = args[:ms] if args.has_key?(:ms)
end

#movement_strategy=(strategy) ⇒ Object

Convenience method to set movement_strategy on entity's location


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

def movement_strategy=(strategy)
  @location.movement_strategy = strategy unless @location.nil?
end

#remove_child(child) ⇒ Object

Remove child from entity


132
133
134
# File 'lib/cosmos/entity.rb', line 132

def remove_child(child)
  children.reject! { |c| c.id == (child.is_a?(String) ? child : child.id) }
end

#to_sObject

Convert entity to string


162
163
164
# File 'lib/cosmos/entity.rb', line 162

def to_s
  self.class.to_s.demodulize + '-' + self.name.to_s
end