Class: Spy::Core
- Inherits:
-
Object
- Object
- Spy::Core
- Defined in:
- lib/spy/core.rb
Overview
The main internal API. This is used directly by ‘Spy::API` and is the primary control center for creating and removing spies.
Syntactic sugar (like ‘Spy.restore(object, msg)` vs `Spy.restore(:all)`) should be handled in `Spy::API` and utilize `Spy::Core`
Constant Summary collapse
- UNSAFE_METHODS =
[:object_id, :__send__, :__id__, :method, :singleton_class]
Instance Method Summary collapse
-
#add_multi_spy(multi_blueprint) ⇒ Object
Start spying on all of the given objects and methods.
-
#add_spy(blueprint) ⇒ Object
Start spying on the given object and method.
-
#initialize ⇒ Core
constructor
A new instance of Core.
-
#remove_all_spies ⇒ Object
Stops spying on all objects and methods.
-
#remove_spy(blueprint) ⇒ Object
Stop spying on the given object and method.
Constructor Details
Instance Method Details
#add_multi_spy(multi_blueprint) ⇒ Object
Start spying on all of the given objects and methods
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/spy/core.rb', line 38 def add_multi_spy(multi_blueprint) target = multi_blueprint.target type = multi_blueprint.type methods = target.public_send(type).reject(&method(:unsafe_method?)) spies = methods.map do |method_name| singular_type = type.to_s.sub(/s$/, '').to_sym add_spy(Blueprint.new(multi_blueprint.target, method_name, singular_type)) end Multi.new(spies) end |
#add_spy(blueprint) ⇒ Object
Start spying on the given object and method
25 26 27 28 29 30 31 32 |
# File 'lib/spy/core.rb', line 25 def add_spy(blueprint) if prev = @registry.get(blueprint) raise Errors::AlreadySpiedError.new("Already spied on #{blueprint} here:\n\t#{prev[0].caller.join("\n\t")}") end spy = Instance.new(blueprint) @registry.insert(blueprint, spy) spy.start end |
#remove_all_spies ⇒ Object
Stops spying on all objects and methods
59 60 61 |
# File 'lib/spy/core.rb', line 59 def remove_all_spies @registry.remove_all.each(&:stop) end |
#remove_spy(blueprint) ⇒ Object
Stop spying on the given object and method
53 54 55 56 |
# File 'lib/spy/core.rb', line 53 def remove_spy(blueprint) spy = @registry.remove(blueprint) spy.stop end |