Class: Bmg::Keys

Inherits:
Object
  • Object
show all
Defined in:
lib/bmg/support/keys.rb

Instance Method Summary collapse

Constructor Details

#initialize(keys, reduce = false) ⇒ Keys

Returns a new instance of Keys.



4
5
6
# File 'lib/bmg/support/keys.rb', line 4

def initialize(keys, reduce = false)
  @keys = reduce ? reduce(keys) : keys
end

Instance Method Details

#allbut(oldtype, newtype, butlist) ⇒ Object



16
17
18
19
20
# File 'lib/bmg/support/keys.rb', line 16

def allbut(oldtype, newtype, butlist)
  keys = @keys.select{|k| (k & butlist).empty? }
  keys = [newtype.attrlist] if keys.empty?
  Keys.new(keys, false)
end

#autowrap(oldtype, newtype, options) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/bmg/support/keys.rb', line 22

def autowrap(oldtype, newtype, options)
  sep = options[:split] || Operator::Autowrap::DEFAULT_OPTIONS[:split]
  keys = @keys.map{|k|
    k.map{|a| a.to_s.split(sep).first }.uniq.map(&:to_sym)
  }
  Keys.new(keys, false)
end

#group(oldtype, newtype, attrs, as) ⇒ Object



30
31
32
33
34
# File 'lib/bmg/support/keys.rb', line 30

def group(oldtype, newtype, attrs, as)
  keys = [ oldtype.attrlist - attrs ]
  keys += @keys.map{|k| (k & attrs).empty? ? k : (k - attrs) + [as] }
  Keys.new(keys, true)
end

#join(oldtype, newtype, right_type, on) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bmg/support/keys.rb', line 36

def join(oldtype, newtype, right_type, on)
  return nil  unless rkeys = right_type.keys
  return self if rkeys.any?{|k| (k - on).empty? }
  keys = []
  @keys.each do |k1|
    right_type.keys.each do |k2|
      keys << (k1 + k2).uniq
    end
  end
  Keys.new(keys, true)
end

#project(oldtype, newtype, attrlist) ⇒ Object



48
49
50
51
52
# File 'lib/bmg/support/keys.rb', line 48

def project(oldtype, newtype, attrlist)
  keys = @keys.select{|k| k.all?{|a| attrlist.include?(a) } }
  keys = [newtype.attrlist] if keys.empty?
  Keys.new(keys, false)
end

#rename(oldtype, newtype, renaming) ⇒ Object



54
55
56
57
# File 'lib/bmg/support/keys.rb', line 54

def rename(oldtype, newtype, renaming)
  keys = @keys.map{|k| k.map{|a| renaming[a] || a } }
  Keys.new(keys, false)
end

#restrict(oldtype, newtype, predicate) ⇒ Object



59
60
61
62
63
# File 'lib/bmg/support/keys.rb', line 59

def restrict(oldtype, newtype, predicate)
  return self if (cs = predicate.constant_variables).empty?
  keys = @keys.map{|k| k - cs }
  Keys.new(keys, false)
end

#select(&bl) ⇒ Object



10
11
12
# File 'lib/bmg/support/keys.rb', line 10

def select(&bl)
  Keys.new(@keys.select(&bl), false)
end

#to_aObject



79
80
81
# File 'lib/bmg/support/keys.rb', line 79

def to_a
  @keys
end

#union(oldtype, newtype, right_type) ⇒ Object



65
66
67
68
69
70
# File 'lib/bmg/support/keys.rb', line 65

def union(oldtype, newtype, right_type)
  return nil unless rkeys = right_type.keys
  return nil unless (oldtype.predicate & right_type.predicate).contradiction?
  shared = @keys.select{|k| rkeys.include?(k) }
  Keys.new(shared, false)
end

#unwrap(oldtype, newtype, attrs) ⇒ Object



72
73
74
75
# File 'lib/bmg/support/keys.rb', line 72

def unwrap(oldtype, newtype, attrs)
  untouched = @keys.select{|k| (attrs & k).empty? }
  Keys.new(untouched, false)
end