Class: Cattri::AttributeOptions
- Inherits:
-
Object
- Object
- Cattri::AttributeOptions
- Defined in:
- lib/cattri/attribute_options.rb
Overview
AttributeOptions encapsulates normalized metadata for a single Cattri-defined attribute.
It validates, transforms, and freezes all input during initialization, ensuring attribute safety and immutability at runtime.
Constant Summary collapse
- VISIBILITIES =
Valid method visibility levels.
%i[public protected private].freeze
- EXPOSE_OPTIONS =
Valid expose options for method generation.
%i[read write read_write none].freeze
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#expose ⇒ Object
readonly
Returns the value of attribute expose.
-
#final ⇒ Object
readonly
Returns the value of attribute final.
-
#ivar ⇒ Object
readonly
Returns the value of attribute ivar.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#predicate ⇒ Object
readonly
Returns the value of attribute predicate.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#transformer ⇒ Object
readonly
Returns the value of attribute transformer.
-
#visibility ⇒ Object
readonly
Returns the value of attribute visibility.
Class Method Summary collapse
-
.validate_expose!(expose) ⇒ Symbol
Validates and normalizes the ‘expose` configuration.
-
.validate_visibility!(visibility) ⇒ Symbol
Validates and normalizes method visibility.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Allows hash-style access to the option set.
-
#initialize(name, ivar: nil, final: false, scope: :instance, predicate: false, default: nil, transformer: nil, expose: :read_write, visibility: :public) ⇒ AttributeOptions
constructor
Initializes a frozen attribute configuration.
-
#to_h ⇒ Hash<Symbol, Object>
Returns a frozen hash representation of this option set.
Constructor Details
#initialize(name, ivar: nil, final: false, scope: :instance, predicate: false, default: nil, transformer: nil, expose: :read_write, visibility: :public) ⇒ AttributeOptions
Initializes a frozen attribute configuration.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cattri/attribute_options.rb', line 73 def initialize( name, ivar: nil, final: false, scope: :instance, predicate: false, default: nil, transformer: nil, expose: :read_write, visibility: :public ) @name = name.to_sym @ivar = normalize_ivar(ivar) @final = final @scope = validate_scope!(scope) @predicate = predicate @default = normalize_default(default) @transformer = normalize_transformer(transformer) @expose = self.class.validate_expose!(expose) @visibility = self.class.validate_visibility!(visibility) freeze end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def default @default end |
#expose ⇒ Object (readonly)
Returns the value of attribute expose.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def expose @expose end |
#final ⇒ Object (readonly)
Returns the value of attribute final.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def final @final end |
#ivar ⇒ Object (readonly)
Returns the value of attribute ivar.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def ivar @ivar end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def name @name end |
#predicate ⇒ Object (readonly)
Returns the value of attribute predicate.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def predicate @predicate end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def scope @scope end |
#transformer ⇒ Object (readonly)
Returns the value of attribute transformer.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def transformer @transformer end |
#visibility ⇒ Object (readonly)
Returns the value of attribute visibility.
59 60 61 |
# File 'lib/cattri/attribute_options.rb', line 59 def visibility @visibility end |
Class Method Details
.validate_expose!(expose) ⇒ Symbol
Validates and normalizes the ‘expose` configuration.
25 26 27 28 29 30 |
# File 'lib/cattri/attribute_options.rb', line 25 def validate_expose!(expose) expose = expose.to_sym return expose if EXPOSE_OPTIONS.include?(expose) # steep:ignore raise Cattri::AttributeError, "Invalid expose option `#{expose.inspect}` for :#{name}" end |
.validate_visibility!(visibility) ⇒ Symbol
Validates and normalizes method visibility.
37 38 39 40 41 42 |
# File 'lib/cattri/attribute_options.rb', line 37 def validate_visibility!(visibility) visibility = visibility.to_sym return visibility if VISIBILITIES.include?(visibility) # steep:ignore raise Cattri::AttributeError, "Invalid visibility `#{visibility.inspect}` for :#{name}" end |
Instance Method Details
#[](key) ⇒ Object
Allows hash-style access to the option set.
120 121 122 |
# File 'lib/cattri/attribute_options.rb', line 120 def [](key) to_h[key.to_sym] end |
#to_h ⇒ Hash<Symbol, Object>
Returns a frozen hash representation of this option set.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cattri/attribute_options.rb', line 100 def to_h hash = { name: @name, ivar: @ivar, final: @final, scope: @scope, predicate: @predicate, default: @default, transformer: @transformer, expose: @expose, visibility: @visibility } hash.freeze hash end |