Class: USerializer::Attribute
- Inherits:
-
Object
- Object
- USerializer::Attribute
- Defined in:
- lib/userializer/attribute.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#initialize(key, opts, block) ⇒ Attribute
constructor
A new instance of Attribute.
- #merge_attributes(res, ser, opts) ⇒ Object
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
#key ⇒ Object (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 |