module Ecoportal module API module Common module Content class CollectionModel < Content::DoubleModel class << self attr_writer :klass attr_accessor :order_matters, :order_key def items_key @items_key ||= "id" end def items_key=(value) @items_key = value && value.to_s.freeze end # Resolves to the nuclear `Class` of the elements # @note # - use block to define `klass` callback # @param value [Hash] base `doc` (raw object) to create the object with # @yield [doc] identifies the target `class` of the raw object # @yieldparam doc [Hash] # @yieldreturn [Klass] the target `class` # @return [Klass] the target `class` def klass(value = NOT_USED, &block) if block @klass = block block.call(value) if value != NOT_USED elsif used_param?(value) if @klass.is_a?(Proc) @klass.call(value) else resolve_class(@klass, exception: false) end else resolve_class(@klass, exception: false) end end # Generates a new object of the target class # @note # - use block to define `new_item` callback, which will prevail over `klass` # - if `new_item` callback was **not** defined, it is required to defnie `klass` # @param doc [Hash] doc to parse # @note if block is given, it ignores `doc` # @yield [doc, parent, key] creates an object instance of the target `klass` # @yieldparam doc [Hash] # @yieldreturn [Klass] instance object of the target `klass` # @return [Klass] instance object of the target `klass` def new_item(doc = NOT_USED, parent: nil, key: nil, &block) if block @new_item = block elsif used_param?(doc) raise "You should define either a 'klass' or a 'new_item' callback first" unless klass? if @new_item @new_item.call(doc, parent, key) else if target_class = self.klass(doc) doc.is_a?(target_class) ? doc : target_class.new(doc, parent: parent, key: key) else raise "Could not find a class for: #{doc}" end end else raise "To define the 'new_item' callback (factory), you need to use a block" end end # @return [Boolean] are there the factory logics to build item objects defined? def klass? @klass || @new_item end def doc_class(name) dim_class = new_class(name, inherits: Common::Content::ArrayModel) do |klass| klass.order_matters = order_matters klass.uniq = uniq end end # Creates a new object with `doc` where `original_doc` is `[]` def create(doc) self.new([]).tap do |page| page.replace_doc(doc) end end end include Enumerable def initialize(ini_doc = [], parent: self, key: nil) unless self.class.klass? raise "Undefined base 'klass' or 'new_item' callback for #{self.class}" end ini_doc = case ini_doc when Array ini_doc when Enumerable ini_doc.to_a else [] end super(ini_doc, parent: parent, key: key) end # Transforms `value` into the actual `key` to access the object in the doc `Array` def _doc_key(value) #print "*(#{value.class})" return super(value) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel) if id = get_key(value) #print "^" _doc_items.index {|item| get_key(item) == id}.tap do |p| #print "{{#{p}}}" end else raise UnlinkedModel.new("Can't find child: #{value}") end end def length; count; end def empty?; count == 0; end def present?; count > 0; end def each(&block) return to_enum(:each) unless block _items.each(&block) end def _items return @_items if @_items [].tap do |elements| variable_set(:@_items, elements) _doc_items.each do |item_doc| elements << new_item(item_doc) end end end def [](value) items_by_key[get_key(value)] end def values_at(*keys) keys.map {|key| self[key]} end # Tries to find the element `value`, if it exists, it updates it # Otherwise it pushes it to the end # @return the element def upsert!(value) unless value.is_a?(Hash) || value.is_a?(Content::DoubleModel) raise "'Content::DoubleModel' or 'Hash' doc required" end item_doc = value.is_a?(Content::DoubleModel)? value.doc : value if item = self[value] item.replace_doc(JSON.parse(item_doc.to_json)) else item = new_item(item_doc) _doc_items << item.doc end item end protected def order_matters?; self.class.order_matters; end def uniq?; self.class.uniq; end def items_key; self.class.items_key; end # Gets the `key` of the object def get_key(value) case value when Content::DoubleModel value.key when Hash value[items_key] when String value end end def _doc_items replace_doc([]) unless doc.is_a?(Array) doc end # @note it does not support a change of `id` on an existing item def items_by_key return @items_by_key if @indexed {}.tap do |hash| variable_set(:@items_by_key, hash) _items.each {|item| hash[item.key] = item} @indexed = true end end private def new_item(value) self.class.new_item(value, parent: self) end private # Helper to remove tracked down instance variables def variable_remove!(key) if (k = get_key(key)) && (item = @items_by_key[k]) _items.delete(item) if _items.include?(item) @items_by_key.delete(k) else super(key) end end # Removes all the persistent variables def variables_remove! @indexed = false super end end end end end end