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.
7 8 9 10 11 |
# File 'lib/active_model/serializer/field.rb', line 7 def initialize(*) super validate_condition! end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block
6 7 8 |
# File 'lib/active_model/serializer/field.rb', line 6 def block @block end |
#name ⇒ Object
Returns the value of attribute name
6 7 8 |
# File 'lib/active_model/serializer/field.rb', line 6 def name @name end |
#options ⇒ Object
Returns the value of attribute options
6 7 8 |
# File 'lib/active_model/serializer/field.rb', line 6 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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_model/serializer/field.rb', line 33 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.
19 20 21 22 23 24 25 |
# File 'lib/active_model/serializer/field.rb', line 19 def value(serializer) if block serializer.instance_eval(&block) else serializer.read_attribute_for_serialization(name) end end |