Class: Spy::Constant
Instance Attribute Summary collapse
-
#base_module ⇒ Module
readonly
The module that is being watched.
-
#constant_name ⇒ Symbol
readonly
The name of the constant that is/will be stubbed.
-
#original_value ⇒ Object
readonly
The original value that was set when it was hooked.
Class Method Summary collapse
-
.get(base_module, constant_name) ⇒ Nil, Constant
retrieves the spy for given constnat and module or returns nil.
-
.off(base_module, constant_name) ⇒ Constant
retrieves the spy for given constant and module and unhooks the constant from the module.
-
.on(base_module, constant_name) ⇒ Constant
finds existing spy or creates a new constant spy and hooks the constant.
Instance Method Summary collapse
-
#and_hide ⇒ self
unsets the constant.
-
#and_return(value) ⇒ self
sets the constant to the requested value.
-
#currently_defined? ⇒ Boolean
checks to see if the constant is currently defined?.
-
#hidden? ⇒ Boolean
checks to see if the constant is hidden?.
-
#hook(opts = {}) ⇒ self
stashes the original constant then overwrites it with nil.
-
#hooked? ⇒ Boolean
checks to see if this spy is hooked?.
-
#initialize(base_module, constant_name) ⇒ Constant
constructor
A new instance of Constant.
-
#name ⇒ Object
full name of spied constant.
-
#previously_defined? ⇒ Boolean
checks to see if the constant is previously defined?.
-
#unhook ⇒ self
restores the original value of the constant or unsets it if it was unset.
Constructor Details
#initialize(base_module, constant_name) ⇒ Constant
Returns a new instance of Constant.
18 19 20 21 22 23 |
# File 'lib/spy/constant.rb', line 18 def initialize(base_module, constant_name) raise ArgumentError, "#{base_module.inspect} is not a kind of Module" unless base_module.is_a? Module raise ArgumentError, "#{constant_name.inspect} is not a kind of Symbol" unless constant_name.is_a? Symbol @base_module, @constant_name = base_module, constant_name.to_sym @original_value = @new_value = @previously_defined = nil end |
Instance Attribute Details
#base_module ⇒ Module (readonly)
Returns the module that is being watched.
|
# File 'lib/spy/constant.rb', line 4
|
#constant_name ⇒ Symbol (readonly)
Returns the name of the constant that is/will be stubbed.
|
# File 'lib/spy/constant.rb', line 4
|
#original_value ⇒ Object (readonly)
Returns the original value that was set when it was hooked.
|
# File 'lib/spy/constant.rb', line 4
|
Class Method Details
.get(base_module, constant_name) ⇒ Nil, Constant
retrieves the spy for given constnat and module or returns nil
117 118 119 120 121 122 |
# File 'lib/spy/constant.rb', line 117 def get(base_module, constant_name) nest = Nest.get(base_module) if nest nest.get(constant_name) end end |
.off(base_module, constant_name) ⇒ Constant
retrieves the spy for given constant and module and unhooks the constant from the module
109 110 111 112 113 |
# File 'lib/spy/constant.rb', line 109 def off(base_module, constant_name) spy = get(base_module, constant_name) raise NoSpyError, "#{constant_name} was not spied on #{base_module}" unless spy spy.unhook end |
.on(base_module, constant_name) ⇒ Constant
finds existing spy or creates a new constant spy and hooks the constant
102 103 104 |
# File 'lib/spy/constant.rb', line 102 def on(base_module, constant_name) new(base_module, constant_name).hook end |
Instance Method Details
#and_hide ⇒ self
unsets the constant
60 61 62 63 |
# File 'lib/spy/constant.rb', line 60 def and_hide base_module.send(:remove_const, constant_name) if currently_defined? self end |
#and_return(value) ⇒ self
sets the constant to the requested value
68 69 70 71 72 73 |
# File 'lib/spy/constant.rb', line 68 def and_return(value) @new_value = value and_hide base_module.const_set(constant_name, @new_value) self end |
#currently_defined? ⇒ Boolean
checks to see if the constant is currently defined?
89 90 91 |
# File 'lib/spy/constant.rb', line 89 def currently_defined? base_module.const_defined?(constant_name, false) end |
#hidden? ⇒ Boolean
checks to see if the constant is hidden?
83 84 85 |
# File 'lib/spy/constant.rb', line 83 def hidden? hooked? && currently_defined? end |
#hook(opts = {}) ⇒ self
stashes the original constant then overwrites it with nil
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/spy/constant.rb', line 33 def hook(opts = {}) opts[:force] ||= false Nest.fetch(base_module).add(self) Agency.instance.recruit(self) @previously_defined = currently_defined? if previously_defined? || !opts[:force] @original_value = base_module.const_get(constant_name, false) end and_return(@new_value) self end |
#hooked? ⇒ Boolean
checks to see if this spy is hooked?
77 78 79 |
# File 'lib/spy/constant.rb', line 77 def hooked? self.class.get(base_module, constant_name) == self end |
#name ⇒ Object
full name of spied constant
26 27 28 |
# File 'lib/spy/constant.rb', line 26 def name "#{base_module.name}::#{constant_name}" end |
#previously_defined? ⇒ Boolean
checks to see if the constant is previously defined?
95 96 97 |
# File 'lib/spy/constant.rb', line 95 def previously_defined? @previously_defined end |
#unhook ⇒ self
restores the original value of the constant or unsets it if it was unset
48 49 50 51 52 53 54 55 56 |
# File 'lib/spy/constant.rb', line 48 def unhook Nest.get(base_module).remove(self) Agency.instance.retire(self) and_return(@original_value) if previously_defined? @original_value = @previously_defined = nil self end |