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
- #embeds_many(method, key: method, klass: nil, enum_class: nil, order_matters: false, order_key: nil, read_only: read_only? ) ⇒ Object
-
#embeds_one(method, klass:, key: method, read_only: read_only?, , nullable: false) ⇒ Object
Helper to embed one nested object under one property.
-
#passarray(*methods, order_matters: true, uniq: true) ⇒ Object
To link as plain
Arrayto a subjacentHashmodel property.
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
Enumerableclass to managemany, you should use:enum_class - otherwise, just indicate the child class in
:klassand it will auto generate the class
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 ( 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 ( 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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 73 def (method, klass:, key: method, read_only: read_only?, nullable: false) ( 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
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 |