Class: HashSerializer::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_serializer/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
# File 'lib/hash_serializer/attribute.rb', line 5

def initialize(name, options = {})
  @name = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/hash_serializer/attribute.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/hash_serializer/attribute.rb', line 3

def options
  @options
end

Instance Method Details

#as_json(object) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/hash_serializer/attribute.rb', line 14

def as_json(object)
  value = object.public_send(name)
  case options[:format]
  when Symbol
    value = object.formats[options[:format]].call(value)
  end
  { key_name => value }
end

#key_nameObject



10
11
12
# File 'lib/hash_serializer/attribute.rb', line 10

def key_name
  options[:as] || name
end