Module: Gorillib::Model::ClassMethods

Defined in:
lib/gorillib/resolution.rb

Overview

Modifies the Gorillib metaprogramming to handle deep recursion on Gorillib::Model collections which would prefer to handle arbitrarily complex resolution requirements via their (custom) receive! method

Instance Method Summary collapse

Instance Method Details

#define_collection_receiver(field) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gorillib/resolution.rb', line 27

def define_collection_receiver(field)
 collection_field_name = field.name; collection_type = field.type
  # @param  [Array[Object],Hash[Object]] the collection to merge
  # @return [Gorillib::Collection] the updated collection
  define_meta_module_method("receive_#{collection_field_name}", true) do |coll, &block|
    begin
      existing = read_attribute(collection_field_name)
      if existing and (not collection_type.native?(coll) or existing.respond_to?(:receive!))
        existing.receive!(coll, &block)
      else
        write_attribute(collection_field_name, coll)
      end
    rescue StandardError => err ; err.polish("#{self.class} #{collection_field_name} collection on #{coll}'") rescue nil ; raise ; end
  end
end