Module: Ecoportal::API::Common::Content::DoubleModel::Attributable::Nesting::ClassMethods

Defined in:
lib/ecoportal/api/common/content/double_model/attributable/nesting.rb,
lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb

Instance Method Summary collapse

Instance Method Details

#_cascaded_attribute!(meth, doc_key = meth, inherited: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb', line 19

def _cascaded_attribute!(meth, doc_key = meth, inherited: false)
  meth          = meth.to_sym
  doc_key       = doc_key.to_s

  dont_override = _cascaded_attributes.key?(meth) && inherited
  return _cascaded_attributes[meth] if dont_override

  _cascaded_attributes[meth] = doc_key
  subclasses.each do |subclass|
    subclass._cascaded_attribute!(meth, doc_key, inherited: true)
  end
end

#_cascaded_attributesObject



32
33
34
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb', line 32

def _cascaded_attributes
  @_cascaded_attributes ||={}
end

#_cascaded_doc_keysObject



40
41
42
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb', line 40

def _cascaded_doc_keys
  _cascaded_attributes.values.uniq
end

#_cascaded_methodsObject



36
37
38
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb', line 36

def _cascaded_methods
  _cascaded_attributes.keys
end

#embeds_many(method, key: method, klass: nil, enum_class: nil, order_matters: false, order_key: nil, read_only: read_only? ) ⇒ Object

Note:
  • if you have a dedicated Enumerable class to manage many, you should use :enum_class
  • otherwise, just indicate the child class in :klass and it will auto generate the class

Parameters:

  • method (Symbol)

    the method that exposes the embeded object

  • key (Symbol) (defaults to: method)

    the key that embeds it to the underlying Hash model

  • order_matters (Boolean) (defaults to: false)

    to state if the order will matter

  • klass (Class, String) (defaults to: nil)

    the class of the individual elements it embeds

  • enum_class (Class, String) (defaults to: nil)

    the class of the collection that will hold the individual elements

  • read_only (Boolean) (defaults to: read_only? )

    whether or not should try to work around items klass missing a key

    • If set to true this is meant only for read purposes (won't be able to successufully insert)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 90

def embeds_many(
  method,
  key:           method,
  klass:         nil,
  enum_class:    nil,
  order_matters: false,
  order_key:     nil,
  read_only:     read_only?
)
  if enum_class
    eclass = enum_class
  elsif klass
    eclass = new_class("#{method}::#{klass}", inherits: CollectionModel) do |dim_class|
      # NOTE: new_class may resolve the namespace of the class to an already existing class
      dim_class.klass       ||= klass
      dim_class.order_matters = order_matters
      dim_class.order_key     = order_key
      dim_class.read_only! if read_only
    end
  else
    raise "You should either specify the 'klass' of the elements or the 'enum_class'"
  end

  embed(
    method,
    key:       key,
    multiple:  true,
    klass:     eclass,
    read_only: read_only
  ) do |instance_of_called_method|
    # keep reference to the original class to resolve the `klass` dependency
    # See stackoverflow: https://stackoverflow.com/a/73709529/4352306
    referrer_class = instance_of_called_method.class
    # eclass is of type CollectionModel, it must have `::klass` class method.
    eclass.klass   = {referrer_class => klass} if klass
    # This helps `resolve_class` to correctly resolve a symbol
    # by using referrer_class as a base module to resolve it
  end
end

#embeds_one(method, klass:, key: method, read_only: read_only?, , nullable: false) ⇒ Object

Helper to embed one nested object under one property

Parameters:

  • method (Symbol)

    the method that exposes the embeded object

  • key (Symbol) (defaults to: method)

    the key that embeds it to the underlying Hash model

  • nullable (Boolean) (defaults to: false)

    to specify if this object can be nil

  • read_only (Boolean) (defaults to: read_only?, )

    if the subjacent model should be read_only

  • klass (Class, String)

    the class of the embedded object



69
70
71
72
73
74
75
76
77
78
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 69

def embeds_one(method, klass:, key: method, read_only: read_only?, nullable: false)
  embed(
    method,
    key:       key,
    nullable:  nullable,
    read_only: read_only,
    multiple:  false,
    klass:     klass
  )
end

#passarray(*methods, order_matters: true, uniq: true) ⇒ Object

To link as plain Array to a subjacent Hash model property

Parameters:

  • methods (Array<Symbol>)

    the method that exposes the value as well as its key in the underlying Hash model.

  • order_matters (Boolean) (defaults to: true)

    does the order matter

  • uniq (Boolean) (defaults to: true)

    should it contain unique elements



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 36

def passarray(*methods, order_matters: true, uniq: true)
  methods.each do |method|
    method = method.to_s.freeze
    var    = instance_variable_name(method)

    _cascaded_attribute!(method)

    dim_class = new_class(method, inherits: ArrayModel) do |klass|
      klass.order_matters = order_matters
      klass.uniq          = uniq
    end

    define_method method do
      return instance_variable_get(var) if instance_variable_defined?(var)

      dim_class.new(
        parent:    self,
        key:       method,
        read_only: read_only?
      ).tap do |new_obj|
        variable_set(var, new_obj)
        _cascaded_attribute!(method)
      end
    end
  end
end