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 ⇒ Object (also: #extra_type)
- #allow_extra? ⇒ Boolean
- #each(&bl) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(attrname) ⇒ Object
- #hash ⇒ Object
-
#initialize(attributes, options = nil) ⇒ Heading
constructor
A new instance of Heading.
- #looks_similar?(other) ⇒ Boolean
- #multi? ⇒ Boolean
- #resolve_proxies(system) ⇒ Object
- #size ⇒ Object
- #suppremum(other) ⇒ Object
- #to_name ⇒ Object
- #unconstrained ⇒ 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
90 91 92 93 |
# File 'lib/finitio/support/heading.rb', line 90 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 ⇒ Object Also known as: extra_type
44 45 46 |
# File 'lib/finitio/support/heading.rb', line 44 def allow_extra [:allow_extra] end |
#allow_extra? ⇒ Boolean
40 41 42 |
# File 'lib/finitio/support/heading.rb', line 40 def allow_extra? !![:allow_extra] end |
#each(&bl) ⇒ Object
49 50 51 52 |
# File 'lib/finitio/support/heading.rb', line 49 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
95 96 97 |
# File 'lib/finitio/support/heading.rb', line 95 def hash self.class.hash ^ attributes.hash ^ .hash end |
#looks_similar?(other) ⇒ Boolean
64 65 66 67 68 |
# File 'lib/finitio/support/heading.rb', line 64 def looks_similar?(other) return self if other == self shared, mine, yours = Support.compare_attrs(attributes, other.attributes) shared.length >= mine.length && shared.length >= yours.length end |
#multi? ⇒ Boolean
36 37 38 |
# File 'lib/finitio/support/heading.rb', line 36 def multi? allow_extra? || any?{|attr| not(attr.required?) } end |
#resolve_proxies(system) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/finitio/support/heading.rb', line 102 def resolve_proxies(system) as = attributes.map{|k,a| a.resolve_proxies(system) } opts = .dup if [:allow_extra] && [:allow_extra].is_a?(Type) opts[:allow_extra] = opts[:allow_extra].resolve_proxies(system) end Heading.new(as, opts) end |
#size ⇒ Object
28 29 30 |
# File 'lib/finitio/support/heading.rb', line 28 def size @attributes.size end |
#suppremum(other) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/finitio/support/heading.rb', line 70 def suppremum(other) raise ArgumentError unless other.is_a?(Heading) return self if other == self = { allow_extra: allow_extra? || other.allow_extra? } shared, mine, yours = Support.compare_attrs(attributes, other.attributes) attributes = shared.map{|attr| a1, o1 = self[attr], other[attr] Attribute.new(attr, a1.type.suppremum(o1.type), a1.required && o1.required) } attributes += mine.map{|attrname| attr = self[attrname] Attribute.new(attr.name, attr.type, false) } attributes += yours.map{|attrname| attr = other[attrname] Attribute.new(attr.name, attr.type, false) } Heading.new(attributes, ) end |
#to_name ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/finitio/support/heading.rb', line 54 def to_name name = map(&:to_name).join(', ') if allow_extra? name << ", " unless empty? name << "..." name << ": #{allow_extra.name}" unless allow_extra == ANY_TYPE end name end |