Class: Domainic::Attributer::DSL::AttributeBuilder
- Inherits:
-
Object
- Object
- Domainic::Attributer::DSL::AttributeBuilder
- Defined in:
- lib/domainic/attributer/dsl/attribute_builder.rb,
lib/domainic/attributer/dsl/attribute_builder/option_parser.rb
Overview
This class provides a rich DSL for configuring attributes with support for default values, coercion, validation, visibility controls, and change tracking. It uses method chaining to allow natural, declarative attribute definitions
Instance Method Summary collapse
-
#coerce_with(proc_symbol = Undefined) {|value| ... } ⇒ self
(also: #coerce)
Provides a way to automatically transform attribute values into the desired format or type.
-
#default(value_or_proc = Undefined) { ... } ⇒ self
(also: #default_generator, #default_value)
Provides a way to assign default values to attributes.
-
#description(text) ⇒ self
(also: #desc)
Provides a way to add descriptive metadata to attributes.
-
#initialize(base, attribute_name, attribute_type, type_validator = Undefined, **options, &block) ⇒ AttributeBuilder
constructor
Initialize a new AttributeBuilder.
-
#non_nilable ⇒ self
(also: #non_nil, #non_null, #non_nullable, #not_nil, #not_nilable, #not_null, #not_nullable)
Ensures that an attribute defined in the block DSL cannot have a
nil
value. -
#on_change(proc = Undefined) {|old_value, new_value| ... } ⇒ self
Allows defining a callback to be triggered whenever the attribute's value changes.
-
#private ⇒ self
Sets both the read and write visibility of an attribute to private.
-
#private_read ⇒ self
Sets the read visibility of an attribute to private, allowing the attribute to be read only within the class itself.
-
#private_write ⇒ self
Sets the write visibility of an attribute to private, allowing the attribute to be modified only within the class.
-
#protected ⇒ self
Sets both the read and write visibility of an attribute to protected, allowing access only within the class and its subclasses.
-
#protected_read ⇒ self
Sets both the read and write visibility of an attribute to protected.
-
#protected_write ⇒ self
Sets both the read and write visibility of an attribute to protected.
-
#public ⇒ self
Explicitly sets both the read and write visibility of an attribute to public, overriding any inherited or previously set visibility.
-
#public_read ⇒ self
Explicitly sets the read visibility of an attribute to public, overriding any inherited or previously set visibility.
-
#public_write ⇒ self
Explicitly sets the write visibility of an attribute to public, overriding any inherited or previously set visibility.
-
#required ⇒ self
Marks an option attribute as required, ensuring that a value must be provided during initialization.
-
#validate_with(object_or_proc = Undefined) {|value| ... } ⇒ self
(also: #validate, #validates)
Adds a custom validation to an attribute, allowing you to define specific criteria that the attribute's value must meet.
Constructor Details
#initialize(base, attribute_name, attribute_type, type_validator = Undefined, **options, &block) ⇒ AttributeBuilder
Initialize a new AttributeBuilder
37 38 39 40 41 42 43 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 37 def initialize(base, attribute_name, attribute_type, type_validator = Undefined, **, &block) @base = base # @type var options: OptionParser::options @options = OptionParser.parse!(attribute_name, attribute_type, ) @options[:validators] << type_validator if type_validator != Undefined instance_exec(&block) if block end |
Instance Method Details
#coerce_with(proc_symbol = Undefined) {|value| ... } ⇒ self Also known as: coerce
When coercion is used with nilable attributes, handlers should account for nil
values appropriately.
Provides a way to automatically transform attribute values into the desired format or type. Coercion ensures input values conform to the expected structure by applying one or more handlers. Handlers can be Procs, lambdas, or method symbols.
Coercions are applied during initialization or whenever the attribute value is updated.
119 120 121 122 123 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 119 def coerce_with(proc_symbol = Undefined, &block) handler = proc_symbol == Undefined ? block : proc_symbol #: Attribute::Coercer::handler @options[:coercers] << handler self end |
#default(value_or_proc = Undefined) { ... } ⇒ self Also known as: default_generator, default_value
Provides a way to assign default values to attributes. These values can be static or dynamically generated using a block. The default value is only applied when no explicit value is provided for the attribute
185 186 187 188 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 185 def default(value_or_proc = Undefined, &block) @options[:default] = value_or_proc == Undefined ? block : value_or_proc self end |
#description(text) ⇒ self Also known as: desc
Descriptions are optional but highly recommended for improving readability and maintainability of the code.
Provides a way to add descriptive metadata to attributes. Descriptions improve code clarity by documenting the purpose or behavior of an attribute. These descriptions can be short or detailed, depending on the context.
224 225 226 227 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 224 def description(text) @options[:description] = text self end |
#non_nilable ⇒ self Also known as: non_nil, non_null, non_nullable, not_nil, not_nilable, not_null, not_nullable
Ensures that an attribute defined in the block DSL cannot have a nil
value. This validation is enforced
during initialization and when modifying the attribute value at runtime. Use non_nilable
for attributes that
must always have a value.
260 261 262 263 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 260 def non_nilable @options[:nilable] = false self end |
#on_change(proc = Undefined) {|old_value, new_value| ... } ⇒ self
Allows defining a callback to be triggered whenever the attribute's value changes. The callback receives the
old value and the new value as arguments, enabling custom logic to be executed on changes. Use on_change
to
react to changes in attribute values, such as updating dependent attributes or triggering side effects.
332 333 334 335 336 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 332 def on_change(proc = Undefined, &block) handler = proc == Undefined ? block : proc #: Attribute::Callback::handler @options[:callbacks] << handler self end |
#private ⇒ self
Sets both the read and write visibility of an attribute to private. This ensures the attribute can only be accessed or modified within the class itself.
357 358 359 360 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 357 def private private_read private_write end |
#private_read ⇒ self
Sets the read visibility of an attribute to private, allowing the attribute to be read only within the class
itself. The write visibility remains unchanged unless explicitly modified. Use private_read
when the value
of an attribute should be hidden from external consumers but writable by external code if needed.
389 390 391 392 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 389 def private_read @options[:read] = :private self end |
#private_write ⇒ self
Sets the write visibility of an attribute to private, allowing the attribute to be modified only within the
class. The read visibility remains unchanged unless explicitly modified. Use private_write
to ensure that an
attribute's value can only be updated internally, while still allowing external code to read its value if
needed.
422 423 424 425 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 422 def private_write @options[:write] = :private self end |
#protected ⇒ self
Sets both the read and write visibility of an attribute to protected, allowing access only within the class
and its subclasses. This visibility restricts external access entirely. Use protected
to share attributes
within a class hierarchy while keeping them hidden from external consumers.
453 454 455 456 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 453 def protected protected_read protected_write end |
#protected_read ⇒ self
Sets both the read and write visibility of an attribute to protected. This allows the attribute to be accessed
or modified only within the class and its subclasses. Use protected
for attributes that should be accessible
to the class and its subclasses but hidden from external consumers.
484 485 486 487 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 484 def protected_read @options[:read] = :protected self end |
#protected_write ⇒ self
Sets both the read and write visibility of an attribute to protected. This allows the attribute to be accessed
or modified only within the class and its subclasses. Use protected
for attributes that should be accessible
to the class and its subclasses but hidden from external consumers.
515 516 517 518 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 515 def protected_write @options[:write] = :protected self end |
#public ⇒ self
Attributes are public by default. Use public
explicitly to override inherited or modified visibility.
Explicitly sets both the read and write visibility of an attribute to public, overriding any inherited or previously set visibility. By default, attributes are public, so this is typically used to revert an attribute's visibility if it was changed in a parent class or module.
549 550 551 552 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 549 def public public_read public_write end |
#public_read ⇒ self
Attributes are publicly readable by default. Use public_read
explicitly to override inherited or
modified visibility.
Explicitly sets the read visibility of an attribute to public, overriding any inherited or previously set visibility. By default, attributes are readable publicly, so this is typically used to revert the read visibility of an attribute if it was modified in a parent class or module.
584 585 586 587 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 584 def public_read @options[:read] = :public self end |
#public_write ⇒ self
Attributes are publicly writable by default. Use public_write
explicitly to override inherited or
modified visibility.
Explicitly sets the write visibility of an attribute to public, overriding any inherited or previously set visibility. By default, attributes are writable publicly, so this is typically used to revert the write visibility of an attribute if it was modified in a parent class or module.
619 620 621 622 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 619 def public_write @options[:write] = :public self end |
#required ⇒ self
required options are enforced during initialization; as long as the option is provided
(even if it is nil
) no error will be raised.
Marks an option attribute as required, ensuring that a value must be provided during
initialization. If a required attribute is not supplied, an error is raised. Use required
to enforce
mandatory attributes.
647 648 649 650 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 647 def required @options[:required] = true self end |
#validate_with(object_or_proc = Undefined) {|value| ... } ⇒ self Also known as: validate, validates
Adds a custom validation to an attribute, allowing you to define specific criteria that the attribute's value
must meet. Validators can be Procs, lambdas, or symbols referencing instance methods. Validation occurs during
initialization and whenever the attribute value is updated. Use validate_with
to enforce rules beyond type
or presence, such as ranges, formats, or custom logic.
712 713 714 715 716 |
# File 'lib/domainic/attributer/dsl/attribute_builder.rb', line 712 def validate_with(object_or_proc = Undefined, &block) handler = object_or_proc == Undefined ? block : object_or_proc #: Attribute::Validator::handler @options[:validators] << handler self end |