class Axiom::Algebra::Rename::Aliases

Aliases that map old attributes to new renamed attributes

Public Class Methods

coerce(attributes, aliases) click to toggle source

Coerce a Hash of old and new attributes into Aliases

@param [Header] attributes

the header containing the old attributes

@param [Aliases, map] aliases

the aliases to coerce

@return [Aliases]

@api private

# File lib/axiom/algebra/rename/aliases.rb, line 187
def self.coerce(attributes, aliases)
  return aliases if aliases.kind_of?(Aliases)
  header  = Relation::Header.coerce(attributes)
  renames = aliases.map do |old_attr, new_attr|
    coerce_alias_pair(header, old_attr, new_attr)
  end
  new(Hash[renames])
end
new(aliases) click to toggle source

Instantiate new set of Aliases

@example

aliases = Aliases.new(aliases)

@param [Hash{Attribute => Attribute}] aliases

@return [Aliases]

@api public

Calls superclass method
# File lib/axiom/algebra/rename/aliases.rb, line 25
def self.new(aliases)
  assert_unique_aliases(aliases)
  super
end
new(aliases) click to toggle source

Initialize rename aliases

@param [Hash] aliases

the old and new attributes

@return [undefined]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 56
def initialize(aliases)
  @aliases = self.class.freezer.call(aliases)
end

Private Class Methods

assert_unique_aliases(aliases) click to toggle source

Asset the aliases are unique

@param [Hash{Attribute => Attribute}] aliases

@return [undefined]

@raise [DuplicateAliasError]

raised when the aliases are duplicates

@api private

# File lib/axiom/algebra/rename/aliases.rb, line 40
def self.assert_unique_aliases(aliases)
  if aliases.values.uniq!
    fail DuplicateAliasError, 'the aliases must be unique'
  end
end
coerce_alias_pair(attributes, old_attr, new_attr) click to toggle source

Coerce old and new attributes into Attribute objects

@param [Header] attributes

the header containing the old attributes

@param [Symbol, Attribute] old_attr

the old attribute name or Attribute object

@param [Symbol, Attribute] new_attr

the new attribute name or Attribute object

@return [Array(Attribute, Attribute)]

@api private

# File lib/axiom/algebra/rename/aliases.rb, line 208
def self.coerce_alias_pair(attributes, old_attr, new_attr)
  old_attr = attributes[old_attr]
  new_attr = old_attr.rename(new_attr) if new_attr.kind_of?(Symbol)
  [old_attr, new_attr]
end

Public Instance Methods

==(other) click to toggle source

Compare the aliases with other aliases for equivalency

@example

aliases == other  # => true or false

@param [Aliases] other

the other aliases to compare with

@return [Boolean]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 161
def ==(other)
  cmp?(__method__, other)
end
[](attribute) click to toggle source

Lookup the new attribute given the old attribute

@example

new_attribute = aliases[old_attribute]

@param [Attribute] attribute

the old attribute

@return [Attribute]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 71
def [](attribute)
  @aliases.fetch(attribute, attribute)
end
each() { |old_attribute, new_attribute| ... } click to toggle source

Iterate over each old and new attribute

@example

aliases = Aliases.new(old => new)
aliases.each { |old, new| ... }

@yield [old, new]

@yieldparam [Attribute] old_attribute

the old attribute

@yieldparam [Attribute] new_attribute

the new attribute

@return [self]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 119
def each
  return to_enum unless block_given?
  @aliases.each { |old_attribute, new_attribute| yield old_attribute, new_attribute }
  self
end
empty?() click to toggle source

Test if there are no aliases

@example

aliases.empty?  # => true or false

@return [Boolean]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 133
def empty?
  @aliases.empty?
end
inverse() click to toggle source

Return the inverse aliases

@example

inverse = aliases.inverse

@return [Aliases]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 145
def inverse
  self.class.new(@aliases.invert)
    .memoize(inverse: self)
end
to_hash() click to toggle source

Convert the aliases to a Hash

@example

hash = aliases.to_hash

@return [Hash]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 173
def to_hash
  @aliases
end
union(other) click to toggle source

Union the aliases with another set of aliases

@example

new_aliases = aliases.union(other)

@param [Aliases] other

the aliases to union with

@return [Aliases]

@api public

# File lib/axiom/algebra/rename/aliases.rb, line 86
def union(other)
  other_aliases = other.to_hash.dup
  inverted      = other_aliases.invert

  # Remove aliases that cancel out, and preserve different aliases
  each do |old_attribute, new_attribute|
    old_attribute = inverted.fetch(old_attribute, old_attribute)
    other_aliases.delete(old_attribute)

    unless old_attribute.eql?(new_attribute)
      other_aliases[old_attribute] = new_attribute
    end
  end

  self.class.new(other_aliases)
end