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/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/date_time.rb,
lib/axiom/attribute/comparable.rb
Overview
Abstract base class representing a type of data in a relation tuple
Direct Known Subclasses
Defined Under Namespace
Modules: Comparable Classes: Boolean, Class, Date, DateTime, Decimal, Float, Integer, Numeric, Object, String, Time
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
private
The attribute name.
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.
-
#initialize(name, options = EMPTY_HASH) ⇒ undefined
constructor
private
Initialize an Attribute.
-
#rename(new_name) ⇒ Attribute
Rename an attribute.
-
#required? ⇒ Boolean
Test if the attribute is required.
-
#type ⇒ Class<Attribute>
Return the type returned from #call.
-
#valid_primitive?(value) ⇒ Boolean
Test if a value is the correct primitive type.
-
#valid_value?(value) ⇒ Boolean
Test if the value matches the attribute constraints.
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 @options = freeze_object() @required = @options.fetch(:required, true) 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
16 17 18 |
# File 'lib/axiom/attribute.rb', line 16 def name @name 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
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/axiom/attribute.rb', line 34 def self.coerce(object) if object.kind_of?(Attribute) object else name, type, = object klass = equal?(Attribute) ? Object : self klass = const_get(type.name) if type 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
68 69 70 71 72 73 74 75 76 |
# File 'lib/axiom/attribute.rb', line 68 def self.infer_type(operand) case operand when Attribute, Function, Aggregate then operand.type when FalseClass then Boolean else type = operand.class descendants.detect { |descendant| type <= descendant.primitive } 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
53 54 55 56 57 58 59 |
# File 'lib/axiom/attribute.rb', line 53 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.call(self) end |
#rename(new_name) ⇒ Attribute
Make this have the same API as functions
Rename an attribute
124 125 126 |
# File 'lib/axiom/attribute.rb', line 124 def rename(new_name) name.equal?(new_name) ? self : self.class.new(new_name, ) end |
#required? ⇒ Boolean
Test if the attribute is required
145 146 147 |
# File 'lib/axiom/attribute.rb', line 145 def required? @required end |
#type ⇒ Class<Attribute>
Return the type returned from #call
133 134 135 |
# File 'lib/axiom/attribute.rb', line 133 def type self.class end |
#valid_primitive?(value) ⇒ Boolean
Test if a value is the correct primitive type
160 161 162 |
# File 'lib/axiom/attribute.rb', line 160 def valid_primitive?(value) value.kind_of?(self.class.primitive) end |
#valid_value?(value) ⇒ Boolean
Test if the value matches the attribute constraints
175 176 177 |
# File 'lib/axiom/attribute.rb', line 175 def valid_value?(value) valid_or_optional?(value) { valid_primitive?(value) } end |