Class: Alf::Engine::Materialize::Array
- Inherits:
-
Object
- Object
- Alf::Engine::Materialize::Array
- Includes:
- Cog
- Defined in:
- lib/alf-engine/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"}
]
Instance Attribute Summary collapse
-
#operand ⇒ Enumerable
readonly
The operand.
-
#ordering ⇒ Ordering
readonly
Ordering to ensure (optional).
Instance Method Summary collapse
- #_each(&block) ⇒ Object
-
#clean ⇒ Object
Frees the materizalied hash.
-
#initialize(operand, ordering = nil) ⇒ Array
constructor
Creates a Materialize::Array instance.
-
#prepare ⇒ Object
Prepare through materialization of the operand as an ordered array.
Methods included from Cog
Constructor Details
#initialize(operand, ordering = nil) ⇒ Array
Creates a Materialize::Array instance
40 41 42 43 44 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 40 def initialize(operand, ordering = nil) @operand = operand @ordering = ordering @materialized = nil end |
Instance Attribute Details
#operand ⇒ Enumerable (readonly)
Returns The operand.
34 35 36 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 34 def operand @operand end |
#ordering ⇒ Ordering (readonly)
Returns Ordering to ensure (optional).
37 38 39 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 37 def ordering @ordering end |
Instance Method Details
#_each(&block) ⇒ Object
47 48 49 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 47 def _each(&block) materialized.each(&block) end |
#clean ⇒ Object
Frees the materizalied hash
65 66 67 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 65 def clean @materialized = nil end |
#prepare ⇒ Object
Prepare through materialization of the operand as an ordered array
54 55 56 57 58 59 60 |
# File 'lib/alf-engine/alf/engine/materialize/array.rb', line 54 def prepare @materialized ||= begin arr = operand.to_a arr.sort!(&ordering.sorter) if ordering arr end end |