Class: BrightSerializer::Attribute
- Inherits:
-
Object
- Object
- BrightSerializer::Attribute
- Defined in:
- lib/bright_serializer/attribute.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#transformed_key ⇒ Object
Returns the value of attribute transformed_key.
Instance Method Summary collapse
- #condition?(object, params) ⇒ Boolean
-
#initialize(key, condition, entity, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #serialize(serializer_instance, object, params) ⇒ Object
Constructor Details
#initialize(key, condition, entity, &block) ⇒ Attribute
Returns a new instance of Attribute.
10 11 12 13 14 15 |
# File 'lib/bright_serializer/attribute.rb', line 10 def initialize(key, condition, entity, &block) @key = key @condition = condition @block = block @entity = entity ? Entity::Base.new(entity) : nil end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/bright_serializer/attribute.rb', line 7 def block @block end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
7 8 9 |
# File 'lib/bright_serializer/attribute.rb', line 7 def condition @condition end |
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
7 8 9 |
# File 'lib/bright_serializer/attribute.rb', line 7 def entity @entity end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/bright_serializer/attribute.rb', line 7 def key @key end |
#transformed_key ⇒ Object
Returns the value of attribute transformed_key.
8 9 10 |
# File 'lib/bright_serializer/attribute.rb', line 8 def transformed_key @transformed_key end |
Instance Method Details
#condition?(object, params) ⇒ Boolean
25 26 27 28 29 |
# File 'lib/bright_serializer/attribute.rb', line 25 def condition?(object, params) return true unless @condition @condition.call(object, params) end |
#serialize(serializer_instance, object, params) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/bright_serializer/attribute.rb', line 17 def serialize(serializer_instance, object, params) return unless object value = attribute_value(serializer_instance, object, params) value.respond_to?(:serializable_hash) ? value.serializable_hash : value end |