lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-1.1.5 vs lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-1.1.6
- old
+ new
@@ -9,10 +9,12 @@
class << self
attr_writer :klass
attr_accessor :order_matters, :order_key
+ # The attr that has been defined as `passkey`
+ # in the item class
def items_key
@items_key ||= "id"
end
def items_key=(value)
@@ -20,27 +22,34 @@
end
# Resolves to the nuclear `Class` of the elements
# @note
# - use block to define `klass` callback
+ # @note When `klass` is resolved, if the items are of type
+ # `DoubleModel`, it sets on the collection class the `items_key`
# @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
+ @klass
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.tap do |result|
+ next unless result.is_a?(Class)
+ next unless result < Ecoportal::API::Common::Content::DoubleModel
+ self.items_key = result.key
end
end
# Generates a new object of the target class
# @note
@@ -125,10 +134,20 @@
#print "^"
_doc_items.index {|item| get_key(item) == id}.tap do |p|
#print "{{#{p}}}"
end
else
- raise UnlinkedModel.new("Can't find child: #{value}")
+ show_str = \
+ case value
+ when Hash
+ value.pretty_inspect
+ when Content::DoubleModel
+ "#{value} with key: #{value.class.key} (items_key: #{self.class.items_key})"
+ else
+ value
+ end
+
+ raise UnlinkedModel, "Can't find child: #{show_str}"
end
end
def length; count; end
def empty?; count == 0; end