Class: Axiom::Attribute
- Inherits:
-
Object
- Object
- Axiom::Attribute
- Extended by:
- Aliasable, DescendantsTracker
- Includes:
- AbstractType, Visitable, Comparable
- Defined in:
- lib/axiom/attribute.rb,
lib/axiom/attribute/date.rb,
lib/axiom/attribute/time.rb,
lib/axiom/attribute/class.rb,
lib/axiom/attribute/float.rb,
lib/axiom/attribute/tuple.rb,
lib/axiom/attribute/object.rb,
lib/axiom/attribute/string.rb,
lib/axiom/attribute/boolean.rb,
lib/axiom/attribute/decimal.rb,
lib/axiom/attribute/integer.rb,
lib/axiom/attribute/numeric.rb,
lib/axiom/attribute/relation.rb,
lib/axiom/attribute/date_time.rb,
lib/axiom/attribute/comparable.rb,
lib/axiom/attribute/value_comparable.rb,
lib/axiom/attribute/length_comparable.rb
Overview
Abstract base class representing a type of data in a relation tuple
Direct Known Subclasses
Defined Under Namespace
Modules: Comparable, LengthComparable, ValueComparable Classes: Boolean, Class, Date, DateTime, Decimal, Float, Integer, Numeric, Object, Relation, String, Time, Tuple
Constant Summary collapse
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
private
The attribute name.
-
#type ⇒ Class<Types::Object>
readonly
private
The attribute type.
Class Method Summary collapse
-
.coerce(object) ⇒ Attribute
private
Coerce an object into an Attribute.
-
.infer_type(operand) ⇒ Class<Attribute>
private
Infer the Attribute type from the operand.
-
.name_from(object) ⇒ Symbol
private
Extract the attribute name from the object.
Instance Method Summary collapse
-
#call(tuple) ⇒ Object
Extract the value corresponding to this attribute from a tuple.
-
#include?(value) ⇒ Boolean
Test if the value matches the attribute constraints.
-
#initialize(name, options = EMPTY_HASH) ⇒ undefined
constructor
private
Initialize an Attribute.
-
#optional? ⇒ Boolean
Test if the attribute is optional.
-
#rename(new_name) ⇒ Attribute
Rename an attribute.
-
#required? ⇒ Boolean
Test if the attribute is required.
Methods included from Aliasable
Methods included from Visitable
Constructor Details
#initialize(name, options = EMPTY_HASH) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize an Attribute
90 91 92 93 94 |
# File 'lib/axiom/attribute.rb', line 90 def initialize(name, = EMPTY_HASH) @name = name.to_sym @required = .fetch(:required, true) @type = self.class.type end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The attribute name
18 19 20 |
# File 'lib/axiom/attribute.rb', line 18 def name @name end |
#type ⇒ Class<Types::Object> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The attribute type
25 26 27 |
# File 'lib/axiom/attribute.rb', line 25 def type @type end |
Class Method Details
.coerce(object) ⇒ Attribute
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Coerce an object into an Attribute
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/axiom/attribute.rb', line 35 def self.coerce(object) if object.kind_of?(Attribute) object elsif equal?(Attribute) Object.coerce(object) else name, type, = object klass = type ? const_get(type.name) : self klass.new(name, || EMPTY_HASH) end end |
.infer_type(operand) ⇒ Class<Attribute>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Infer the Attribute type from the operand
70 71 72 73 74 75 76 |
# File 'lib/axiom/attribute.rb', line 70 def self.infer_type(operand) if operand.respond_to?(:type) operand.type else Types::Type.descendants.detect { |type| type.include?(operand) } end end |
.name_from(object) ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract the attribute name from the object
55 56 57 58 59 60 61 |
# File 'lib/axiom/attribute.rb', line 55 def self.name_from(object) if object.respond_to?(:name) object.name else Array(object).first.to_sym end end |
Instance Method Details
#call(tuple) ⇒ Object
Extract the value corresponding to this attribute from a tuple
107 108 109 |
# File 'lib/axiom/attribute.rb', line 107 def call(tuple) tuple[self] end |
#include?(value) ⇒ Boolean
Test if the value matches the attribute constraints
167 168 169 |
# File 'lib/axiom/attribute.rb', line 167 def include?(value) valid_or_optional?(value, &type.method(:include?)) end |
#optional? ⇒ Boolean
Test if the attribute is optional
152 153 154 |
# File 'lib/axiom/attribute.rb', line 152 def optional? !required? end |
#rename(new_name) ⇒ Attribute
Make this have the same API as functions
Rename an attribute
124 125 126 127 128 129 130 |
# File 'lib/axiom/attribute.rb', line 124 def rename(new_name) if name.equal?(new_name) self else self.class.new(new_name, required: required?) end end |
#required? ⇒ Boolean
Test if the attribute is required
140 141 142 |
# File 'lib/axiom/attribute.rb', line 140 def required? @required end |