Class: Axiom::Relation::Keys

Inherits:
Object
  • Object
show all
Extended by:
Aliasable
Includes:
Enumerable
Defined in:
lib/axiom/relation/keys.rb

Overview

A set of keys in a Header

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Aliasable

inheritable_alias

Constructor Details

#initialize(keys) ⇒ undefined

Initialize Keys

Examples:

keys = Keys.new([[:id]])

Parameters:



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

Parameters:

Yields:

  • (attributes)

Yield Parameters:

  • attributes (Header, Array)

    the attributes in each key

Returns:



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

Examples:

keys = Keys.new(keys)

Parameters:

  • keys (Array<Header>) (defaults to: EMPTY_ARRAY)

Returns:



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

Examples:

difference = keys.difference(other)

Parameters:

Returns:



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

Examples:

keys = Keys.new(keys)
keys.each { |key| ... }

Yields:

  • (key)

Yield Parameters:

Returns:

  • (self)


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

Examples:

keys.empty?  # => true or false

Returns:

  • (Boolean)


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

Examples:

extended = keys.extend(attributes)

Parameters:

  • attributes (#to_ary)

    the attributes to add to the keys

Returns:



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

Examples:

intersection = keys.intersect(other)

Parameters:

Returns:



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.

Examples:

projected = keys.project(attributes)

Parameters:

  • attributes (#map)

    the attributes to keep in the keys

Returns:



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

Examples:

renamed = keys.rename(aliases)

Parameters:

  • aliases (Aliases)

    the old and new attribute names

Returns:



183
184
185
# File 'lib/axiom/relation/keys.rb', line 183

def rename(aliases)
  new(map { |key| key.rename(aliases) })
end

#to_aryArray

Convert the Keys into an Array

Examples:

array = keys.to_ary

Returns:

  • (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

Examples:

union = keys.union(other)

Parameters:

Returns:



211
212
213
# File 'lib/axiom/relation/keys.rb', line 211

def union(other)
  new(to_ary | other)
end