Class: Axiom::Relation::Header
- Inherits:
-
Object
- Object
- Axiom::Relation::Header
- Extended by:
- Aliasable
- Includes:
- Enumerable
- Defined in:
- lib/axiom/relation/header.rb
Overview
A set of attributes that correspond to values in each tuple
Direct Known Subclasses
Instance Attribute Summary collapse
-
#keys ⇒ Keys
readonly
private
The header keys.
Class Method Summary collapse
-
.coerce(object, options = EMPTY_HASH) {|attribute| ... } ⇒ Header
private
Coerce an Array-like object into a Header.
-
.new(attributes = EMPTY_ARRAY, _options = EMPTY_HASH) ⇒ Header
Instantiate a Header.
Instance Method Summary collapse
-
#call(name) ⇒ Attribute
Lookup an attribute in the header given a name.
-
#difference(other) ⇒ Header
Return the difference of the header with another header.
-
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the header.
-
#empty? ⇒ Boolean
Test if there are no attributes.
-
#extend(attributes) ⇒ Header
Return a header with the new attributes added.
-
#initialize(attributes, options) ⇒ undefined
constructor
Initialize a Header.
-
#intersect(other) ⇒ Header
Return the intersection of the header with another header.
-
#project(attributes) ⇒ Header
Return a header with only the attributes specified.
-
#rename(aliases) ⇒ Header
Return a header with the attributes renamed.
-
#to_ary ⇒ Array
Convert the Header into an Array.
-
#union(other) ⇒ Header
Return the union of the header with another header.
Methods included from Aliasable
Constructor Details
#initialize(attributes, options) ⇒ undefined
Initialize a Header
126 127 128 129 130 131 |
# File 'lib/axiom/relation/header.rb', line 126 def initialize(attributes, ) @attributes = freeze_object(attributes) @options = freeze_object() @attribute_for = Hash[@attributes.map(&:name).zip(@attributes)] @keys = coerce_keys end |
Instance Attribute Details
#keys ⇒ Keys (readonly)
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.
The header keys
24 25 26 |
# File 'lib/axiom/relation/header.rb', line 24 def keys @keys end |
Class Method Details
.coerce(object, options = EMPTY_HASH) {|attribute| ... } ⇒ Header
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 an Array-like object into a Header
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/axiom/relation/header.rb', line 39 def self.coerce(object, = EMPTY_HASH) if object.kind_of?(self) object else # Find the attribute with the block if possible, then fallback # to the default coercion method. block = lambda do |attribute| block_given? and yield(attribute) or coerce_attribute(attribute) end new(Array(object).map(&block), ) end end |
.new(attributes = EMPTY_ARRAY, _options = EMPTY_HASH) ⇒ Header
Instantiate a Header
64 65 66 67 |
# File 'lib/axiom/relation/header.rb', line 64 def self.new(attributes = EMPTY_ARRAY, = EMPTY_HASH) assert_unique_names(attributes.map(&:name)) super end |
Instance Method Details
#call(name) ⇒ Attribute
Lookup an attribute in the header given a name
164 165 166 167 168 169 |
# File 'lib/axiom/relation/header.rb', line 164 def call(name) @attribute_for.fetch(Attribute.name_from(name)) do |attribute_name| raise UnknownAttributeError, "the attribute #{attribute_name} is unknown" end end |
#difference(other) ⇒ Header
Return the difference of the header with another header
The original keys from the header are reused because a difference does not affect key constraints.
278 279 280 281 |
# File 'lib/axiom/relation/header.rb', line 278 def difference(other) other = coerce(other) new(to_ary - other, :keys => keys - other.keys) end |
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the header
147 148 149 150 151 |
# File 'lib/axiom/relation/header.rb', line 147 def each return to_enum unless block_given? to_ary.each { |attribute| yield attribute } self end |
#empty? ⇒ Boolean
Test if there are no attributes
303 304 305 |
# File 'lib/axiom/relation/header.rb', line 303 def empty? to_ary.empty? end |
#extend(attributes) ⇒ Header
Return a header with the new attributes added
The original keys from the header are reused because an extension does not affect key constraints.
203 204 205 |
# File 'lib/axiom/relation/header.rb', line 203 def extend(attributes) new(to_ary + coerce(attributes), :keys => keys) end |
#intersect(other) ⇒ Header
Return the intersection of the header with another header
The unique keys from the headers become the new keys because an intersection strengthens key constraints.
241 242 243 244 245 |
# File 'lib/axiom/relation/header.rb', line 241 def intersect(other) other = coerce(other) attributes = to_ary & other new(attributes, :keys => (keys | other.keys).project(attributes)) end |
#project(attributes) ⇒ Header
Return a header with only the attributes specified
The unique keys intersected with the attributes become the new keys because a projection strengthens key constraints.
185 186 187 |
# File 'lib/axiom/relation/header.rb', line 185 def project(attributes) coerce(attributes, :keys => keys.project(attributes)) end |
#rename(aliases) ⇒ Header
Return a header with the attributes renamed
The attributes in the original keys are renamed, but a rename does not otherwise affect the key constraints.
221 222 223 224 225 226 |
# File 'lib/axiom/relation/header.rb', line 221 def rename(aliases) new( map { |attribute| aliases[attribute] }, :keys => keys.rename(aliases) ) end |
#to_ary ⇒ Array
Convert the Header into an Array
291 292 293 |
# File 'lib/axiom/relation/header.rb', line 291 def to_ary @attributes end |
#union(other) ⇒ Header
Return the union of the header with another header
The common keys from the headers become the new keys because a union weakens key constraints.
260 261 262 263 |
# File 'lib/axiom/relation/header.rb', line 260 def union(other) other = coerce(other) new(to_ary | other, :keys => keys & other.keys) end |