Class: Finitio::Heading

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/finitio/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.

Constant Summary collapse

DEFAULT_OPTIONS =
{ allow_extra: false }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attributes, options = nil) ⇒ Heading

Returns a new instance of Heading.



13
14
15
16
# File 'lib/finitio/support/heading.rb', line 13

def initialize(attributes, options = nil)
  @attributes = normalize_attributes(attributes)
  @options    = normalize_options(options)
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
61
# File 'lib/finitio/support/heading.rb', line 58

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

#[](attrname) ⇒ Object



18
19
20
# File 'lib/finitio/support/heading.rb', line 18

def [](attrname)
  @attributes[attrname]
end

#allow_extra?Boolean

Returns:

  • (Boolean)


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

def allow_extra?
  options[:allow_extra]
end

#each(&bl) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/finitio/support/heading.rb', line 32

def empty?
  size == 0
end

#fetch(attrname) ⇒ Object



22
23
24
25
26
# File 'lib/finitio/support/heading.rb', line 22

def fetch(attrname)
  @attributes.fetch(attrname) do
    raise Error, "No such attribute `#{attrname}`"
  end
end

#hashObject



63
64
65
# File 'lib/finitio/support/heading.rb', line 63

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

#multi?Boolean

Returns:

  • (Boolean)


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

def multi?
  allow_extra? || any?{|attr| not(attr.required?) }
end

#sizeObject



28
29
30
# File 'lib/finitio/support/heading.rb', line 28

def size
  @attributes.size
end

#to_nameObject



49
50
51
52
53
54
55
56
# File 'lib/finitio/support/heading.rb', line 49

def to_name
  name = map(&:to_name).join(', ')
  if allow_extra?
    name << ", " unless empty?
    name << "..."
  end
  name
end