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.
-
#to_ary ⇒ Array
readonly
Convert the Header into an Array.
Class Method Summary collapse
-
.coerce(object, options = EMPTY_HASH) {|attribute| ... } ⇒ Header
private
Coerce an Array-like object into a Header.
-
.new(attributes = EMPTY_ARRAY) ⇒ Header
Instantiate a Header.
Instance Method Summary collapse
-
#context {|context| ... } ⇒ Header
private
Evaluate a block within the context of the header.
-
#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.
-
#fetch(name) ⇒ Attribute
Lookup an attribute in the header given a name.
-
#initialize(attributes, options = EMPTY_HASH) ⇒ 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.
-
#size ⇒ Integer
The number of attributes.
-
#union(other) ⇒ Header
Return the union of the header with another header.
Methods included from Aliasable
Constructor Details
#initialize(attributes, options = EMPTY_HASH) ⇒ undefined
Initialize a Header
136 137 138 139 140 |
# File 'lib/axiom/relation/header.rb', line 136 def initialize(attributes, = EMPTY_HASH) @to_ary = self.class.freezer.call(attributes) @attribute_for = Hash[@to_ary.map(&:name).zip(@to_ary)] @keys = coerce_keys(.fetch(:keys, EMPTY_ARRAY)) 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 |
#to_ary ⇒ Array (readonly)
Convert the Header into an Array
34 35 36 |
# File 'lib/axiom/relation/header.rb', line 34 def to_ary @to_ary 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/axiom/relation/header.rb', line 49 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| coerce_attribute(block_given? && yield(attribute) || attribute) end new(Array(object).map(&block), ) end end |
.new(attributes = EMPTY_ARRAY) ⇒ Header
Instantiate a Header
74 75 76 77 |
# File 'lib/axiom/relation/header.rb', line 74 def self.new(attributes = EMPTY_ARRAY, *) assert_unique_names(attributes.map(&:name)) super end |
Instance Method Details
#context {|context| ... } ⇒ 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.
Evaluate a block within the context of the header
303 304 305 |
# File 'lib/axiom/relation/header.rb', line 303 def context(&block) Evaluator::Context.new(self, &block) 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.
287 288 289 290 |
# File 'lib/axiom/relation/header.rb', line 287 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
156 157 158 159 160 |
# File 'lib/axiom/relation/header.rb', line 156 def each return to_enum unless block_given? to_ary.each { |attribute| yield attribute } self end |
#empty? ⇒ Boolean
Test if there are no attributes
324 325 326 |
# File 'lib/axiom/relation/header.rb', line 324 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.
214 215 216 |
# File 'lib/axiom/relation/header.rb', line 214 def extend(attributes) new(to_ary + coerce(attributes), keys: keys) end |
#fetch(name) ⇒ Attribute
Lookup an attribute in the header given a name
173 174 175 176 177 178 179 180 |
# File 'lib/axiom/relation/header.rb', line 173 def fetch(name) @attribute_for.fetch(Attribute.name_from(name)) do |attribute_name| fail( UnknownAttributeError, "the attribute #{attribute_name} is unknown" ) end 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.
250 251 252 253 254 |
# File 'lib/axiom/relation/header.rb', line 250 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.
196 197 198 |
# File 'lib/axiom/relation/header.rb', line 196 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.
232 233 234 235 |
# File 'lib/axiom/relation/header.rb', line 232 def rename(aliases) aliases = Algebra::Rename::Aliases.coerce(self, aliases) new(map(&aliases.method(:[])), keys: keys.rename(aliases)) end |
#size ⇒ Integer
The number of attributes
312 313 314 |
# File 'lib/axiom/relation/header.rb', line 312 def size to_ary.size 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.
269 270 271 272 |
# File 'lib/axiom/relation/header.rb', line 269 def union(other) other = coerce(other) new(to_ary | other, keys: keys & other.keys) end |