Class: Finitio::Attribute
- Inherits:
-
Object
- Object
- Finitio::Attribute
- Includes:
- Metadata
- Defined in:
- lib/finitio/support/attribute.rb
Overview
Helper class for tuple and relation attributes.
An attribute is simply a ‘(name: AttrName, type: Type)` pair, where the type is a Finitio type.
Constant Summary
Constants included from Metadata
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
(also: #required?)
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#fetch_on(arg, &bl) ⇒ Object
Fetch the attribute on ‘arg`, which is expected to be a Hash object.
- #hash ⇒ Object
-
#initialize(name, type, required = true, metadata = nil) ⇒ Attribute
constructor
A new instance of Attribute.
- #optional? ⇒ Boolean
- #resolve_proxies(system) ⇒ Object
- #to_name ⇒ Object
- #unconstrained ⇒ Object
Methods included from Metadata
#metadata, #metadata=, #metadata?
Constructor Details
#initialize(name, type, required = true, metadata = nil) ⇒ Attribute
Returns a new instance of Attribute.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/finitio/support/attribute.rb', line 11 def initialize(name, type, required = true, = nil) unless name.is_a?(Symbol) raise ArgumentError, "Symbol expected for attribute name, got `#{name}`" end unless type.is_a?(Type) raise ArgumentError, "Type expected for attribute domain, got `#{type}`" end @name, @type, @required, @metadata = name, type, required, end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
22 23 24 |
# File 'lib/finitio/support/attribute.rb', line 22 def name @name end |
#required ⇒ Object (readonly) Also known as: required?
Returns the value of attribute required.
22 23 24 |
# File 'lib/finitio/support/attribute.rb', line 22 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
22 23 24 |
# File 'lib/finitio/support/attribute.rb', line 22 def type @type end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
48 49 50 51 |
# File 'lib/finitio/support/attribute.rb', line 48 def ==(other) other.is_a?(Attribute) && name==other.name && \ type==other.type && required==other.required end |
#fetch_on(arg, &bl) ⇒ Object
Fetch the attribute on ‘arg`, which is expected to be a Hash object.
This method allows working with ruby hashes having either Symbols or Strings as keys. It ensures that no Symbol is created by the rest of the code, since this would provide a DoS attack vector under MRI.
35 36 37 38 39 40 41 42 |
# File 'lib/finitio/support/attribute.rb', line 35 def fetch_on(arg, &bl) unless arg.respond_to?(:fetch) raise ArgumentError, "Object responding to `fetch` expected" end arg.fetch(name) do arg.fetch(name.to_s, &bl) end end |
#hash ⇒ Object
54 55 56 |
# File 'lib/finitio/support/attribute.rb', line 54 def hash name.hash ^ type.hash ^ required.hash end |
#optional? ⇒ Boolean
25 26 27 |
# File 'lib/finitio/support/attribute.rb', line 25 def optional? !required? end |
#resolve_proxies(system) ⇒ Object
58 59 60 |
# File 'lib/finitio/support/attribute.rb', line 58 def resolve_proxies(system) Attribute.new(name, type.resolve_proxies(system), required, ) end |
#to_name ⇒ Object
44 45 46 |
# File 'lib/finitio/support/attribute.rb', line 44 def to_name required ? "#{name}: #{type}" : "#{name} :? #{type}" end |