Module: RSpec::SleepingKingStudios::Deferred::Dsl::MemoizedHelpers

Included in:
RSpec::SleepingKingStudios::Deferred::Dsl
Defined in:
lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb

Overview

DSL for defining memoized helpers for deferred examples.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(other) ⇒ Object

Callback invoked when the module is extended into another module or class.

Defines a HelperImplementations module on the module and includes it in the module.

Parameters:

  • other (Module)

    the other module or class.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 14

def self.extended(other)
  super

  return if other.const_defined?(:HelperImplementations, true)

  other.const_set(
    :HelperImplementations,
    Module.new do
      named = Module.new

      const_set(:NamedSuper, named)

      include named
    end
  )
end

Instance Method Details

#call(example_group) ⇒ void

This method returns an undefined value.

Invokes the deferred examples on the given example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group.



32
33
34
35
36
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 32

def call(example_group)
  super

  include self::HelperImplementations
end

#let(helper_name = nil, &block) ⇒ void

This method returns an undefined value.

Defines a memoized helper.

Parameters:

  • helper_name (String, Symbol) (defaults to: nil)

    the name of the helper method.

  • block (Block)

    the implementation of the helper method.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 45

def let(helper_name, &)
  helper_name = helper_name.to_sym

  self::HelperImplementations.define_method(helper_name, &)

  define_method(helper_name) do
    helper_values = @memoized_helper_values ||= {}

    helper_values.fetch(helper_name) do
      helper_values[helper_name] = super()
    end
  end
end

#let!(helper_name = nil, &block) ⇒ void

This method returns an undefined value.

Defines a memoized helper and adds a hook to evaluate it before examples.

Parameters:

  • helper_name (String, Symbol) (defaults to: nil)

    the name of the helper method.

  • block (Block)

    the implementation of the helper method.



66
67
68
69
70
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 66

def let!(helper_name, &)
  let(helper_name, &)

  before(:example) { send(helper_name) }
end

#let?(helper_name, &block) ⇒ void

This method returns an undefined value.

Defines an optional memoized helper.

The helper will use the parent value if defined; otherwise, will use the given value.

Parameters:

  • helper_name (String, Symbol)

    the name of the helper method.

  • block (Block)

    the implementation of the helper method.



81
82
83
84
85
86
87
88
89
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 81

def let?(helper_name, &block)
  wrapped = lambda do
    next super() if defined?(super())

    instance_exec(&block)
  end

  let(helper_name, &wrapped)
end

#subject(helper_name = nil, &block) ⇒ void

This method returns an undefined value.

Defines a memoized subject helper.

Parameters:

  • helper_name (String, Symbol) (defaults to: nil)

    the name of the helper method.

  • block (Block)

    the implementation of the helper method.



98
99
100
101
102
103
104
105
106
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 98

def subject(helper_name = nil, &)
  let(:subject, &)

  define_method(helper_name) { subject } if helper_name

  self::HelperImplementations::NamedSuper.define_method(:subject) do
    raise NotImplementedError, '`super` in named subjects is not supported'
  end
end

#subject!(helper_name = nil, &block) ⇒ void

This method returns an undefined value.

Defines a memoized subject helper and adds a hook to evaluate it.

Parameters:

  • helper_name (String, Symbol) (defaults to: nil)

    the name of the helper method.

  • block (Block)

    the implementation of the helper method.



115
116
117
118
119
# File 'lib/rspec/sleeping_king_studios/deferred/dsl/memoized_helpers.rb', line 115

def subject!(helper_name = nil, &)
  subject(helper_name, &)

  before(:example) { send(helper_name) }
end