Class: Alf::Engine::Take

Inherits:
Object
  • Object
show all
Includes:
Cog
Defined in:
lib/alf/engine/take.rb

Overview

Take only a frame of the input relation according to an (offset, limit) parameter.

Example:

res = [
  {:name => "Jones", :price => 12.0, :id => 1},
  {:name => "Smith", :price => 10.0, :id => 2},
  {:name => "Blake", :price => 14.0, :id => 3}
  {:name => "Adams", :price => 13.0, :id => 4}
]
Aggregate.new(res, 1, 2).to_a
# => [
#      {:name => "Smith", :price => 10.0, :id => 2},
#      {:name => "Blake", :price => 14.0, :id => 3}
#    ]

Constant Summary

Constants included from Cog

Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes included from Compiler::Cog

#compiler, #expr

Instance Method Summary collapse

Methods included from Cog

#children, #each, #options, #to_s

Methods included from Compiler::Cog

#cog_orders, #orderedby?, #relation_type, #to_ascii_tree, #to_cog, #to_relation

Constructor Details

#initialize(operand, offset, limit, expr = nil, compiler = nil) ⇒ Take

Creates a Take instance


34
35
36
37
38
39
# File 'lib/alf/engine/take.rb', line 34

def initialize(operand, offset, limit, expr = nil, compiler = nil)
  super(expr, compiler)
  @operand = operand
  @offset  = offset
  @limit   = limit
end

Instance Attribute Details

#limitInteger (readonly)

Returns The frame limit

Returns:

  • (Integer)

    The frame limit


31
32
33
# File 'lib/alf/engine/take.rb', line 31

def limit
  @limit
end

#offsetInteger (readonly)

Returns The frame offset

Returns:

  • (Integer)

    The frame offset


28
29
30
# File 'lib/alf/engine/take.rb', line 28

def offset
  @offset
end

#operandEnumerable (readonly)

Returns The operand

Returns:

  • (Enumerable)

    The operand


25
26
27
# File 'lib/alf/engine/take.rb', line 25

def operand
  @operand
end

Instance Method Details

#_eachObject


42
43
44
45
46
47
48
49
# File 'lib/alf/engine/take.rb', line 42

def _each
  i = -1
  operand.each do |tuple|
    next   if (i += 1) < offset
    return if i >= offset+limit
    yield(tuple)
  end
end

#argumentsObject


51
52
53
# File 'lib/alf/engine/take.rb', line 51

def arguments
  [ offset, limit ]
end