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

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

Instance Method Summary collapse

Instance Method Details

#pass_reader(*methods) ⇒ Object

Note:

it does not create an instance variable

Same as attr_reader but links to a subjacent Hash model property



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ecoportal/api/common/content/double_model/attributable/passthrough.rb', line 25

def pass_reader(*methods)
  methods.each do |method|
    method = method.to_s.freeze

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

#pass_writer(*methods) ⇒ Object

Note:

it does not create an instance variable

Same as attr_writer but links to a subjacent Hash model property



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ecoportal/api/common/content/double_model/attributable/passthrough.rb', line 42

def pass_writer(*methods)
  methods.each do |method|
    method = method.to_s.freeze

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

#passthrough(*methods, read_only: false) ⇒ Object

Same as attr_accessor but links to a subjacent Hash model property



58
59
60
61
62
# File 'lib/ecoportal/api/common/content/double_model/attributable/passthrough.rb', line 58

def passthrough(*methods, read_only: false)
  pass_reader(*methods)
  pass_writer(*methods) unless read_only
  self
end