Module: Ecoportal::API::Common::Content::DoubleModel::Attributable::Nesting::Keyable::ClassMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/keyable.rb', line 21

def key
  @key
end

Instance Method Details

#key?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/keyable.rb', line 23

def key?
  !!key
end

#passkey(method) ⇒ Object

Note:

Content::CollectionModel needs to find elements in the doc Array. The only way to do it is via the access key (i.e. id). However, there is no chance you can avoid infinite loop for get_key (CollectionModel) without setting an instance variable key at the moment of the object creation, when the doc is firstly received

This method is essential to give stability to the model

Parameters:

  • method (Symbol)

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ecoportal/api/common/content/double_model/attributable/nesting/keyable.rb', line 41

def passkey(method)
  method   = method.to_s.freeze
  var      = instance_variable_name(method)
  self.key = method

  define_method method do
    return instance_variable_get(var) if instance_variable_defined?(var)

    value = send(:doc)[method]
    value = yield(value) if block_given?
    value
  end

  define_method "#{method}=" do |value|
    variable_set(var, value)
    value = yield(value) if block_given?
    send(:doc)[method] = value
  end

  self
end