Class: Axiom::Relation::Keys
- Inherits:
-
Object
- Object
- Axiom::Relation::Keys
- Extended by:
- Aliasable
- Includes:
- Enumerable
- Defined in:
- lib/axiom/relation/keys.rb
Overview
A set of keys in a Header
Class Method Summary collapse
-
.coerce(object) {|attributes| ... } ⇒ Keys
private
Coerce an Array-like object into Keys.
-
.new(keys = EMPTY_ARRAY) ⇒ Keys
Instantiate Keys.
Instance Method Summary collapse
-
#difference(other) ⇒ Keys
Return the difference of the keys with other keys.
-
#each {|key| ... } ⇒ self
Iterate over each key in the Keys.
-
#empty? ⇒ Boolean
Test if there are no keys.
-
#extend(attributes) ⇒ Keys
Return keys with the new attributes added.
-
#initialize(keys) ⇒ undefined
constructor
Initialize Keys.
-
#intersect(other) ⇒ Keys
Return the intersection of the keys with other keys.
-
#project(attributes) ⇒ Keys
Return keys with only the attributes specified.
-
#rename(aliases) ⇒ Keys
Return keys with the attributes renamed.
-
#to_ary ⇒ Array
Convert the Keys into an Array.
-
#union(other) ⇒ Keys
Return the union of the keys with other keys.
Methods included from Aliasable
Constructor Details
#initialize(keys) ⇒ undefined
Initialize Keys
111 112 113 |
# File 'lib/axiom/relation/keys.rb', line 111 def initialize(keys) @keys = self.class.freezer.call(keys) end |
Class Method Details
.coerce(object) {|attributes| ... } ⇒ Keys
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 Keys
30 31 32 33 34 35 36 37 |
# File 'lib/axiom/relation/keys.rb', line 30 def self.coerce(object, &block) if object.kind_of?(self) object else block ||= method(:coerce_attributes) new(object.map(&block)) end end |
.new(keys = EMPTY_ARRAY) ⇒ Keys
Instantiate Keys
49 50 51 52 |
# File 'lib/axiom/relation/keys.rb', line 49 def self.new(keys = EMPTY_ARRAY) assert_irreducible_keys(keys.map(&:to_set)) super end |
Instance Method Details
#difference(other) ⇒ Keys
Return the difference of the keys with other keys
225 226 227 |
# File 'lib/axiom/relation/keys.rb', line 225 def difference(other) new(to_ary - other) end |
#each {|key| ... } ⇒ self
Iterate over each key in the Keys
128 129 130 131 132 |
# File 'lib/axiom/relation/keys.rb', line 128 def each return to_enum unless block_given? to_ary.each { |key| yield key } self end |
#empty? ⇒ Boolean
Test if there are no keys
249 250 251 |
# File 'lib/axiom/relation/keys.rb', line 249 def empty? to_ary.empty? end |
#extend(attributes) ⇒ Keys
Return keys with the new attributes added
164 165 166 167 168 169 170 |
# File 'lib/axiom/relation/keys.rb', line 164 def extend(attributes) if empty? coerce(attributes) else new(map { |key| key | attributes }) end end |
#intersect(other) ⇒ Keys
Return the intersection of the keys with other keys
197 198 199 |
# File 'lib/axiom/relation/keys.rb', line 197 def intersect(other) new(to_ary & other) end |
#project(attributes) ⇒ Keys
Return keys with only the attributes specified
When the attributes do not match any attributes in an existing key, then the key should be removed because it means the attributes it applied against no longer exist in the new header.
149 150 151 |
# File 'lib/axiom/relation/keys.rb', line 149 def project(attributes) new(map { |key| key & attributes }.delete_if(&:empty?)) end |
#rename(aliases) ⇒ Keys
Return keys with the attributes renamed
183 184 185 |
# File 'lib/axiom/relation/keys.rb', line 183 def rename(aliases) new(map { |key| key.rename(aliases) }) end |
#to_ary ⇒ Array
Convert the Keys into an Array
237 238 239 |
# File 'lib/axiom/relation/keys.rb', line 237 def to_ary @keys end |
#union(other) ⇒ Keys
Return the union of the keys with other keys
211 212 213 |
# File 'lib/axiom/relation/keys.rb', line 211 def union(other) new(to_ary | other) end |