Class: Bmg::Operator::Ungroup

Inherits:
Object
  • Object
show all
Includes:
Unary
Defined in:
lib/bmg/operator/ungroup.rb

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Unary

#bind

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

#_count, #bind, #count, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_csv, #to_json, #to_xlsx, #type, #update, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #minus, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#cross_product, #exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, operand, attrs) ⇒ Ungroup

Returns a new instance of Ungroup.



6
7
8
9
10
# File 'lib/bmg/operator/ungroup.rb', line 6

def initialize(type, operand, attrs)
  @type = type
  @operand = operand
  @attrs = attrs
end

Instance Method Details

#_each(tuple, attr, attrs, &bl) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bmg/operator/ungroup.rb', line 35

def _each(tuple, attr, attrs, &bl)
  rva = tuple[attr] || []
  rva.each do |rvt|
    t = tuple.merge(rvt).tap{|t| t.delete(attr) }
    if attrs.empty?
      yield(t)
    else
      _each(t, attrs[0], attrs[1..-1], &bl)
    end
  end
end

#each(&bl) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bmg/operator/ungroup.rb', line 18

def each(&bl)
  return to_enum unless block_given?
  if type.knows_keys? && type.keys.any?{|k| (k & attrs).empty? }
    operand.each do |tuple|
      _each(tuple, attrs[0], attrs[1..-1], &bl)
    end
  else
    with_dups = []
    operand.each do |tuple|
      _each(tuple, attrs[0], attrs[1..-1]){|t|
        with_dups << t
      }
    end
    with_dups.uniq.each(&bl)
  end
end

#to_astObject



47
48
49
# File 'lib/bmg/operator/ungroup.rb', line 47

def to_ast
  [ :ungroup, operand.to_ast, attrs ]
end