Module: CMDx::Util
Overview
Shared helpers for resolving ‘:if` / `:unless` conditional options across tasks, callbacks, inputs, outputs, validators, and deprecations. Normalizes booleans, symbols (method names), procs, and call-ables into a truth value.
Instance Method Summary collapse
-
#evaluate(condition, receiver, *args) ⇒ Boolean, Object
Evaluates a condition against ‘receiver`, dispatching by type.
-
#if?(condition, receiver, *args) ⇒ Boolean
Evaluates an ‘:if`-style condition.
-
#satisfied?(condition_if, condition_unless, receiver, *args) ⇒ Boolean
Combines ‘:if` and `:unless` gates.
-
#unless?(condition, receiver, *args) ⇒ Boolean
Evaluates an ‘:unless`-style condition.
Instance Method Details
#evaluate(condition, receiver, *args) ⇒ Boolean, Object
Evaluates a condition against ‘receiver`, dispatching by type.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cmdx/util.rb', line 18 def evaluate(condition, receiver, *args) case condition when FalseClass, NilClass false when TrueClass true when Symbol receiver.send(condition, *args) when Proc receiver.instance_exec(*args, &condition) else return condition.call(receiver, *args) if condition.respond_to?(:call) raise ArgumentError, "condition must be a Symbol, Proc, or respond to #call" end end |
#if?(condition, receiver, *args) ⇒ Boolean
Evaluates an ‘:if`-style condition. `nil` is treated as “always true”.
41 42 43 44 45 |
# File 'lib/cmdx/util.rb', line 41 def if?(condition, receiver, *args) return true if condition.nil? evaluate(condition, receiver, *args) end |
#satisfied?(condition_if, condition_unless, receiver, *args) ⇒ Boolean
Combines ‘:if` and `:unless` gates. Used across the framework to decide whether a conditional feature (callback, retry, validator, etc.) should run.
67 68 69 70 |
# File 'lib/cmdx/util.rb', line 67 def satisfied?(condition_if, condition_unless, receiver, *args) if?(condition_if, receiver, *args) && unless?(condition_unless, receiver, *args) end |
#unless?(condition, receiver, *args) ⇒ Boolean
Evaluates an ‘:unless`-style condition. `nil` is treated as “always true”.
53 54 55 56 57 |
# File 'lib/cmdx/util.rb', line 53 def unless?(condition, receiver, *args) return true if condition.nil? !evaluate(condition, receiver, *args) end |