Module: Riffer::Helpers::CallOrValue

Extended by:
CallOrValue
Included in:
CallOrValue
Defined in:
lib/riffer/helpers/call_or_value.rb

Overview

Resolves the “Proc-or-value” idiom: if thing is a Proc, calls it (passing context when its arity is non-zero); otherwise returns thing unchanged. When thing is nil, returns default.

Instance Method Summary collapse

Instance Method Details

#resolve(thing, context: nil, default: nil) ⇒ Object

: (untyped, ?context: untyped, ?default: untyped) -> untyped



11
12
13
14
15
# File 'lib/riffer/helpers/call_or_value.rb', line 11

def resolve(thing, context: nil, default: nil)
  return default if thing.nil?
  return thing unless thing.is_a?(Proc)
  thing.arity.zero? ? thing.call : thing.call(context)
end