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



90
91
92
93
# File 'lib/finitio/support/heading.rb', line 90

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_extraObject Also known as: extra_type



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

def allow_extra
  options[:allow_extra]
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



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

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



95
96
97
# File 'lib/finitio/support/heading.rb', line 95

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

#looks_similar?(other) ⇒ Boolean

Returns:

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

Returns:

  • (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 = options.dup
  if options[:allow_extra] && options[:allow_extra].is_a?(Type)
    opts[:allow_extra] = opts[:allow_extra].resolve_proxies(system)
  end
  Heading.new(as, opts)
end

#sizeObject



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

def size
  @attributes.size
end

#suppremum(other) ⇒ Object

Raises:

  • (ArgumentError)


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
  options = { 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, options)
end

#to_nameObject



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

#unconstrainedObject



113
114
115
# File 'lib/finitio/support/heading.rb', line 113

def unconstrained
  Heading.new(attributes.values.map{|a| a.unconstrained }, options)
end