Class: CMDx::Attribute
- Inherits:
-
Object
- Object
- CMDx::Attribute
- Defined in:
- lib/cmdx/attribute.rb
Overview
Represents a configurable attribute within a CMDx task. Attributes define the data structure and validation rules for task parameters. They can be nested to create complex hierarchical data structures.
Instance Attribute Summary collapse
-
#children ⇒ Array<Attribute>
readonly
Returns the child attributes for nested structures.
-
#name ⇒ Symbol
readonly
Returns the name of this attribute.
-
#options ⇒ Hash{Symbol => Object}
readonly
Returns the configuration options for this attribute.
-
#parent ⇒ Attribute?
readonly
Returns the parent attribute if this is a nested attribute.
-
#task ⇒ CMDx::Task
Returns the task instance associated with this attribute.
-
#types ⇒ Array<Class>
readonly
Returns the expected type(s) for this attribute’s value.
Class Method Summary collapse
-
.build(*names, **options) {|self| ... } ⇒ Array<Attribute>
Builds multiple attributes with the same configuration.
-
.optional(*names, **options) {|self| ... } ⇒ Array<Attribute>
Creates optional attributes (not required).
-
.required(*names, **options) {|self| ... } ⇒ Array<Attribute>
Creates required attributes.
Instance Method Summary collapse
-
#define_and_verify_tree ⇒ Object
Defines and verifies the entire attribute tree including nested children.
-
#initialize(name, options = {}) {|self| ... } ⇒ Attribute
constructor
Creates a new attribute with the specified name and configuration.
-
#method_name ⇒ Symbol
Generates the method name for accessing this attribute.
-
#required? ⇒ Boolean
Checks if the attribute is required.
-
#source ⇒ Symbol
Determines the source of the attribute value.
Constructor Details
#initialize(name, options = {}) {|self| ... } ⇒ Attribute
Creates a new attribute with the specified name and configuration.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cmdx/attribute.rb', line 97 def initialize(name, = {}, &) @parent = .delete(:parent) @required = .delete(:required) || false @types = Array(.delete(:types) || .delete(:type)) @name = name.to_sym @options = @children = [] instance_eval(&) if block_given? end |
Instance Attribute Details
#children ⇒ Array<Attribute> (readonly)
Returns the child attributes for nested structures.
53 54 55 |
# File 'lib/cmdx/attribute.rb', line 53 def children @children end |
#name ⇒ Symbol (readonly)
Returns the name of this attribute.
33 34 35 |
# File 'lib/cmdx/attribute.rb', line 33 def name @name end |
#options ⇒ Hash{Symbol => Object} (readonly)
Returns the configuration options for this attribute.
43 44 45 |
# File 'lib/cmdx/attribute.rb', line 43 def @options end |
#parent ⇒ Attribute? (readonly)
Returns the parent attribute if this is a nested attribute.
63 64 65 |
# File 'lib/cmdx/attribute.rb', line 63 def parent @parent end |
#task ⇒ CMDx::Task
Returns the task instance associated with this attribute.
23 24 25 |
# File 'lib/cmdx/attribute.rb', line 23 def task @task end |
#types ⇒ Array<Class> (readonly)
Returns the expected type(s) for this attribute’s value.
73 74 75 |
# File 'lib/cmdx/attribute.rb', line 73 def types @types end |
Class Method Details
.build(*names, **options) {|self| ... } ⇒ Array<Attribute>
Builds multiple attributes with the same configuration.
127 128 129 130 131 132 133 134 135 |
# File 'lib/cmdx/attribute.rb', line 127 def build(*names, **, &) if names.none? raise ArgumentError, "no attributes given" elsif (names.size > 1) && .key?(:as) raise ArgumentError, "the :as option only supports one attribute per definition" end names.filter_map { |name| new(name, **, &) } end |
.optional(*names, **options) {|self| ... } ⇒ Array<Attribute>
Creates optional attributes (not required).
151 152 153 |
# File 'lib/cmdx/attribute.rb', line 151 def optional(*names, **, &) build(*names, **.merge(required: false), &) end |
.required(*names, **options) {|self| ... } ⇒ Array<Attribute>
Creates required attributes.
169 170 171 |
# File 'lib/cmdx/attribute.rb', line 169 def required(*names, **, &) build(*names, **.merge(required: true), &) end |
Instance Method Details
#define_and_verify_tree ⇒ Object
Defines and verifies the entire attribute tree including nested children.
229 230 231 232 233 234 235 236 |
# File 'lib/cmdx/attribute.rb', line 229 def define_and_verify_tree define_and_verify children.each do |child| child.task = task child.define_and_verify_tree end end |
#method_name ⇒ Symbol
Generates the method name for accessing this attribute.
217 218 219 220 221 222 223 224 |
# File 'lib/cmdx/attribute.rb', line 217 def method_name @method_name ||= [:as] || begin prefix = AFFIX.call([:prefix]) { "#{source}_" } suffix = AFFIX.call([:suffix]) { "_#{source}" } :"#{prefix}#{name}#{suffix}" end end |
#required? ⇒ Boolean
Checks if the attribute is required.
183 184 185 |
# File 'lib/cmdx/attribute.rb', line 183 def required? !!@required end |
#source ⇒ Symbol
Determines the source of the attribute value.
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/cmdx/attribute.rb', line 195 def source @source ||= parent&.method_name || begin value = [:source] if value.is_a?(Proc) task.instance_eval(&value) elsif value.respond_to?(:call) value.call(task) else value || :context end end end |