Class: Jinx::Transformer
- Includes:
- Enumerable, Collection
- Defined in:
- lib/jinx/helpers/transformer.rb
Overview
This Transformer helper class applies a transformer block to a base enumeration.
Instance Method Summary collapse
-
#each ⇒ Object
Calls the block on each item after this Transformer’s transformer block is applied.
-
#initialize(enum = [], &transformer) ⇒ Transformer
constructor
A new instance of Transformer.
-
#on(enum) ⇒ Object
Sets the base Enumerable on which this Transformer operates and returns this transformer, e.g.: transformer = Transformer.new { |n| n * 2 } transformer.on([1, 2, 3]).to_a #=> [2, 4, 6].
Methods included from Enumerable
#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure
Methods included from Collection
#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union
Constructor Details
#initialize(enum = [], &transformer) ⇒ Transformer
Returns a new instance of Transformer.
8 9 10 11 |
# File 'lib/jinx/helpers/transformer.rb', line 8 def initialize(enum=[], &transformer) @base = enum @xfm = transformer end |
Instance Method Details
#each ⇒ Object
Calls the block on each item after this Transformer’s transformer block is applied.
22 23 24 |
# File 'lib/jinx/helpers/transformer.rb', line 22 def each @base.each { |item| yield(item.nil? ? nil : @xfm.call(item)) } end |
#on(enum) ⇒ Object
Sets the base Enumerable on which this Transformer operates and returns this transformer, e.g.:
transformer = Transformer.new { |n| n * 2 }
transformer.on([1, 2, 3]).to_a #=> [2, 4, 6]
16 17 18 19 |
# File 'lib/jinx/helpers/transformer.rb', line 16 def on(enum) @base = enum self end |