Class: Stannum::Attribute
- Inherits:
-
Object
- Object
- Stannum::Attribute
- Includes:
- Support::Optional
- Defined in:
- lib/stannum/attribute.rb
Overview
Data object representing an attribute on an entity.
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the attribute.
-
#options ⇒ Hash
readonly
The attribute options.
-
#type ⇒ String
readonly
The name of the attribute type Class or Module.
Instance Method Summary collapse
-
#association_name ⇒ String
The name of the association if the attribute is a foreign key; otherwise false.
-
#default ⇒ Object
The default value for the attribute, if any.
-
#default? ⇒ Boolean
True if the attribute has a default value; otherwise false.
-
#default_value_for(context) ⇒ Object
The value of the default attribute for the given context object, if any.
-
#foreign_key? ⇒ Boolean
True if the attribute represents the foreign key for an association; otherwise false.
-
#initialize(name:, options:, type:) ⇒ Attribute
constructor
A new instance of Attribute.
-
#primary_key? ⇒ Boolean
True if the attribute represents the primary key for the entity; otherwise false.
-
#reader_name ⇒ Symbol
The name of the reader method for the attribute.
-
#resolved_type ⇒ Module
The type of the attribute.
-
#writer_name ⇒ Symbol
The name of the writer method for the attribute.
Methods included from Support::Optional
#optional?, #required?, resolve
Constructor Details
#initialize(name:, options:, type:) ⇒ Attribute
Returns a new instance of Attribute.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stannum/attribute.rb', line 72 def initialize(name:, options:, type:) validate_name(name) () validate_type(type) @name = name.to_s @options = tools.hash_tools.convert_keys_to_symbols( || {}) @options = resolve_required_option(**@options) @type, @resolved_type = resolve_type(type) end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns the name of the attribute.
85 86 87 |
# File 'lib/stannum/attribute.rb', line 85 def name @name end |
#options ⇒ Hash (readonly)
Returns the attribute options.
88 89 90 |
# File 'lib/stannum/attribute.rb', line 88 def @options end |
#type ⇒ String (readonly)
Returns the name of the attribute type Class or Module.
91 92 93 |
# File 'lib/stannum/attribute.rb', line 91 def type @type end |
Instance Method Details
#association_name ⇒ String
Returns the name of the association if the attribute is a foreign key; otherwise false.
95 96 97 |
# File 'lib/stannum/attribute.rb', line 95 def association_name @options[:association_name] end |
#default ⇒ Object
Returns the default value for the attribute, if any.
100 101 102 |
# File 'lib/stannum/attribute.rb', line 100 def default @options[:default] end |
#default? ⇒ Boolean
Returns true if the attribute has a default value; otherwise false.
106 107 108 |
# File 'lib/stannum/attribute.rb', line 106 def default? !@options[:default].nil? end |
#default_value_for(context) ⇒ Object
Returns the value of the default attribute for the given context object, if any.
115 116 117 118 119 |
# File 'lib/stannum/attribute.rb', line 115 def default_value_for(context) return default unless default.is_a?(Proc) default.arity.zero? ? default.call : default.call(context) end |
#foreign_key? ⇒ Boolean
Returns true if the attribute represents the foreign key for an association; otherwise false.
123 124 125 |
# File 'lib/stannum/attribute.rb', line 123 def foreign_key? !!@options[:foreign_key] end |
#primary_key? ⇒ Boolean
Returns true if the attribute represents the primary key for the entity; otherwise false.
129 130 131 |
# File 'lib/stannum/attribute.rb', line 129 def primary_key? !!@options[:primary_key] end |
#reader_name ⇒ Symbol
Returns the name of the reader method for the attribute.
134 135 136 |
# File 'lib/stannum/attribute.rb', line 134 def reader_name @reader_name ||= name.intern end |
#resolved_type ⇒ Module
Returns the type of the attribute.
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/stannum/attribute.rb', line 139 def resolved_type return @resolved_type if @resolved_type @resolved_type = Object.const_get(type) unless @resolved_type.is_a?(Module) raise NameError, "constant #{type} is not a Class or Module" end @resolved_type end |
#writer_name ⇒ Symbol
Returns the name of the writer method for the attribute.
152 153 154 |
# File 'lib/stannum/attribute.rb', line 152 def writer_name @writer_name ||= :"#{name}=" end |