Class: Finitio::Heading
- Inherits:
-
Object
- Object
- Finitio::Heading
- 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
- #==(other) ⇒ Object
- #[](attrname) ⇒ Object
- #allow_extra? ⇒ Boolean
- #each(&bl) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(attrname) ⇒ Object
- #hash ⇒ Object
-
#initialize(attributes, options = nil) ⇒ Heading
constructor
A new instance of Heading.
- #multi? ⇒ Boolean
- #size ⇒ Object
- #to_name ⇒ Object
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, = nil) @attributes = normalize_attributes(attributes) @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 && == other. end |
#[](attrname) ⇒ Object
18 19 20 |
# File 'lib/finitio/support/heading.rb', line 18 def [](attrname) @attributes[attrname] end |
#allow_extra? ⇒ Boolean
40 41 42 |
# File 'lib/finitio/support/heading.rb', line 40 def allow_extra? [: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
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 |
#hash ⇒ Object
63 64 65 |
# File 'lib/finitio/support/heading.rb', line 63 def hash self.class.hash ^ attributes.hash ^ .hash end |
#multi? ⇒ Boolean
36 37 38 |
# File 'lib/finitio/support/heading.rb', line 36 def multi? allow_extra? || any?{|attr| not(attr.required?) } end |
#size ⇒ Object
28 29 30 |
# File 'lib/finitio/support/heading.rb', line 28 def size @attributes.size end |
#to_name ⇒ Object
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 |