Class: Etna::Clients::Magma::Models
- Inherits:
-
Object
- Object
- Etna::Clients::Magma::Models
- Defined in:
- lib/etna/clients/magma/models.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #all ⇒ Object
- #build_model(model_key) ⇒ Object
- #each ⇒ Object
-
#find_reciprocal(model_name: nil, model: self.model(model_name), link_attribute_name: nil, attribute: model&.template&.attributes&.attribute(link_attribute_name), link_model: self.model(attribute&.link_model_name)) ⇒ Object
Can look up reciprocal links by many means.
-
#initialize(raw = {}) ⇒ Models
constructor
A new instance of Models.
- #is_table?(model_name) ⇒ Boolean
- #model(model_key) ⇒ Object
- #model_keys ⇒ Object
- #to_directed_graph(include_casual_links = false) ⇒ Object
- #update(model_name, model) ⇒ Object
Constructor Details
#initialize(raw = {}) ⇒ Models
Returns a new instance of Models.
219 220 221 |
# File 'lib/etna/clients/magma/models.rb', line 219 def initialize(raw = {}) @raw = raw end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
217 218 219 |
# File 'lib/etna/clients/magma/models.rb', line 217 def raw @raw end |
Instance Method Details
#+(other) ⇒ Object
246 247 248 249 250 |
# File 'lib/etna/clients/magma/models.rb', line 246 def +(other) raw_update = {} raw_update[other.name] = other.raw Models.new({}.update(raw).update(raw_update)) end |
#all ⇒ Object
242 243 244 |
# File 'lib/etna/clients/magma/models.rb', line 242 def all raw.values.map { |r| Model.new(r) } end |
#build_model(model_key) ⇒ Object
227 228 229 |
# File 'lib/etna/clients/magma/models.rb', line 227 def build_model(model_key) Model.new(raw[model_key] ||= {}) end |
#each ⇒ Object
236 237 238 239 240 |
# File 'lib/etna/clients/magma/models.rb', line 236 def each model_keys.each do |model_key| yield model_key, model(model_key) end end |
#find_reciprocal(model_name: nil, model: self.model(model_name), link_attribute_name: nil, attribute: model&.template&.attributes&.attribute(link_attribute_name), link_model: self.model(attribute&.link_model_name)) ⇒ Object
Can look up reciprocal links by many means. At minimum, a source model or model name must be provided, and either a link_attribute_name, attribute, or link_model.
268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/etna/clients/magma/models.rb', line 268 def find_reciprocal( model_name: nil, model: self.model(model_name), link_attribute_name: nil, attribute: model&.template&.attributes&.attribute(link_attribute_name), link_model: self.model(attribute&.link_model_name) ) return nil if model.nil? return link_model&.template&.attributes&.all&.find { |a| a.name == link_attribute_name } if link_attribute_name link_model&.template&.attributes&.all&.find { |a| a.link_model_name == model.name } end |
#is_table?(model_name) ⇒ Boolean
259 260 261 262 263 264 |
# File 'lib/etna/clients/magma/models.rb', line 259 def is_table?(model_name) parent_model_name = self.model(model_name)&.template&.parent parent_model = self.model(parent_model_name) parent_attribute = parent_model&.template&.attributes&.attribute(model_name) parent_attribute&.attribute_type == 'table' end |
#model(model_key) ⇒ Object
231 232 233 234 |
# File 'lib/etna/clients/magma/models.rb', line 231 def model(model_key) return nil unless raw.include?(model_key.to_s) Model.new(raw[model_key.to_s]) end |
#model_keys ⇒ Object
223 224 225 |
# File 'lib/etna/clients/magma/models.rb', line 223 def model_keys raw.keys end |
#to_directed_graph(include_casual_links = false) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/etna/clients/magma/models.rb', line 282 def to_directed_graph(include_casual_links = false) graph = ::DirectedGraph.new model_keys.sort.each do |model_name| graph.add_connection(model(model_name).template.parent, model_name) if include_casual_links attributes = model(model_name).template.attributes attributes.attribute_keys.each do |attribute_name| attribute = attributes.attribute(attribute_name) linked_model_name = attribute.link_model_name if linked_model_name if attribute.attribute_type == AttributeType::PARENT graph.add_connection(linked_model_name, model_name) elsif attribute.attribute_type == AttributeType::COLLECTION graph.add_connection(model_name, linked_model_name) elsif attribute.attribute_type == AttributeType::CHILD graph.add_connection(model_name, linked_model_name) elsif attribute.attribute_type == AttributeType::LINK graph.add_connection(model_name, linked_model_name) end end end end end graph end |
#update(model_name, model) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/etna/clients/magma/models.rb', line 252 def update(model_name, model) raw[ model_name.to_s ] ||= {} raw[ model_name.to_s ]["template"] = model.template.raw raw[ model_name.to_s ]["documents"] ||= {} raw[ model_name.to_s ]["documents"].update(model.documents.raw) end |