Class: Jrf::RowContext

Inherits:
Object
  • Object
show all
Defined in:
lib/jrf/row_context.rb

Constant Summary collapse

MISSING =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil) ⇒ RowContext

Returns a new instance of RowContext.



26
27
28
29
30
# File 'lib/jrf/row_context.rb', line 26

def initialize(obj = nil)
  @obj = obj
  @__jrf_current_stage = nil
  @__jrf_current_input = obj
end

Instance Attribute Details

#__jrf_current_stage=(value) ⇒ Object (writeonly)

Sets the attribute __jrf_current_stage

Parameters:

  • value

    the value to set the attribute __jrf_current_stage to.



10
11
12
# File 'lib/jrf/row_context.rb', line 10

def __jrf_current_stage=(value)
  @__jrf_current_stage = value
end

Class Method Details

.define_reducer(name, &definition) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jrf/row_context.rb', line 13

def define_reducer(name, &definition)
  define_method(name) do |*args, **kwargs, &block|
    spec = definition.call(self, *args, **kwargs, block: block)
    @__jrf_current_stage.step_reduce(
      spec.fetch(:value),
      initial: reducer_initial_value(spec.fetch(:initial)),
      finish: spec[:finish],
      &spec.fetch(:step)
    )
  end
end

Instance Method Details

#_Object



38
39
40
# File 'lib/jrf/row_context.rb', line 38

def _
  @obj
end

#flatObject



42
43
44
# File 'lib/jrf/row_context.rb', line 42

def flat
  Control::Flat.new(current_input)
end

#group_by(key, &block) ⇒ Object



179
180
181
182
# File 'lib/jrf/row_context.rb', line 179

def group_by(key, &block)
  block ||= proc { group }
  @__jrf_current_stage.step_group_by(key, &block)
end

#map(&block) ⇒ Object

Raises:

  • (ArgumentError)


167
168
169
170
171
# File 'lib/jrf/row_context.rb', line 167

def map(&block)
  raise ArgumentError, "map requires a block" unless block

  @__jrf_current_stage.step_map(:map, @obj, &block)
end

#map_values(&block) ⇒ Object

Raises:

  • (ArgumentError)


173
174
175
176
177
# File 'lib/jrf/row_context.rb', line 173

def map_values(&block)
  raise ArgumentError, "map_values requires a block" unless block

  @__jrf_current_stage.step_map(:map_values, @obj, &block)
end

#reduce(initial, &block) ⇒ Object

Raises:

  • (ArgumentError)


161
162
163
164
165
# File 'lib/jrf/row_context.rb', line 161

def reduce(initial, &block)
  raise ArgumentError, "reduce requires a block" unless block

  @__jrf_current_stage.step_reduce(current_input, initial: initial, &block)
end

#reset(obj) ⇒ Object



32
33
34
35
36
# File 'lib/jrf/row_context.rb', line 32

def reset(obj)
  @obj = obj
  @__jrf_current_input = obj
  self
end

#select(predicate) ⇒ Object



46
47
48
# File 'lib/jrf/row_context.rb', line 46

def select(predicate)
  predicate ? current_input : Control::DROPPED
end