Class: Qrb::Heading

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/qrb/support/heading.rb

Overview

Helper class for tuple and relation types.

A heading is a set of attributes, with the constraint that no two attributes have the same name.

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Heading

Returns a new instance of Heading.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/qrb/support/heading.rb', line 11

def initialize(attributes)
  unless attributes.is_a?(Enumerable) and \
         attributes.all?{|a| a.is_a?(Attribute) }
    raise ArgumentError, "Enumerable[Attribute] expected"
  end

  @attributes = {}
  attributes.each do |attr|
    if @attributes[attr.name]
      raise ArgumentError, "Attribute names must be unique"
    end
    @attributes[attr.name] = attr
  end
  @attributes.freeze
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
47
# File 'lib/qrb/support/heading.rb', line 44

def ==(other)
  return nil unless other.is_a?(Heading)
  attributes == other.attributes
end

#each(&bl) ⇒ Object



35
36
37
38
# File 'lib/qrb/support/heading.rb', line 35

def each(&bl)
  return to_enum unless bl
  @attributes.values.each(&bl)
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/qrb/support/heading.rb', line 31

def empty?
  size == 0
end

#hashObject



49
50
51
# File 'lib/qrb/support/heading.rb', line 49

def hash
  self.class.hash ^ attributes.hash
end

#sizeObject



27
28
29
# File 'lib/qrb/support/heading.rb', line 27

def size
  @attributes.size
end

#to_nameObject



40
41
42
# File 'lib/qrb/support/heading.rb', line 40

def to_name
  map(&:to_name).join(', ')
end