lib/ecoportal/api/common/content/double_model.rb in ecoportal-api-oozes-0.5.5 vs lib/ecoportal/api/common/content/double_model.rb in ecoportal-api-oozes-0.5.6

- old
+ new

@@ -151,10 +151,18 @@ dklass.order_key = order_key end embeds_one(method, key: key, multiple: true, klass: dim_class) 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 attr_reader :_parent, :_key def initialize(doc = {}, parent: self, key: nil) @@ -248,28 +256,28 @@ #def print # puts JSON.pretty_generate(as_json) # self #end - protected - - def is_root? - _parent == self && !!defined?(@doc) - end - - def linked? - is_root? || !!_parent.doc - end - def replace_doc(new_doc) raise UnlinkedModel.new(from: "#{self.class}#replace_doc", key: _key) unless linked? if is_root? @doc = new_doc else dig_set(_parent.doc, [_doc_key(_key)].flatten, new_doc) _parent.variable_remove!(_key) variables_remove! end + end + + protected + + def is_root? + _parent == self && !!defined?(@doc) + end + + def linked? + is_root? || !!_parent.doc end def replace_original_doc(new_doc) raise UnlinkedModel.new(from: "#{self.class}#replace_original_doc", key: _key) unless linked? if is_root?