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

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

Instance Method Summary collapse

Instance Method Details

#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)


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
129
130
131
132
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 94

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 73

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



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
62
63
64
65
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 37

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

    _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(
        doc[obj_k],
        parent:    self,
        key:       obj_k,
        read_only: read_only?
      ).tap do |new_obj|
        variable_set(var, new_obj)
        # ensure children classes propagation!

        _cascaded_attribute!(method)
      end
    end
  end
end