Module: FidoMetadata::Attributes

Instance Method Summary collapse

Instance Method Details

#from_json(hash = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/fido_metadata/attributes.rb', line 27

def from_json(hash = {})
  instance = new
  hash.each do |k, v|
    method_name = :"#{underscore_name(k)}="
    instance.public_send(method_name, v) if instance.respond_to?(method_name)
  end

  instance
end

#json_accessor(name, coercer = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fido_metadata/attributes.rb', line 15

def json_accessor(name, coercer = nil)
  underscored_name = underscore_name(name)
  attr_accessor underscored_name

  if coercer
    define_method(:"#{underscored_name}=") do |value|
      coerced_value = coercer.coerce(value)
      instance_variable_set(:"@#{underscored_name}", coerced_value)
    end
  end
end