Class: Alf::Engine::Materialize::Hash
- Inherits:
-
Object
- Object
- Alf::Engine::Materialize::Hash
- Includes:
- Cog
- Defined in:
- lib/alf-engine/alf/engine/materialize/hash.rb
Overview
Provides in-memory materialization through a ruby Hash.
This class acts as a Cog, that it, it is an enumerable of tuples. No particular ordering is guaranteed. In addition, the class provides indexed access through the ‘[]` method.
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"}
]
op = Materialize::Hash.new(rel, AttrList[:city])
op.to_a
# => same as rel, no ordering guaranteed
op[:city => "London"].to_a
# => [
{:name => "Jones", :city => "London"},
{:name => "Blake", :city => "London"}
]
op[:city => "London"].to_a
# => [
{:name => "Jones", :city => "London"},
{:name => "Blake", :city => "London"}
]
op[:city => "Athens"].to_a
# => []
Instance Attribute Summary collapse
-
#allbut ⇒ Boolean
readonly
Hash on all but specified attributes?.
-
#key ⇒ AttrList
readonly
Attributes for the hash key.
-
#operand ⇒ Enumerable
readonly
The operand.
Instance Method Summary collapse
-
#[](key_tuple, project = false) ⇒ Cog
Returns tuples that match a given key.
- #_each(&block) ⇒ Object
-
#clean ⇒ Object
Frees the materizalied hash.
-
#each_pair(&block) ⇒ Object
Yields indexed (key, tuples) pairs in turn.
-
#initialize(operand, key, allbut = false) ⇒ Hash
constructor
Creates a Materialize::Hash instance.
-
#prepare ⇒ Object
Prepare through materialization of the operand as a hash.
Methods included from Cog
Constructor Details
#initialize(operand, key, allbut = false) ⇒ Hash
Creates a Materialize::Hash instance
53 54 55 56 57 58 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 53 def initialize(operand, key, allbut = false) @operand = operand @key = key @allbut = allbut @materialized = nil end |
Instance Attribute Details
#allbut ⇒ Boolean (readonly)
Returns Hash on all but specified attributes?.
50 51 52 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 50 def allbut @allbut end |
#key ⇒ AttrList (readonly)
Returns Attributes for the hash key.
47 48 49 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 47 def key @key end |
#operand ⇒ Enumerable (readonly)
Returns The operand.
44 45 46 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 44 def operand @operand end |
Instance Method Details
#[](key_tuple, project = false) ⇒ Cog
Returns tuples that match a given key.
This method returns a Cog instance in all case. En empty Cog is returned if no tuples match the key.
80 81 82 83 84 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 80 def [](key_tuple, project = false) key_tuple = key_for(key_tuple) if project m = materialized m.has_key?(key_tuple) ? m[key_tuple] : [] end |
#_each(&block) ⇒ Object
61 62 63 64 65 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 61 def _each(&block) materialized.each_value do |rel| rel.each(&block) end end |
#clean ⇒ Object
Frees the materizalied hash
103 104 105 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 103 def clean @materialized = nil end |
#each_pair(&block) ⇒ Object
Yields indexed (key, tuples) pairs in turn.
68 69 70 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 68 def each_pair(&block) materialized.each_pair(&block) end |
#prepare ⇒ Object
Prepare through materialization of the operand as a hash
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/alf-engine/alf/engine/materialize/hash.rb', line 89 def prepare @materialized ||= begin h = ::Hash.new{|h,k| h[k] = []} operand.each do |tuple| h[key_for(tuple)] << tuple end h end self end |