Class: Alf::Engine::Materialize::Array
- Inherits:
-
Object
- Object
- Alf::Engine::Materialize::Array
- Includes:
- Cog, Alf::Engine::Materialize
- Defined in:
- lib/alf/engine/materialize/array.rb
Overview
Provides in-memory materialization through a ruby Array.
This class acts as a Cog, that it, it is an enumerable of tuples. An optional ordering can be passed at construction.
Materialization occurs at prepare time, with auto-prepare on first access.
Example:
rel = [
{:name => "Jones", :city => "London"},
{:name => "Smith", :city => "Paris"},
{:name => "Blake", :city => "London"}
]
Materialize::Array.new(rel).to_a
# => same as rel, in same order as the source
Materialize::Array.new(rel, Ordering[[:name, :asc]]).to_a
# => [
{:name => "Blake", :city => "London"},
{:name => "Jones", :city => "London"},
{:name => "Smith", :city => "Paris"}
]
Constant Summary
Constants included from Cog
Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS
Instance Attribute Summary collapse
-
#operand ⇒ Enumerable
readonly
The operand.
-
#ordering ⇒ Ordering
readonly
Ordering to ensure (optional).
Attributes included from Compiler::Cog
Instance Method Summary collapse
- #_each(&block) ⇒ Object
-
#clean ⇒ Object
Frees the materizalied hash.
-
#initialize(operand, ordering = nil, expr = nil, compiler = nil) ⇒ Array
constructor
Creates a Materialize::Array instance.
-
#prepare ⇒ Object
Prepare through materialization of the operand as an ordered array.
Methods included from Cog
#arguments, #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, ordering = nil, expr = nil, compiler = nil) ⇒ Array
Creates a Materialize::Array instance
41 42 43 44 45 46 |
# File 'lib/alf/engine/materialize/array.rb', line 41 def initialize(operand, ordering = nil, expr = nil, compiler = nil) super(expr, compiler) @operand = operand @ordering = ordering @materialized = nil end |
Instance Attribute Details
#operand ⇒ Enumerable (readonly)
35 36 37 |
# File 'lib/alf/engine/materialize/array.rb', line 35 def operand @operand end |
#ordering ⇒ Ordering (readonly)
38 39 40 |
# File 'lib/alf/engine/materialize/array.rb', line 38 def ordering @ordering end |
Instance Method Details
#_each(&block) ⇒ Object
49 50 51 |
# File 'lib/alf/engine/materialize/array.rb', line 49 def _each(&block) materialized.each(&block) end |
#clean ⇒ Object
Frees the materizalied hash
67 68 69 |
# File 'lib/alf/engine/materialize/array.rb', line 67 def clean @materialized = nil end |
#prepare ⇒ Object
Prepare through materialization of the operand as an ordered array
56 57 58 59 60 61 62 |
# File 'lib/alf/engine/materialize/array.rb', line 56 def prepare @materialized ||= begin arr = operand.to_a arr.sort!(&ordering.sorter) if ordering arr end end |