Class: Realize::Collection::AtIndex
- Inherits:
-
Object
- Object
- Realize::Collection::AtIndex
- Defined in:
- lib/realize/collection/at_index.rb
Overview
Transformer to get an item of a collection
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
-
#initialize(index:) ⇒ AtIndex
constructor
A new instance of AtIndex.
- #transform(_resolver, value, _time, _record) ⇒ Object
Constructor Details
#initialize(index:) ⇒ AtIndex
Returns a new instance of AtIndex.
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
#index ⇒ Object (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 |