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.
-
#description ⇒ String
readonly
Returns the description of the attribute.
-
#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.
-
#to_h ⇒ Hash
}.
Constructor Details
#initialize(name, options = {}) {|self| ... } ⇒ Attribute
Creates a new attribute with the specified name and configuration.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cmdx/attribute.rb', line 108 def initialize(name, = {}, &) @parent = .delete(:parent) @required = .delete(:required) || false @types = Array(.delete(:types) || .delete(:type)) @description = .delete(:description) || .delete(:desc) @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 |
#description ⇒ String (readonly)
Returns the description of the attribute.
83 84 85 |
# File 'lib/cmdx/attribute.rb', line 83 def description @description 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.
139 140 141 142 143 144 145 146 147 |
# File 'lib/cmdx/attribute.rb', line 139 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).
163 164 165 |
# File 'lib/cmdx/attribute.rb', line 163 def optional(*names, **, &) build(*names, **.merge(required: false), &) end |
.required(*names, **options) {|self| ... } ⇒ Array<Attribute>
Creates required attributes.
181 182 183 |
# File 'lib/cmdx/attribute.rb', line 181 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.
241 242 243 244 245 246 247 248 |
# File 'lib/cmdx/attribute.rb', line 241 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.
229 230 231 232 233 234 235 236 |
# File 'lib/cmdx/attribute.rb', line 229 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.
195 196 197 |
# File 'lib/cmdx/attribute.rb', line 195 def required? !!@required end |
#source ⇒ Symbol
Determines the source of the attribute value.
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cmdx/attribute.rb', line 207 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 |
#to_h ⇒ Hash
}
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/cmdx/attribute.rb', line 264 def to_h { name: name, method_name: method_name, description: description, required: required?, types: types, options: .except(:if, :unless), children: children.map(&:to_h) } end |