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, Link, Reflection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeField

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

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



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

def block
  @block
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



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

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



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

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.

Parameters:

Returns:

  • (Bool)


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.

Parameters:

  • The (Serializer)

    serializer instance for which the value is computed.

Returns:

  • (Object)

    value



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