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



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

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)
  raise '#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



45
46
47
48
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 45

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
32
33
34
# File 'lib/motion-spec/context_helper/memoized_helpers.rb', line 27

def subject(name = nil, &block)
  if name
    let(name, &block)
    alias_method :subject, name
  else
    let(:subject, &block)
  end
end

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



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

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