Class: ActiveModel::Serializer::Field

Inherits:
Struct
  • Object
show all
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

Attribute, Reflection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeField



7
8
9
10
11
# File 'lib/active_model/serializer/field.rb', line 7

def initialize(*)
  super

  validate_condition!
end

Instance Attribute Details

#blockObject

Returns the value of attribute block



6
7
8
# File 'lib/active_model/serializer/field.rb', line 6

def block
  @block
end

#nameObject

Returns the value of attribute name



6
7
8
# File 'lib/active_model/serializer/field.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options



6
7
8
# File 'lib/active_model/serializer/field.rb', line 6

def options
  @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