Class: Bronze::Entities::Attributes::Metadata
- Inherits:
-
Object
- Object
- Bronze::Entities::Attributes::Metadata
- Defined in:
- lib/bronze/entities/attributes/metadata.rb
Overview
Data class that characterizes an entity attribute and allows for reflection on its properties and options.
Instance Attribute Summary collapse
-
#name ⇒ String, Symbol
readonly
The name of the attribute.
-
#options ⇒ Hash
readonly
Additional options for the attribute.
-
#reader_name ⇒ String, Symbol
readonly
The name of the attribute’s reader method.
-
#type ⇒ Class
readonly
The type of the attribute.
-
#writer_name ⇒ String, Symbol
readonly
The name of the attribute’s writer method.
Instance Method Summary collapse
-
#allow_nil? ⇒ Boolean
True if the attribute allows nil values, otherwise false.
-
#default ⇒ Object
(also: #default_value)
The default value for the attribute.
-
#default? ⇒ Boolean
True if the default value is set, otherwise false.
-
#default_transform? ⇒ Boolean
True if the attribute does not have a custom transform, or if the transform is flagged as a default transform; otherwise false.
-
#foreign_key? ⇒ Boolean
True if the attribute is a foreign key, otherwise false.
-
#initialize(name, type, options) ⇒ Metadata
constructor
A new instance of Metadata.
-
#primary_key? ⇒ Boolean
True if the attribute is a primary key, otherwise false.
-
#read_only? ⇒ Boolean
True if the attribute is read-only, otherwise false.
-
#transform ⇒ Bronze::Transform
The transform used to normalize and denormalize the attribute.
-
#transform? ⇒ Boolean
True if the attribute has a custom transform, otherwise false.
Constructor Details
#initialize(name, type, options) ⇒ Metadata
Returns a new instance of Metadata.
12 13 14 15 16 17 18 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 12 def initialize(name, type, ) @name = name.intern @type = type @options = @reader_name = name.intern @writer_name = "#{name}=".intern end |
Instance Attribute Details
#name ⇒ String, Symbol (readonly)
Returns the name of the attribute.
21 22 23 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 21 def name @name end |
#options ⇒ Hash (readonly)
Returns additional options for the attribute.
24 25 26 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 24 def @options end |
#reader_name ⇒ String, Symbol (readonly)
Returns the name of the attribute’s reader method.
27 28 29 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 27 def reader_name @reader_name end |
#type ⇒ Class (readonly)
Returns the type of the attribute.
30 31 32 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 30 def type @type end |
#writer_name ⇒ String, Symbol (readonly)
Returns the name of the attribute’s writer method.
33 34 35 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 33 def writer_name @writer_name end |
Instance Method Details
#allow_nil? ⇒ Boolean
Returns true if the attribute allows nil values, otherwise false.
37 38 39 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 37 def allow_nil? !!@options[:allow_nil] end |
#default ⇒ Object Also known as: default_value
Returns the default value for the attribute.
42 43 44 45 46 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 42 def default val = @options[:default] val.is_a?(Proc) ? val.call : val end |
#default? ⇒ Boolean
Returns true if the default value is set, otherwise false.
50 51 52 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 50 def default? !@options[:default].nil? end |
#default_transform? ⇒ Boolean
Returns true if the attribute does not have a custom transform, or if the transform is flagged as a default transform; otherwise false.
56 57 58 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 56 def default_transform? !!@options[:default_transform] || !transform? end |
#foreign_key? ⇒ Boolean
Returns true if the attribute is a foreign key, otherwise false.
61 62 63 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 61 def foreign_key? !!@options[:foreign_key] end |
#primary_key? ⇒ Boolean
Returns true if the attribute is a primary key, otherwise false.
66 67 68 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 66 def primary_key? !!@options[:primary_key] end |
#read_only? ⇒ Boolean
Returns true if the attribute is read-only, otherwise false.
71 72 73 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 71 def read_only? !!@options[:read_only] end |
#transform ⇒ Bronze::Transform
Returns the transform used to normalize and denormalize the attribute.
77 78 79 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 77 def transform @options[:transform] end |
#transform? ⇒ Boolean
Returns true if the attribute has a custom transform, otherwise false.
83 84 85 |
# File 'lib/bronze/entities/attributes/metadata.rb', line 83 def transform? !!@options[:transform] end |