Class: Dbee::KeyChain

Inherits:
Object
  • Object
show all
Defined in:
lib/dbee/key_chain.rb

Overview

A KeyChain is a collection of KeyPath objects. It knows how to deal with aggregate methods, such as equality of a set of KeyPath objects and finding an ancestor path in all the KeyPath objects’ ancestor paths. You can pass in either KeyPath instances or strings, which will be coerced to KeyPath objects. Duplicates will also be removed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_paths = []) ⇒ KeyChain

Returns a new instance of KeyChain.



20
21
22
23
24
25
# File 'lib/dbee/key_chain.rb', line 20

def initialize(key_paths = [])
  @key_path_set       = key_paths.map { |k| KeyPath.get(k) }.to_set
  @ancestor_path_set  = @key_path_set.map(&:ancestor_paths).flatten.to_set

  freeze
end

Instance Attribute Details

#ancestor_path_setObject (readonly)

Returns the value of attribute ancestor_path_set.



18
19
20
# File 'lib/dbee/key_chain.rb', line 18

def ancestor_path_set
  @ancestor_path_set
end

#key_path_setObject (readonly)

Returns the value of attribute key_path_set.



18
19
20
# File 'lib/dbee/key_chain.rb', line 18

def key_path_set
  @key_path_set
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



31
32
33
# File 'lib/dbee/key_chain.rb', line 31

def ==(other)
  other.instance_of?(self.class) && key_path_set == other.key_path_set
end

#ancestor_path?(*parts) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/dbee/key_chain.rb', line 36

def ancestor_path?(*parts)
  path = parts.flatten.compact.join(KeyPath::SPLIT_CHAR)

  ancestor_path_set.include?(path)
end

#hashObject



27
28
29
# File 'lib/dbee/key_chain.rb', line 27

def hash
  key_path_set.hash
end

#to_unique_ancestorsObject

Returns a unique set of ancestors by considering all column names to be the same.



43
44
45
46
47
48
# File 'lib/dbee/key_chain.rb', line 43

def to_unique_ancestors # :nodoc:
  normalized_paths = key_path_set.map do |kp|
    KeyPath.new((kp.ancestor_names + COLUMN_PLACEHOLDER).join(KeyPath::SPLIT_CHAR))
  end
  self.class.new(normalized_paths.uniq)
end