class HashSerializer::Attribute

Attributes

name[R]
options[R]

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/hash_serializer/attribute.rb, line 5
def initialize(name, options = {})
  @name = name
  @options = options
end

Public Instance Methods

as_json(object) click to toggle source
# 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_name() click to toggle source
# File lib/hash_serializer/attribute.rb, line 10
def key_name
  options[:as] || name
end