Module: Ecoportal::API::Common::Content::DoubleModel::Attributable::Enforce::ClassMethods

Defined in:
lib/ecoportal/api/common/content/double_model/attributable/enforce.rb

Instance Method Summary collapse

Instance Method Details

#enforce!(doc) ⇒ Object

Ensures doc has the model_forced_keys. If it doesn't, it adds those missing with the defined default values



38
39
40
41
42
43
44
45
46
47
# File 'lib/ecoportal/api/common/content/double_model/attributable/enforce.rb', line 38

def enforce!(doc)
  return unless doc.is_a?(Hash)
  return if     model_forced_keys.empty?

  model_forced_keys.each do |key, default|
    doc[key] = default unless doc.key?(key)
  end

  doc
end

#passforced(method, default:, read_only: false) ⇒ Object

Note:
  • DoubleModel can be used with objects that do not use patch_ver
  • This ensures that they will get the correct patch update model.

These are methods that should always be present in patch update

Parameters:

  • method (Symbol)

    the method that exposes the value as well as its key in the underlying Hash model.

  • default (Value)

    the default value that this key will be written in the model when it doesn't exist.



31
32
33
34
# File 'lib/ecoportal/api/common/content/double_model/attributable/enforce.rb', line 31

def passforced(method, default:, read_only: false)
  model_forced_keys[method.to_s.freeze] = default
  passthrough(method, read_only: read_only)
end