lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-0.9.7 vs lib/ecoportal/api/common/content/collection_model.rb in ecoportal-api-v2-1.0.1

- old
+ new

@@ -50,20 +50,20 @@ # @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) + def new_item(doc = NOT_USED, parent: nil, key: nil, read_only: false, &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) + doc.is_a?(target_class) ? doc : target_class.new(doc, parent: parent, key: key, read_only: read_only) else raise "Could not find a class for: #{doc}" end end else @@ -86,11 +86,11 @@ include Enumerable inheritable_class_vars :klass, :order_matters, :order_key, :items_key, :new_item - def initialize(ini_doc = [], parent: self, key: nil) + def initialize(ini_doc = [], parent: self, key: nil, read_only: false) unless self.class.klass? raise "Undefined base 'klass' or 'new_item' callback for #{self.class}" end ini_doc = case ini_doc @@ -100,11 +100,11 @@ ini_doc.to_a else [] end - super(ini_doc, parent: parent, key: key) + super(ini_doc, parent: parent, key: key, read_only: read_only) end # @return [Class] the class of the elements of the Collection def items_class self.class.klass @@ -254,10 +254,10 @@ end private def new_item(value) - self.class.new_item(value, parent: self) + self.class.new_item(value, parent: self, read_only: self._read_only) end # Helper to remove tracked down instance variables def variable_remove!(key) if @items_by_key && (k = get_key(key)) && (item = @items_by_key[k])