lib/ecoportal/api/common/content/double_model.rb in ecoportal-api-v2-1.1.6 vs lib/ecoportal/api/common/content/double_model.rb in ecoportal-api-v2-1.1.7

- old
+ new

@@ -36,10 +36,20 @@ def new_uuid(length: 24) uid(length) end + def read_only? + @read_only = false if @read_only.nil? + @read_only + end + + # Be able to define if a class should be read-only + def read_only! + @read_only = true + end + # Same as `attr_reader` but links to a subjacent `Hash` model property # @note it does **not** create an _instance variable_ # @param methods [Array<Symbol>] the method that exposes the value # as well as its `key` in the underlying `Hash` model. def pass_reader(*methods) @@ -174,11 +184,11 @@ klass.uniq = uniq end define_method method do return instance_variable_get(var) if instance_variable_defined?(var) - new_obj = dim_class.new(parent: self, key: method, read_only: self._read_only) + new_obj = dim_class.new(parent: self, key: method, read_only: self.read_only?) variable_set(var, new_obj) end end end @@ -200,18 +210,19 @@ # @param klass [Class, String] the class of the individual elements it embeds # @param enum_class [Class, String] the class of the collection that will hold the individual elements # @param read_only [Boolean] whether or not should try to **work around** items `klass` missing a `key` # - If set to `true` this is meant only for read purposes (won't be able to successufully insert) def embeds_many(method, key: method, klass: nil, enum_class: nil, - order_matters: false, order_key: nil, read_only: false) + order_matters: false, order_key: nil, read_only: self.read_only?) if enum_class eclass = enum_class elsif klass eclass = new_class("#{method}::#{klass}", inherits: Common::Content::CollectionModel) do |dim_class| dim_class.klass = klass dim_class.order_matters = order_matters dim_class.order_key = order_key + dim_class.read_only! if read_only end else raise "You should either specify the 'klass' of the elements or the 'enum_class'" end embed(method, key: key, multiple: true, klass: eclass, read_only: read_only) do |instance_with_called_method| @@ -222,11 +233,11 @@ end end private - def embed(method, key: method, nullable: false, multiple: false, klass:, read_only: false, &block) + def embed(method, key: method, nullable: false, multiple: false, klass:, read_only: self.read_only?, &block) method = method.to_s.freeze var = instance_variable_name(method).freeze k = key.to_s.freeze # retrieving method (getter) @@ -249,11 +260,11 @@ end end end end - embedded_class.new(doc[k], parent: self, key: k, read_only: self._read_only || read_only).tap do |obj| + embedded_class.new(doc[k], parent: self, key: k, read_only: self.read_only? || read_only).tap do |obj| variable_set(var, obj) end end end @@ -261,16 +272,16 @@ def model_forced_keys @forced_model_keys ||= {} end end - inheritable_class_vars :forced_model_keys, :key + inheritable_class_vars :forced_model_keys, :key, :read_only # `_key` refers to the parent's property that links to this model attr_reader :_parent, :_key, :_read_only - def initialize(doc = {}, parent: self, key: nil, read_only: false) + def initialize(doc = {}, parent: self, key: nil, read_only: self.class.read_only?) @_dim_vars = [] @_parent = parent || self @_key = key || self @_read_only = read_only @@ -285,10 +296,16 @@ self.key = doc[key_method] #puts "\n$(#{self.key}<=>#{self.class})" end end + # @note `read_only` allows for some optimizations, such as storing values + # in instance variables, for optimization purposes + def read_only? + @_read_only + end + def root return self if is_root? _parent.root end @@ -406,9 +423,12 @@ def doc_var? !!defined?(@doc) end + # Both requisites + # @note that for optimization purposes, `@doc` var may be used when + # the object is `read_only?` def is_root? _parent == self && doc_var? end def linked?