Module: Ecoportal::API::Common::Content::CollectionModel::Modifiers::ItemsKlass::ClassMethods
- Defined in:
- lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb
Constant Summary collapse
- NOT_USED =
Content::ClassHelpers::NOT_USED
Instance Attribute Summary collapse
-
#klass(value = NOT_USED) {|doc| ... } ⇒ Class, ...
Resolves to the nuclear
Classof the elements.
Instance Method Summary collapse
-
#klass? ⇒ Boolean
Are there the factory logics to build item objects defined?.
-
#new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false) {|doc, parent, key| ... } ⇒ Klass
Generates a new object of the target class.
-
#new_item_class_based? ⇒ Boolean
Optimization.
Instance Attribute Details
#klass(value = NOT_USED) {|doc| ... } ⇒ Class, ...
Note:
- use block to define
klasscallback
Note:
When klass is resolved, if the items are of type
DoubleModel, it sets on the collection class the items_key
Note:
when klass is directly resolved (not via doc) only once
it will set @klass as resolved and will use this class from now on.
This is an optimization to cut class lookups
Resolves to the nuclear Class of the elements
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 57 def klass(value = NOT_USED, &block) @klass = block if block_given? if @klass.is_a?(Proc) && used_param?(value) @klass.call(value) elsif @klass && !@klass.is_a?(Proc) && !@klass.is_a?(Class) @klass = resolve_class(@klass, exception: false) @klass else @klass end.tap do |result| next unless result.is_a?(Class) next unless result < Ecoportal::API::Common::Content::DoubleModel self.items_key = result.key end end |
Instance Method Details
#klass? ⇒ Boolean
Returns are there the factory logics to build item objects defined?.
37 38 39 |
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 37 def klass? @klass || @new_item end |
#new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false) {|doc, parent, key| ... } ⇒ Klass
Note:
- use block to define
new_itemcallback, which will prevail overklass - if
new_itemcallback was not defined, it is required to defnieklass
Note:
if block is given, it ignores doc
Generates a new object of the target class
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 91 def new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false, &block) if block_given? @new_item = block return end msg = 'To define the new_item callback (factory), you need to use a block' raise msg unless used_param?(doc) msg = 'You should define either a klass or a new_item callback first' raise msg unless klass? return @new_item.call(doc, parent, key) if @new_item.is_a?(Proc) target_class = klass(doc) raise "Could not find a class for: #{doc}" unless target_class return doc if doc.is_a?(target_class) target_class.new(doc, parent: parent, key: key, read_only: read_only) end |
#new_item_class_based? ⇒ Boolean
Optimization
28 29 30 31 32 33 34 |
# File 'lib/ecoportal/api/common/content/collection_model/modifiers/items_klass.rb', line 28 def new_item_class_based? return false if @new_item.is_a?(Proc) return false if klass.is_a?(Proc) return true if klass.is_a?(Class) false end |