Class: Domainic::Command::Context::Attribute
- Inherits:
-
Object
- Object
- Domainic::Command::Context::Attribute
- Defined in:
- lib/domainic/command/context/attribute.rb
Overview
Represents an attribute within a command context. This class manages the lifecycle of an attribute, including its validation, default values, and metadata such as descriptions
The Attribute class supports a variety of configuration options, such as marking an attribute as required,
defining static or dynamic default values, and specifying custom validators. These features ensure that
attributes conform to expected rules and provide useful metadata for documentation or runtime behavior
Instance Attribute Summary collapse
-
#description ⇒ String?
readonly
The textual description of the attribute, providing metadata about its purpose or usage.
-
#name ⇒ Symbol
readonly
The name of the attribute, uniquely identifying it within a command context.
Instance Method Summary collapse
-
#default ⇒ Object?
Retrieves the default value of the attribute.
-
#default? ⇒ Boolean
Determines whether the attribute has a default value defined.
-
#initialize ⇒ Attribute
constructor
Create a new attribute instance.
-
#required? ⇒ Boolean
Determines whether the attribute is marked as required.
-
#valid?(value) ⇒ Boolean
Validates the given value against the attribute's type validator.
Constructor Details
#initialize ⇒ Attribute
Create a new attribute instance
70 71 72 73 74 75 76 77 78 |
# File 'lib/domainic/command/context/attribute.rb', line 70 def initialize(name, *type_validator_and_description, **) = .transform_keys(&:to_sym) @name = name.to_sym @required = [:required] == true initialize_default() initialize_description_and_type_validator(type_validator_and_description, ) end |
Instance Attribute Details
#description ⇒ String? (readonly)
The textual description of the attribute, providing metadata about its purpose or usage
32 33 34 |
# File 'lib/domainic/command/context/attribute.rb', line 32 def description @description end |
#name ⇒ Symbol (readonly)
The name of the attribute, uniquely identifying it within a command context
37 38 39 |
# File 'lib/domainic/command/context/attribute.rb', line 37 def name @name end |
Instance Method Details
#default ⇒ Object?
Retrieves the default value of the attribute. If a default generator is specified, it evaluates the generator and returns the result
85 86 87 88 89 |
# File 'lib/domainic/command/context/attribute.rb', line 85 def default return unless default? @default.is_a?(Proc) ? @default.call : @default end |
#default? ⇒ Boolean
Determines whether the attribute has a default value defined
95 96 97 |
# File 'lib/domainic/command/context/attribute.rb', line 95 def default? @default != UNDEFINED_DEFAULT end |
#required? ⇒ Boolean
Determines whether the attribute is marked as required
103 104 105 |
# File 'lib/domainic/command/context/attribute.rb', line 103 def required? @required end |
#valid?(value) ⇒ Boolean
Validates the given value against the attribute's type validator
113 114 115 116 117 118 119 120 121 |
# File 'lib/domainic/command/context/attribute.rb', line 113 def valid?(value) return false if value.nil? && required? return true if @type_validator.nil? validator = @type_validator return validator.call(value) if validator.is_a?(Proc) validator === value || value.is_a?(validator) # rubocop:disable Style/CaseEquality end |