Class: CMDx::Input
- Inherits:
-
Object
- Object
- CMDx::Input
- Defined in:
- lib/cmdx/input.rb
Overview
A single declared task input. Holds declaration options (:source,
:default, :required, :coerce, validators, :transform, etc.) and
owns the resolution pipeline that produces the value the task will read
through the generated accessor.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#accessor_name ⇒ Symbol
Computed accessor/reader method name.
- #as ⇒ Symbol?
-
#as_json ⇒ Hash{Symbol => Object}
JSON-friendly hash view.
- #condition_if ⇒ Symbol, ...
- #condition_unless ⇒ Symbol, ...
- #default ⇒ Object, ...
- #description ⇒ String?
-
#initialize(name, children: EMPTY_ARRAY, **options) ⇒ Input
constructor
A new instance of Input.
-
#ivar_name ⇒ Symbol
Backing ivar used by the generated reader method.
- #prefix ⇒ Boolean, ...
- #required ⇒ Boolean
-
#required?(task = nil) ⇒ Boolean
Evaluates required-ness against
task, respecting:if/:unless. -
#resolve(task) ⇒ Object?
Fetches + coerces + transforms + validates the value from its configured
:sourceontask. -
#resolve_from_parent(parent_value, task) ⇒ Object?
Same as #resolve but fetches the value from
parent_value(used for nested child inputs) instead of the declared:source. - #source ⇒ Symbol, ...
- #suffix ⇒ Boolean, ...
-
#to_h ⇒ Hash{Symbol => Object}
Serialized schema used by
inputs_schema. -
#to_json(*args) ⇒ String
Serializes the input schema to a JSON string.
- #transform ⇒ Symbol, ...
Constructor Details
#initialize(name, children: EMPTY_ARRAY, **options) ⇒ Input
Returns a new instance of Input.
25 26 27 28 29 |
# File 'lib/cmdx/input.rb', line 25 def initialize(name, children: EMPTY_ARRAY, **) @name = name.to_sym @children = children.freeze @options = .freeze end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
10 11 12 |
# File 'lib/cmdx/input.rb', line 10 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/cmdx/input.rb', line 10 def name @name end |
Instance Method Details
#accessor_name ⇒ Symbol
Computed accessor/reader method name. Uses :as when provided,
otherwise combines :prefix, name, and :suffix around the source.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cmdx/input.rb', line 85 def accessor_name return as if as @accessor_name ||= begin prefix_str = case prefix when true "#{source}_" when ::String prefix end suffix_str = case suffix when true "_#{source}" when ::String suffix end :"#{prefix_str}#{name}#{suffix_str}" end end |
#as ⇒ Symbol?
37 38 39 |
# File 'lib/cmdx/input.rb', line 37 def as @options[:as] end |
#as_json ⇒ Hash{Symbol => Object}
JSON-friendly hash view. Aliases #to_h for conventional as_json
callers (e.g. Rails).
164 165 166 |
# File 'lib/cmdx/input.rb', line 164 def as_json(*) to_h end |
#condition_if ⇒ Symbol, ...
67 68 69 |
# File 'lib/cmdx/input.rb', line 67 def condition_if @options[:if] end |
#condition_unless ⇒ Symbol, ...
72 73 74 |
# File 'lib/cmdx/input.rb', line 72 def condition_unless @options[:unless] end |
#default ⇒ Object, ...
57 58 59 |
# File 'lib/cmdx/input.rb', line 57 def default @options[:default] end |
#description ⇒ String?
32 33 34 |
# File 'lib/cmdx/input.rb', line 32 def description @options[:description] || @options[:desc] end |
#ivar_name ⇒ Symbol
Returns backing ivar used by the generated reader method.
109 110 111 |
# File 'lib/cmdx/input.rb', line 109 def ivar_name @ivar_name ||= :"@_input_#{accessor_name}" end |
#prefix ⇒ Boolean, ...
42 43 44 |
# File 'lib/cmdx/input.rb', line 42 def prefix @options[:prefix] end |
#required ⇒ Boolean
77 78 79 |
# File 'lib/cmdx/input.rb', line 77 def required @options.fetch(:required, false) end |
#required?(task = nil) ⇒ Boolean
Evaluates required-ness against task, respecting :if/:unless.
When called without a task, returns the static :required flag.
118 119 120 121 122 123 124 |
# File 'lib/cmdx/input.rb', line 118 def required?(task = nil) return false unless required return true if task.nil? return false unless Util.satisfied?(condition_if, condition_unless, task) true end |
#resolve(task) ⇒ Object?
Fetches + coerces + transforms + validates the value from its
configured :source on task. Missing-but-required inputs add a
validation error to task.errors. Returns nil when coercion or any
validator fails (the failure message is recorded on task.errors).
133 134 135 136 |
# File 'lib/cmdx/input.rb', line 133 def resolve(task) value, key_provided = resolve_with_key(task) run_pipeline(value, key_provided, task) end |
#resolve_from_parent(parent_value, task) ⇒ Object?
Same as #resolve but fetches the value from parent_value (used for
nested child inputs) instead of the declared :source.
144 145 146 147 |
# File 'lib/cmdx/input.rb', line 144 def resolve_from_parent(parent_value, task) value, key_provided = resolve_from_parent_with_key(parent_value) run_pipeline(value, key_provided, task) end |
#source ⇒ Symbol, ...
52 53 54 |
# File 'lib/cmdx/input.rb', line 52 def source @options[:source] || :context end |
#suffix ⇒ Boolean, ...
47 48 49 |
# File 'lib/cmdx/input.rb', line 47 def suffix @options[:suffix] end |
#to_h ⇒ Hash{Symbol => Object}
Returns serialized schema used by inputs_schema.
150 151 152 153 154 155 156 157 158 |
# File 'lib/cmdx/input.rb', line 150 def to_h { name: accessor_name, description:, required: required?, options: @options, children: children.map(&:to_h) } end |
#to_json(*args) ⇒ String
Serializes the input schema to a JSON string. Non-primitive entries in
:options (Procs, arbitrary callables) emit via their stdlib to_json
defaults.
174 175 176 |
# File 'lib/cmdx/input.rb', line 174 def to_json(*args) to_h.to_json(*args) end |
#transform ⇒ Symbol, ...
62 63 64 |
# File 'lib/cmdx/input.rb', line 62 def transform @options[:transform] end |