Class: Alf::Engine::Hierarchize
- Inherits:
-
Object
- Object
- Alf::Engine::Hierarchize
- Includes:
- Cog
- Defined in:
- lib/alf/engine/hierarchize.rb
Constant Summary
Constants included from Cog
Cog::EMPTY_CHILDREN, Cog::EMPTY_OPTIONS
Instance Attribute Summary collapse
-
#children ⇒ AttrName
readonly
Attribute name for children.
-
#id ⇒ AttrList
readonly
Attribute list of the id.
-
#operand ⇒ Enumerable
readonly
The operand.
-
#parent ⇒ AttrList
readonly
Attribute list for the parent.
-
#relation_type ⇒ Class
readonly
The type of the children relation.
-
#tuple_type ⇒ Class
readonly
The type of the children tuples.
Attributes included from Compiler::Cog
Instance Method Summary collapse
- #_each(&block) ⇒ Object
- #arguments ⇒ Object
-
#initialize(operand, id, parent, children, expr = nil, compiler = nil) ⇒ Hierarchize
constructor
Creates a Hierarchize instance.
- #to_relation ⇒ Object
Methods included from Cog
Methods included from Compiler::Cog
#cog_orders, #orderedby?, #to_ascii_tree, #to_cog
Constructor Details
#initialize(operand, id, parent, children, expr = nil, compiler = nil) ⇒ Hierarchize
Creates a Hierarchize instance
25 26 27 28 29 30 31 |
# File 'lib/alf/engine/hierarchize.rb', line 25 def initialize(operand, id, parent, children, expr = nil, compiler = nil) super(expr, compiler) @operand = operand @id = id @parent = parent @children = children end |
Instance Attribute Details
#children ⇒ AttrName (readonly)
Returns Attribute name for children.
16 17 18 |
# File 'lib/alf/engine/hierarchize.rb', line 16 def children @children end |
#id ⇒ AttrList (readonly)
Returns Attribute list of the id.
10 11 12 |
# File 'lib/alf/engine/hierarchize.rb', line 10 def id @id end |
#operand ⇒ Enumerable (readonly)
Returns The operand.
7 8 9 |
# File 'lib/alf/engine/hierarchize.rb', line 7 def operand @operand end |
#parent ⇒ AttrList (readonly)
Returns Attribute list for the parent.
13 14 15 |
# File 'lib/alf/engine/hierarchize.rb', line 13 def parent @parent end |
#relation_type ⇒ Class (readonly)
Returns the type of the children relation.
19 20 21 |
# File 'lib/alf/engine/hierarchize.rb', line 19 def relation_type @relation_type end |
#tuple_type ⇒ Class (readonly)
Returns the type of the children tuples.
22 23 24 |
# File 'lib/alf/engine/hierarchize.rb', line 22 def tuple_type @tuple_type end |
Instance Method Details
#_each(&block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/alf/engine/hierarchize.rb', line 34 def _each(&block) by_id = Hash.new{|h,k| h[k] = {children => nil} } operand.each do |tuple| tuple = tuple.to_hash infer_types(tuple) unless @relation_type # extract my key and my parent's key tuple_key, parent_key = keys_of(tuple) # this will be my tuple by_id[tuple_key] = tuple.merge(by_id[tuple_key]) # add me to my parent unless same tuple unless tuple_key==parent_key my_tuple = tuple_type.new(by_id[tuple_key]) by_id[parent_key][children] ||= relation_type.new(Set.new) by_id[parent_key][children].send(:reused_instance) << my_tuple end end # Now output tuples that are their own parent by_id.each_pair do |k,tuple| tuple_key, parent_key = keys_of(tuple) tuple[children] ||= relation_type.empty yield(tuple) if tuple_key==parent_key end end |
#arguments ⇒ Object
62 63 64 |
# File 'lib/alf/engine/hierarchize.rb', line 62 def arguments [ id, parent, children ] end |
#to_relation ⇒ Object
66 67 68 69 |
# File 'lib/alf/engine/hierarchize.rb', line 66 def to_relation tuples = to_set relation_type.new(tuples) end |