Module: Cuprum::Rails::RSpec::ContractHelpers

Defined in:
lib/cuprum/rails/rspec/contract_helpers.rb

Overview

Helper methods for defining RSpec contracts.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.option_with_default(value, context:, default: nil) ⇒ Object

Resolves a configuration option.

If the configured value is a Proc, the Proc is executed in the context of the example group and the resulting value returned. Otherwise, returns the configured value, or the given default if the configured value is nil.

Parameters:

  • value (Object)

    The configured value for the option.

  • context (RSpec::SleepingKingStudios::ExampleGroup)

    The example group used as a context if the configured value is a Proc.

  • default (Object) (defaults to: nil)

    An optional default value if the configured value is nil.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cuprum/rails/rspec/contract_helpers.rb', line 19

def option_with_default(value, context:, default: nil)
  # :nocov:
  case value
  when Proc
    return context.instance_exec(&value) if value.arity.zero?

    context.instance_exec(default, &value)
  when nil
    default
  else
    value
  end
  # :nocov:
end

Instance Method Details

#option_with_default(value, context: nil, default: nil) ⇒ Object

Resolves a configuration option.

If the configured value is a Proc, the Proc is executed in the context of the example group and the resulting value returned. Otherwise, returns the configured value, or the given default if the configured value is nil.

Parameters:

  • value (Object)

    The configured value for the option.

  • context (RSpec::SleepingKingStudios::ExampleGroup) (defaults to: nil)

    The example group used as a context if the configured value is a Proc.

  • default (Object) (defaults to: nil)

    An optional default value if the configured value is nil.



46
47
48
49
50
51
52
53
54
55
# File 'lib/cuprum/rails/rspec/contract_helpers.rb', line 46

def option_with_default(value, context: nil, default: nil)
  # :nocov:
  Cuprum::Rails::RSpec::ContractHelpers
    .option_with_default(
      value,
      context: context || self,
      default: default
    )
  # :nocov:
end