Class: Axiom::Algebra::Rename::Aliases

Inherits:
Object
  • Object
show all
Extended by:
Axiom::Aliasable
Includes:
Enumerable
Defined in:
lib/axiom/algebra/rename/aliases.rb

Overview

Aliases that map old attributes to new renamed attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Axiom::Aliasable

inheritable_alias

Constructor Details

#initialize(aliases) ⇒ undefined

Initialize rename aliases

Parameters:

  • aliases (Hash)

    the old and new attributes



56
57
58
# File 'lib/axiom/algebra/rename/aliases.rb', line 56

def initialize(aliases)
  @aliases = self.class.freezer.call(aliases)
end

Class Method Details

.coerce(attributes, aliases) ⇒ Aliases

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce a Hash of old and new attributes into Aliases

Parameters:

  • attributes (Header)

    the header containing the old attributes

  • aliases (Aliases, #map)

    the aliases to coerce

Returns:



187
188
189
190
191
192
193
194
# 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) ⇒ Aliases

Instantiate new set of Aliases

Examples:

aliases = Aliases.new(aliases)

Parameters:

Returns:



25
26
27
28
# File 'lib/axiom/algebra/rename/aliases.rb', line 25

def self.new(aliases)
  assert_unique_aliases(aliases)
  super
end

Instance Method Details

#==(other) ⇒ Boolean

Compare the aliases with other aliases for equivalency

Examples:

aliases == other  # => true or false

Parameters:

  • other (Aliases)

    the other aliases to compare with

Returns:

  • (Boolean)


161
162
163
# File 'lib/axiom/algebra/rename/aliases.rb', line 161

def ==(other)
  cmp?(__method__, other)
end

#[](attribute) ⇒ Attribute

Lookup the new attribute given the old attribute

Examples:

new_attribute = aliases[old_attribute]

Parameters:

  • attribute (Attribute)

    the old attribute

Returns:



71
72
73
# File 'lib/axiom/algebra/rename/aliases.rb', line 71

def [](attribute)
  @aliases.fetch(attribute, attribute)
end

#each {|old, new| ... } ⇒ self

Iterate over each old and new attribute

Examples:

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

Yields:

Yield Parameters:

  • old_attribute (Attribute)

    the old attribute

  • new_attribute (Attribute)

    the new attribute

Returns:

  • (self)


119
120
121
122
123
# 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?Boolean

Test if there are no aliases

Examples:

aliases.empty?  # => true or false

Returns:

  • (Boolean)


133
134
135
# File 'lib/axiom/algebra/rename/aliases.rb', line 133

def empty?
  @aliases.empty?
end

#inverseAliases

Return the inverse aliases

Examples:

inverse = aliases.inverse

Returns:



145
146
147
148
# File 'lib/axiom/algebra/rename/aliases.rb', line 145

def inverse
  self.class.new(@aliases.invert)
    .memoize(inverse: self)
end

#to_hashHash

Convert the aliases to a Hash

Examples:

hash = aliases.to_hash

Returns:

  • (Hash)


173
174
175
# File 'lib/axiom/algebra/rename/aliases.rb', line 173

def to_hash
  @aliases
end

#union(other) ⇒ Aliases

Union the aliases with another set of aliases

Examples:

new_aliases = aliases.union(other)

Parameters:

  • other (Aliases)

    the aliases to union with

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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