Class: Etna::Clients::Magma::Models

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/clients/magma/models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = {}) ⇒ Models

Returns a new instance of Models.



205
206
207
# File 'lib/etna/clients/magma/models.rb', line 205

def initialize(raw = {})
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



203
204
205
# File 'lib/etna/clients/magma/models.rb', line 203

def raw
  @raw
end

Instance Method Details

#+(other) ⇒ Object



226
227
228
229
230
# File 'lib/etna/clients/magma/models.rb', line 226

def +(other)
  raw_update = {}
  raw_update[other.name] = other.raw
  Models.new({}.update(raw).update(raw_update))
end

#allObject



222
223
224
# File 'lib/etna/clients/magma/models.rb', line 222

def all
  raw.values.map { |r| Model.new(r) }
end

#build_model(model_key) ⇒ Object



213
214
215
# File 'lib/etna/clients/magma/models.rb', line 213

def build_model(model_key)
  Model.new(raw[model_key] ||= {})
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.



234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/etna/clients/magma/models.rb', line 234

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? || model.name.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

#model(model_key) ⇒ Object



217
218
219
220
# File 'lib/etna/clients/magma/models.rb', line 217

def model(model_key)
  return nil unless raw.include?(model_key.to_s)
  Model.new(raw[model_key.to_s])
end

#model_keysObject



209
210
211
# File 'lib/etna/clients/magma/models.rb', line 209

def model_keys
  raw.keys
end

#to_directed_graph(include_casual_links = false) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/etna/clients/magma/models.rb', line 248

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