Module: Orangutan::Chantek

Defined in:
lib/orangutan/chantek.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



10
11
12
# File 'lib/orangutan/chantek.rb', line 10

def calls
  @calls
end

#expectationsObject (readonly)

Returns the value of attribute expectations.



10
11
12
# File 'lib/orangutan/chantek.rb', line 10

def expectations
  @expectations
end

Instance Method Details

#call(name, method, *args) ⇒ Object



12
13
14
# File 'lib/orangutan/chantek.rb', line 12

def call name, method, *args
  Call.new(name, method, args)
end

#expectation(name) ⇒ Object



127
128
129
# File 'lib/orangutan/chantek.rb', line 127

def expectation name
  @named_expectations[name]
end

#fire_event(name, event, *args) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/orangutan/chantek.rb', line 118

def fire_event name, event, *args
  events_for_name = @events[name]
  raise "failed to find any events for #{name}" unless events_for_name
  # it makes no sense, but 'events_for_name[event]' returns nil so in desperation, we iterate over the events
  events_for_name.each do |event_name,delegates|
    delegates.each { |delegate| delegate.invoke *args } if event_name == event
  end
end

#first_match(name, method, args) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/orangutan/chantek.rb', line 108

def first_match name, method, args
  expectations_for_name = @expectations[name]
  if expectations_for_name
    expectations_for_name.each do |expectation|
        return expectation if expectation.matches?(method, *args)
    end
  end
  nil
end

#implement_event(clazz, name, delegates) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/orangutan/chantek.rb', line 86

def implement_event clazz, name, delegates
  method = "add_#{name}".to_sym
  clazz.instance_eval do
    define_method method  do |delegate|
      __react__(method, [])
      delegates << delegate
    end
  end
end

#implement_interface(clazz, interface, events) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/orangutan/chantek.rb', line 63

def implement_interface clazz, interface, events
  return unless interface
  reflector = Reflector.new(interface)
  clazz.instance_eval { include interface }
  reflector.methods.each {|method| implement_method clazz, method }
  reflector.events.each do |event|
    events[event] = []
    implement_event clazz, event, events[event]
  end
end

#implement_method(clazz, name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/orangutan/chantek.rb', line 74

def implement_method clazz, name
  clazz.instance_eval do
    include StubInclude

    define_method name do |*args|
      yield_container, return_container = __react__(name, args)
      yield_container.value.each {|v| yield *v } if yield_container && block_given?
      return __return__(name, return_container)
    end
  end
end

#register_object(name, object) ⇒ Object



25
26
27
# File 'lib/orangutan/chantek.rb', line 25

def register_object name, object
  @stubs[name] ||= object
end

#reset_stubsObject



16
17
18
19
20
21
22
23
# File 'lib/orangutan/chantek.rb', line 16

def reset_stubs
  @calls = []
  @expectations = {}
  @named_expectations = {}
  @stubs= {}
  @events = {}
  @stubbed_methods = []
end

#restore_method(name, method) ⇒ Object



44
45
46
47
48
49
# File 'lib/orangutan/chantek.rb', line 44

def restore_method name, method
  @stubs[name].eigen_eval do
    alias_method(method, "__unstubbed_#{method}")
  end
  @stubbed_methods.delete [name, method]
end

#restore_methodsObject



51
52
53
# File 'lib/orangutan/chantek.rb', line 51

def restore_methods
  @stubbed_methods.each {|tuple| restore_method tuple[0], tuple[1]}
end

#so_when(name, params = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/orangutan/chantek.rb', line 96

def so_when name, params={}
  expectations_for_name = @expectations[name]
  @expectations[name] = expectations_for_name = [] unless expectations_for_name
  expectation = Orangutan::Expectation.new
  if params[:as]
    raise "An expectation called foo_expection was already registered" if @named_expectations[params[:as]]
    @named_expectations[params[:as]] = expectation
  end
  expectations_for_name << expectation
  expectation
end

#stub(name, params = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/orangutan/chantek.rb', line 55

def stub name, params={}
  return @stubs[name] if @stubs[name]
  clazz = Class.new(StubBase)
  @events[name] = {}
  implement_interface clazz, params[:clr_interface], @events[name]
  @stubs[name] = clazz.new(name, self, params[:recursive])
end

#stub_method(name, method) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/orangutan/chantek.rb', line 29

def stub_method name, method
  object = @stubs[name]
  raise "could not find an object registered with the name #{name}" unless object
  chantek = self
  object.instance_eval do
    @parent = chantek
    @name = name
  end
  @stubbed_methods << [name, method]
  object.eigen_eval do
    alias_method("__unstubbed_#{method}", method)
  end
  implement_method object.eigen, method
end

#stub_namesObject



131
132
133
# File 'lib/orangutan/chantek.rb', line 131

def stub_names
  @stubs.keys
end