Module: MotionSpec::ContextHelper::MemoizedHelpers

Included in:
MotionSpec::Context
Defined in:
lib/motion-spec/context_helper/memoized_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__memoizedObject

Returns the value of attribute __memoized.



5
6
7
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 5

def __memoized
  @__memoized
end

Instance Method Details

#is_expectedObject

rubocop:disable Style/PredicateName



38
39
40
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 38

def is_expected # rubocop:disable Style/PredicateName
  expect(subject)
end

#let(name, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 7

def let(name, &block)
  fail '#let or #subject called without a block' unless block_given?

  (class << self; self; end).class_eval do
    define_method(name) do
      self.__memoized ||= {}
      __memoized.fetch(name) { __memoized[name] = block.call }
    end
  end

  # The way that nested contexts are implemented requires us to manually
  # reset any memoized values after each spec via an 'after'.
  after { reset_memoized }
end

#let!(name, &block) ⇒ Object



22
23
24
25
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 22

def let!(name, &block)
  let(name, &block)
  before { __send__(name) }
end

#reset_memoizedObject



42
43
44
45
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 42

def reset_memoized
  @__memoized = nil
  parent_context.reset_memoized if respond_to?(:parent_context)
end

#subject(name = nil, &block) ⇒ Object



27
28
29
30
31
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 27

def subject(name = nil, &block)
  let(name, &block) if name

  let(:subject, &block)
end

#subject!(name = nil, &block) ⇒ Object



33
34
35
36
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 33

def subject!(name = nil, &block)
  subject(name, &block)
  before { subject }
end