Class: ActiveModel::Serializer::Field
- Inherits:
-
Struct
- Object
- Struct
- ActiveModel::Serializer::Field
- Defined in:
- lib/active_model/serializer/field.rb
Overview
Holds all the meta-data about a field (i.e. attribute or association) as it was specified in the ActiveModel::Serializer class. Notice that the field block is evaluated in the context of the serializer.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#excluded?(serializer) ⇒ Bool
private
Decide whether the field should be serialized by the given serializer instance.
-
#initialize ⇒ Field
constructor
A new instance of Field.
-
#value(serializer) ⇒ Object
private
Compute the actual value of a field for a given serializer instance.
Constructor Details
#initialize ⇒ Field
Returns a new instance of Field.
9 10 11 12 13 |
# File 'lib/active_model/serializer/field.rb', line 9 def initialize(*) super validate_condition! end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block
8 9 10 |
# File 'lib/active_model/serializer/field.rb', line 8 def block @block end |
#name ⇒ Object
Returns the value of attribute name
8 9 10 |
# File 'lib/active_model/serializer/field.rb', line 8 def name @name end |
#options ⇒ Object
Returns the value of attribute options
8 9 10 |
# File 'lib/active_model/serializer/field.rb', line 8 def @options end |
Instance Method Details
#excluded?(serializer) ⇒ Bool
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Decide whether the field should be serialized by the given serializer instance.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_model/serializer/field.rb', line 35 def excluded?(serializer) case condition_type when :if !evaluate_condition(serializer) when :unless evaluate_condition(serializer) else false end end |
#value(serializer) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compute the actual value of a field for a given serializer instance.
21 22 23 24 25 26 27 |
# File 'lib/active_model/serializer/field.rb', line 21 def value(serializer) if block serializer.instance_eval(&block) else serializer.read_attribute_for_serialization(name) end end |