Module: Icss::ReceiverModel

Includes:
Gorillib::Hashlike, Gorillib::Hashlike::TreeMerge, Meta::RecordModel, ActsAsHash, ActsAsLoadable, ActsAsTuple
Included in:
Meta::CodeAsset, Meta::DataAsset, Meta::License, Meta::Message, Meta::MessageSample, Meta::Protocol, Meta::RecordField, Meta::Source, Target
Defined in:
lib/icss/receiver_model.rb,
lib/icss/receiver_model/tree_merge.rb,
lib/icss/receiver_model/validations.rb,
lib/icss/receiver_model/acts_as_hash.rb,
lib/icss/receiver_model/acts_as_tuple.rb,
lib/icss/receiver_model/acts_as_catalog.rb,
lib/icss/receiver_model/acts_as_loadable.rb,
lib/icss/receiver_model/active_model_shim.rb

Defined Under Namespace

Modules: ActiveModelShim, ActsAsCatalog, ActsAsHash, ActsAsLoadable, ActsAsTuple, ClassMethods, Validations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsTuple

#to_tuple

Methods included from Meta::RecordModel

#attr_set?, #receive!, #to_zaml

Methods included from ActsAsLoadable

#merge_from_file!

Methods included from ActsAsHash

#[], #[]=, #attributes, #delete, #keys, #to_hash

Class Method Details

.included(base) ⇒ Object

put all the things in ClassMethods at class level



16
17
18
19
20
21
# File 'lib/icss/receiver_model.rb', line 16

def self.included base
  base.class_eval do
    include Icss::ReceiverModel::ActiveModelShim
    extend  Icss::ReceiverModel::ClassMethods
  end
end

Instance Method Details

#tree_merge!(other_hash) ⇒ Object

Recursively merges using receive

Modifies the full receiver chain in-place.

For each key in keys,

  • if self’s value is nil, receive the attribute.

  • if self’s attribute is an Array, append to it.

  • if self’s value responds to tree_merge!, tree merge it.

  • if self’s value responds_to merge!, merge! it.

  • otherwise, receive the value from other_hash



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/icss/receiver_model/tree_merge.rb', line 14

def tree_merge!(other_hash)
  super(other_hash) do |key, self_val, other_val|
    field = self.class.field_named(key)
    if field && self_val.is_a?(Array) && field.has_key?(:indexed_on)
      index_attr = field[:indexed_on]
      other_val.each do |other_el|
        other_el_name = other_el[index_attr]  or next
        self_el  = self_val.find{|el| el[index_attr].to_s == other_el_name.to_s }
        if self_el then  self_el.tree_merge!(other_el)
        else self_val << other_el
        end
      end
      self_val
    else
      false
    end
  end
  self
end