Class: RR::Space

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/space.rb

Overview

RR::Space is a Dependency Injection en.wikipedia.org/wiki/Dependency_injection and global state object for the RR framework. The RR::Space.instance is a singleton that holds the state.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpace

Returns a new instance of Space.



20
21
22
23
24
# File 'lib/rr/space.rb', line 20

def initialize
  @double_injections = HashWithObjectIdKey.new
  @ordered_doubles = []
  @trim_backtrace = false
end

Class Attribute Details

.instanceObject



7
8
9
# File 'lib/rr/space.rb', line 7

def instance
  @instance ||= new
end

Instance Attribute Details

#double_injectionsObject (readonly)

Returns the value of attribute double_injections.



18
19
20
# File 'lib/rr/space.rb', line 18

def double_injections
  @double_injections
end

#ordered_doublesObject (readonly)

Returns the value of attribute ordered_doubles.



18
19
20
# File 'lib/rr/space.rb', line 18

def ordered_doubles
  @ordered_doubles
end

#trim_backtraceObject

Returns the value of attribute trim_backtrace.



19
20
21
# File 'lib/rr/space.rb', line 19

def trim_backtrace
  @trim_backtrace
end

Instance Method Details

#double(double_injection, definition = double_definition) ⇒ Object

Creates and registers a Double to be verified.



41
42
43
44
45
46
# File 'lib/rr/space.rb', line 41

def double(double_injection, definition = double_definition)
  double = Double.new(self, double_injection, definition)
  double.definition.double = double
  double_injection.register_double double
  double
end

#double_creatorObject

Creates a DoubleCreator.



36
37
38
# File 'lib/rr/space.rb', line 36

def double_creator
  DoubleCreator.new(self)
end

#double_definitionObject



48
49
50
# File 'lib/rr/space.rb', line 48

def double_definition
  DoubleDefinition.new(self)
end

#double_injection(object, method_name) ⇒ Object

Reuses or creates, if none exists, a DoubleInjection for the passed in object and method_name. When a DoubleInjection is created, it binds the dispatcher to the object.



56
57
58
59
60
61
62
63
64
# File 'lib/rr/space.rb', line 56

def double_injection(object, method_name)
  double_injection = @double_injections[object][method_name.to_sym]
  return double_injection if double_injection

  double_injection = DoubleInjection.new(object, method_name.to_sym)
  @double_injections[object][method_name.to_sym] = double_injection
  double_injection.bind
  double_injection
end

#double_method_proxy(creator, object, method_name = nil, &definition) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rr/space.rb', line 26

def double_method_proxy(creator, object, method_name=nil, &definition)
  if method_name && definition
    raise ArgumentError, "Cannot pass in a method name and a block"
  end
  proxy = DoubleMethodProxy.new(creator, object, &definition)
  return proxy unless method_name
  proxy.__send__(method_name)
end

#register_ordered_double(double) ⇒ Object

Registers the ordered Double to be verified.



67
68
69
# File 'lib/rr/space.rb', line 67

def register_ordered_double(double)
  @ordered_doubles << double
end

#resetObject

Resets the registered Doubles and ordered Doubles



99
100
101
102
# File 'lib/rr/space.rb', line 99

def reset
  reset_ordered_doubles
  reset_double_injections
end

#reset_double(object, method_name) ⇒ Object

Resets the DoubleInjection for the passed in object and method_name.



112
113
114
115
116
# File 'lib/rr/space.rb', line 112

def reset_double(object, method_name)
  double_injection = @double_injections[object].delete(method_name)
  @double_injections.delete(object) if @double_injections[object].empty?
  double_injection.reset
end

#verify_double(object, method_name) ⇒ Object

Verifies the DoubleInjection for the passed in object and method_name.



105
106
107
108
109
# File 'lib/rr/space.rb', line 105

def verify_double(object, method_name)
  @double_injections[object][method_name].verify
ensure
  reset_double object, method_name
end

#verify_doublesObject

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



90
91
92
93
94
95
96
# File 'lib/rr/space.rb', line 90

def verify_doubles
  @double_injections.each do |object, method_double_map|
    method_double_map.keys.each do |method_name|
      verify_double(object, method_name)
    end
  end
end

#verify_ordered_double(double) ⇒ Object

Verifies that the passed in ordered Double is being called in the correct position.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rr/space.rb', line 73

def verify_ordered_double(double)
  unless double.terminal?
    raise Errors::DoubleOrderError,
          "Ordered Doubles cannot have a NonTerminal TimesCalledExpectation"
  end
  unless @ordered_doubles.first == double
    message = Double.formatted_name(double.method_name, double.expected_arguments)
    message << " called out of order in list\n"
    message << Double.list_message_part(@ordered_doubles)
    raise Errors::DoubleOrderError, message
  end
  @ordered_doubles.shift unless double.attempt?
  double
end