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
- #_cascaded_attribute!(meth, doc_key = meth, inherited: false) ⇒ Object
- #_cascaded_attributes ⇒ Object
- #_cascaded_doc_keys ⇒ Object
- #_cascaded_methods ⇒ Object
- #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
#_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_attributes ⇒ Object
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_keys ⇒ Object
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_methods ⇒ Object
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
Enumerableclass to managemany, you should use:enum_class - otherwise, just indicate the child class in
:klassand it will auto generate the class
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 ( 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
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting.rb', line 69 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
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 |