Class: USerializer::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts, block) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
9
10
11
12
# File 'lib/userializer/attribute.rb', line 5

def initialize(key, opts, block)
  @key = key
  @opts = opts
  @block = block

  @skip_nil = opts[:skip_nil] || false
  @conditional_block = opts[:if] || proc { true }
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Instance Method Details

#merge_attributes(res, ser, opts) ⇒ Object



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

def merge_attributes(res, ser, opts)
  return unless @conditional_block.call(ser.object, opts)

  value = @block ? @block.call(ser.object, opts) : ser.send(@key)

  return if value.nil? && @skip_nil

  res[@key] = value
end