Class: AppQuery::RowBuilder

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

Overview

Composable pipeline of row transformers.

Appended via << and applied in registration order — earliest pushed runs first, latest pushed runs last (so its return value is what callers see). An empty pipeline is a no-op.

Examples:

rb = AppQuery::RowBuilder.new
rb << ->(row) { row.merge("a" => 1) }
rb << ->(row) { row.merge("b" => 2) }
rb.call({})  # => {"a" => 1, "b" => 2}

Instance Method Summary collapse

Constructor Details

#initialize(procs = []) ⇒ RowBuilder

Returns a new instance of RowBuilder.



170
171
172
# File 'lib/app_query.rb', line 170

def initialize(procs = [])
  @procs = procs
end

Instance Method Details

#<<(callable) ⇒ Object

Append a transformer. Returns self so q.row_builder << proc reads naturally and <<= also works.



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

def <<(callable)
  @procs << callable
  self
end

#call(row) ⇒ Object



181
182
183
# File 'lib/app_query.rb', line 181

def call(row)
  @procs.reduce(row) { |acc, p| p.call(acc) }
end

#dupObject

Independent copy — used by Q#deep_dup so chained queries don't share the parent's pipeline.



191
192
193
# File 'lib/app_query.rb', line 191

def dup
  self.class.new(@procs.dup)
end

#empty?Boolean

Returns:

  • (Boolean)


185
# File 'lib/app_query.rb', line 185

def empty? = @procs.empty?

#sizeObject



187
# File 'lib/app_query.rb', line 187

def size = @procs.size