Module: Castkit::Attributes::Options
- Included in:
- Definition
- Defined in:
- lib/castkit/attributes/options.rb
Overview
Provides a DSL for configuring attribute options within an attribute definition.
This module is designed to be extended by class-level definition objects such as ‘Castkit::Attributes::Definition`, and is used to build reusable sets of options for attributes declared within `Castkit::DataObject` classes.
Constant Summary collapse
- ACCESS_MODES =
Valid access modes for an attribute.
%i[read write].freeze
- DEFAULTS =
Default configuration for attribute options.
{ required: true, ignore_nil: false, ignore_blank: false, ignore: false, composite: false, transient: false, unwrapped: false, prefix: nil, access: ACCESS_MODES, force_type: !Castkit.configuration.enforce_typing }.freeze
Instance Method Summary collapse
-
#access(value = nil) ⇒ void
Sets access modes for the attribute.
-
#composite(value = nil) ⇒ void
Marks the attribute as a composite (e.g., nested ‘DataObject`).
-
#default(value = nil) ⇒ void
Sets the default value or proc for the attribute.
-
#force_type(value = nil) ⇒ void
Enables or disables forced typecasting, or sets a custom flag.
-
#format(value) ⇒ void
Sets a format constraint (e.g., regex validation).
-
#ignore(value = nil) ⇒ void
Marks the attribute to be ignored entirely.
-
#ignore_blank(value = nil) ⇒ void
Ignores blank values (‘“”`, `[]`, `{}`) during serialization.
-
#ignore_nil(value = nil) ⇒ void
Ignores ‘nil` values during serialization or persistence.
-
#of(value) ⇒ void
Sets the element type for array attributes.
-
#prefix(value = nil) ⇒ void
Adds a prefix for unwrapped attribute keys.
-
#readonly(value = nil) ⇒ void
Shortcut to make the attribute readonly (‘access: [:read]`).
-
#required(value = nil) ⇒ void
Marks the attribute as required or optional.
-
#transient(value = nil) ⇒ void
Marks the attribute as transient (not included in persistence or serialization).
-
#type(value = nil) ⇒ Symbol
Sets or retrieves the attribute type.
-
#unwrapped(value = nil) ⇒ void
Marks the attribute as unwrapped (inline merging of nested fields).
-
#validator(value) ⇒ void
Attaches a custom validator callable for this attribute.
Instance Method Details
#access(value = nil) ⇒ void
This method returns an undefined value.
Sets access modes for the attribute.
125 126 127 128 |
# File 'lib/castkit/attributes/options.rb', line 125 def access(value = nil) value = validate_access_modes!(value) set_option(:access, value) end |
#composite(value = nil) ⇒ void
This method returns an undefined value.
Marks the attribute as a composite (e.g., nested ‘DataObject`).
143 144 145 |
# File 'lib/castkit/attributes/options.rb', line 143 def composite(value = nil) set_option(:composite, value || true) end |
#default(value = nil) ⇒ void
This method returns an undefined value.
Sets the default value or proc for the attribute.
61 62 63 |
# File 'lib/castkit/attributes/options.rb', line 61 def default(value = nil) set_option(:default, value) end |
#force_type(value = nil) ⇒ void
This method returns an undefined value.
Enables or disables forced typecasting, or sets a custom flag.
69 70 71 |
# File 'lib/castkit/attributes/options.rb', line 69 def force_type(value = nil) set_option(:force_type, value || !Castkit.configuration.enforce_typing) end |
#format(value) ⇒ void
This method returns an undefined value.
Sets a format constraint (e.g., regex validation).
159 160 161 |
# File 'lib/castkit/attributes/options.rb', line 159 def format(value) set_option(:format, value) end |
#ignore(value = nil) ⇒ void
This method returns an undefined value.
Marks the attribute to be ignored entirely.
85 86 87 |
# File 'lib/castkit/attributes/options.rb', line 85 def ignore(value = nil) set_option(:ignore, value || true) end |
#ignore_blank(value = nil) ⇒ void
This method returns an undefined value.
Ignores blank values (‘“”`, `[]`, `{}`) during serialization.
101 102 103 |
# File 'lib/castkit/attributes/options.rb', line 101 def ignore_blank(value = nil) set_option(:ignore_blank, value || true) end |
#ignore_nil(value = nil) ⇒ void
This method returns an undefined value.
Ignores ‘nil` values during serialization or persistence.
93 94 95 |
# File 'lib/castkit/attributes/options.rb', line 93 def ignore_nil(value = nil) set_option(:ignore_nil, value || true) end |
#of(value) ⇒ void
This method returns an undefined value.
Sets the element type for array attributes.
51 52 53 54 55 |
# File 'lib/castkit/attributes/options.rb', line 51 def of(value) return unless @type == :array set_option(:of, value) end |
#prefix(value = nil) ⇒ void
This method returns an undefined value.
Adds a prefix for unwrapped attribute keys.
109 110 111 |
# File 'lib/castkit/attributes/options.rb', line 109 def prefix(value = nil) set_option(:prefix, value) end |
#readonly(value = nil) ⇒ void
This method returns an undefined value.
Shortcut to make the attribute readonly (‘access: [:read]`).
134 135 136 137 |
# File 'lib/castkit/attributes/options.rb', line 134 def readonly(value = nil) value = value || true ? [:read] : ACCESS_MODES set_option(:access, value) end |
#required(value = nil) ⇒ void
This method returns an undefined value.
Marks the attribute as required or optional.
77 78 79 |
# File 'lib/castkit/attributes/options.rb', line 77 def required(value = nil) set_option(:required, value || true) end |
#transient(value = nil) ⇒ void
This method returns an undefined value.
Marks the attribute as transient (not included in persistence or serialization).
151 152 153 |
# File 'lib/castkit/attributes/options.rb', line 151 def transient(value = nil) set_option(:transient, value || true) end |
#type(value = nil) ⇒ Symbol
Sets or retrieves the attribute type.
43 44 45 |
# File 'lib/castkit/attributes/options.rb', line 43 def type(value = nil) value.nil? ? definition[:type] : (definition[:type] = value.to_sym) end |
#unwrapped(value = nil) ⇒ void
This method returns an undefined value.
Marks the attribute as unwrapped (inline merging of nested fields).
117 118 119 |
# File 'lib/castkit/attributes/options.rb', line 117 def unwrapped(value = nil) set_option(:unwrapped, value || true) end |
#validator(value) ⇒ void
This method returns an undefined value.
Attaches a custom validator callable for this attribute.
167 168 169 |
# File 'lib/castkit/attributes/options.rb', line 167 def validator(value) set_option(:validator, value) end |