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
# File 'lib/jrf/row_context.rb', line 26

def initialize(obj = nil)
  @obj = obj
  @__jrf_current_stage = nil
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.allocate_reducer(
      spec.fetch(:value),
      initial: reducer_initial_value(spec.fetch(:initial)),
      finish: spec[:finish],
      &spec.fetch(:step)
    )
  end
end

Instance Method Details

#_Object



36
37
38
# File 'lib/jrf/row_context.rb', line 36

def _
  @obj
end

#flatObject



40
41
42
# File 'lib/jrf/row_context.rb', line 40

def flat
  Control::Flat.new(@obj)
end

#group_by(key, &block) ⇒ Object



176
177
178
179
# File 'lib/jrf/row_context.rb', line 176

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

#map(&block) ⇒ Object

Raises:

  • (ArgumentError)


164
165
166
167
168
# File 'lib/jrf/row_context.rb', line 164

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

  @__jrf_current_stage.allocate_map(:array, @obj, &block)
end

#map_values(&block) ⇒ Object

Raises:

  • (ArgumentError)


170
171
172
173
174
# File 'lib/jrf/row_context.rb', line 170

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

  @__jrf_current_stage.allocate_map(:hash, @obj, &block)
end

#reduce(initial, &block) ⇒ Object

Raises:

  • (ArgumentError)


158
159
160
161
162
# File 'lib/jrf/row_context.rb', line 158

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

  @__jrf_current_stage.allocate_reducer(@obj, initial: initial, &block)
end

#reset(obj) ⇒ Object



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

def reset(obj)
  @obj = obj
  self
end

#select(predicate) ⇒ Object



44
45
46
# File 'lib/jrf/row_context.rb', line 44

def select(predicate)
  predicate ? @obj : Control::DROPPED
end