Class: Axiom::Algebra::Rename::Aliases
- Inherits:
-
Object
- Object
- Axiom::Algebra::Rename::Aliases
- 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
-
.coerce(attributes, aliases) ⇒ Aliases
private
Coerce a Hash of old and new attributes into Aliases.
-
.new(aliases) ⇒ Aliases
Instantiate new set of Aliases.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare the aliases with other aliases for equivalency.
-
#[](attribute) ⇒ Attribute
Lookup the new attribute given the old attribute.
-
#each {|old, new| ... } ⇒ self
Iterate over each old and new attribute.
-
#empty? ⇒ Boolean
Test if there are no aliases.
-
#initialize(aliases) ⇒ undefined
constructor
Initialize rename aliases.
-
#inverse ⇒ Aliases
Return the inverse aliases.
-
#to_hash ⇒ Hash
Convert the aliases to a Hash.
-
#union(other) ⇒ Aliases
Union the aliases with another set of aliases.
Methods included from Axiom::Aliasable
Constructor Details
#initialize(aliases) ⇒ undefined
Initialize rename aliases
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
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
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
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
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
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
133 134 135 |
# File 'lib/axiom/algebra/rename/aliases.rb', line 133 def empty? @aliases.empty? end |
#inverse ⇒ Aliases
Return the inverse aliases
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_hash ⇒ Hash
Convert the aliases to a 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
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 |