Class: Axiom::Relation::Header

Inherits:
Object
  • Object
show all
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

Operation::Order::DirectionSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Aliasable

inheritable_alias

Constructor Details

#initialize(attributes, options) ⇒ undefined

Initialize a Header

Examples:

header = Header.new(attributes, :keys => [ [ :id ] ])

Parameters:

  • attributes (Array)
  • options (Hash)


126
127
128
129
130
131
# File 'lib/axiom/relation/header.rb', line 126

def initialize(attributes, options)
  @attributes    = freeze_object(attributes)
  @options       = freeze_object(options)
  @attribute_for = Hash[@attributes.map(&:name).zip(@attributes)]
  @keys          = coerce_keys
end

Instance Attribute Details

#keysKeys (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

Returns:



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

Parameters:

  • object (Header, #to_ary)

    the header or attributes

  • options (Hash) (defaults to: EMPTY_HASH)

Yields:

  • (attribute)

Yield Parameters:

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/axiom/relation/header.rb', line 39

def self.coerce(object, options = 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), options)
  end
end

.new(attributes = EMPTY_ARRAY, _options = EMPTY_HASH) ⇒ Header

Instantiate a Header

Examples:

header = Header.new(attributes)

Parameters:

  • attributes (Array<Attribute>) (defaults to: EMPTY_ARRAY)

    optional attributes

  • _options (Hash) (defaults to: EMPTY_HASH)

Returns:



64
65
66
67
# File 'lib/axiom/relation/header.rb', line 64

def self.new(attributes = EMPTY_ARRAY, _options = 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

Examples:

attribute = header.call(:id)

Parameters:

Returns:

  • (Attribute)

    the attribute when the name is known



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.

Examples:

difference = header.difference(other)

Parameters:

Returns:



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

Examples:

header = Header.new(attributes)
header.each { |attribute| ... }

Yields:

  • (attribute)

Yield Parameters:

  • attribute (Attribute)

    each attribute in the header

Returns:

  • (self)


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

Examples:

header.empty?  # => true or false

Returns:

  • (Boolean)


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.

Examples:

extended = header.extend(attributes)

Parameters:

  • attributes (#to_ary)

    the attributes to add to the header

Returns:



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.

Examples:

intersection = header.intersect(other)

Parameters:

Returns:



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.

Examples:

projected = header.project(attributes)

Parameters:

  • attributes (#map)

    the attributes to keep in the header

Returns:



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.

Examples:

renamed = header.rename(aliases)

Parameters:

  • aliases (Aliases)

    the old and new attribute names

Returns:



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_aryArray

Convert the Header into an Array

Examples:

array = header.to_ary

Returns:

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

Examples:

union = header.union(other)

Parameters:

Returns:



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