Class: Realize::Collection::AtIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/realize/collection/at_index.rb

Overview

Transformer to get an item of a collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:) ⇒ AtIndex

Returns a new instance of AtIndex.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/realize/collection/at_index.rb', line 18

def initialize(index:)
  raise ArgumentError, 'index is required' if index.to_s.empty?

  @index = index.to_i

  freeze
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



16
17
18
# File 'lib/realize/collection/at_index.rb', line 16

def index
  @index
end

Instance Method Details

#transform(_resolver, value, _time, _record) ⇒ Object



26
27
28
# File 'lib/realize/collection/at_index.rb', line 26

def transform(_resolver, value, _time, _record)
  value.is_a?(Array) ? value[index] : value
end