Class: Fast::Json::Serializer::Attr

Inherits:
Object
  • Object
show all
Defined in:
lib/fast/json/serializer/attr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, &block) ⇒ Attr

options can be

- column_name, from which column to get value
- format, method to be called on the value


10
11
12
13
14
15
# File 'lib/fast/json/serializer/attr.rb', line 10

def initialize(name, options, &block)
  @name = name
  @block = block
  @column_name = options[:column_name]&.to_s
  @format = options[:format]
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/fast/json/serializer/attr.rb', line 5

def block
  @block
end

#column_nameObject

Returns the value of attribute column_name.



5
6
7
# File 'lib/fast/json/serializer/attr.rb', line 5

def column_name
  @column_name
end

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/fast/json/serializer/attr.rb', line 5

def format
  @format
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/fast/json/serializer/attr.rb', line 5

def name
  @name
end

Instance Method Details

#calc_value(serializer, hash) ⇒ Object



17
18
19
20
21
22
# File 'lib/fast/json/serializer/attr.rb', line 17

def calc_value(serializer, hash)
  return block.call(serializer, hash) if block

  value = hash[column_name || name]
  format ? value.send(format) : value
end