Module: CMDx::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/cmdx/util.rb

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

Instance Method Details

#evaluate(condition, receiver, *args) ⇒ Boolean, Object

Evaluates a condition against ‘receiver`, dispatching by type.

Parameters:

  • condition (Boolean, nil, Symbol, Proc, #call)

    ‘:if`/`:unless`-style gate, method name, or callable evaluated against `receiver`

  • receiver (Object)

    object the condition runs against (usually a Task)

  • args (Array<Object>)

    extra arguments forwarded to the condition

Returns:

  • (Boolean, Object)

    truthiness result (Procs ‘instance_exec` on receiver)

Raises:

  • (ArgumentError)

    when the condition is not a supported 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”.

Parameters:

  • condition (Boolean, nil, Symbol, Proc, #call)

    gate to check

  • receiver (Object)

    object the condition runs against

  • args (Array<Object>)

    extra arguments forwarded to the condition

Returns:

  • (Boolean)

    true when ‘condition` is nil or evaluates truthy



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.

Parameters:

  • condition_if (Boolean, nil, Symbol, Proc, #call)

    ‘:if` gate

  • condition_unless (Boolean, nil, Symbol, Proc, #call)

    ‘:unless` gate

  • receiver (Object)

    object the conditions run against

  • args (Array<Object>)

    extra arguments forwarded to both conditions

Returns:

  • (Boolean)

    true only when both gates pass



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”.

Parameters:

  • condition (Boolean, nil, Symbol, Proc, #call)

    gate to check

  • receiver (Object)

    object the condition runs against

  • args (Array<Object>)

    extra arguments forwarded to the condition

Returns:

  • (Boolean)

    true when ‘condition` is nil or evaluates falsy



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