Class: Gimme::GivesClassMethods
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Gimme::GivesClassMethods
- Defined in:
- lib/gimme/gives_class_methods.rb
Instance Attribute Summary collapse
-
#raises_no_method_error ⇒ Object
Returns the value of attribute raises_no_method_error.
Instance Method Summary collapse
-
#initialize(cls) ⇒ GivesClassMethods
constructor
A new instance of GivesClassMethods.
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(cls) ⇒ GivesClassMethods
Returns a new instance of GivesClassMethods.
5 6 7 8 9 |
# File 'lib/gimme/gives_class_methods.rb', line 5 def initialize(cls) @cls = cls @raises_no_method_error = true @@stubbings ||= {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gimme/gives_class_methods.rb', line 11 def method_missing(method, *args, &block) cls = @cls = (@cls) method = ResolvesMethods.new(,method,args).resolve(@raises_no_method_error) hidden_method_name = hidden_name_for(method) if @cls.respond_to?(method) && !@cls.respond_to?(hidden_method_name) .send(:alias_method, hidden_method_name, method) end @@stubbings[cls] ||= {} @@stubbings[cls][method] ||= {} @@stubbings[cls][method][args] = block if block #TODO this will be redundantly overwritten .instance_eval do define_method method do |*actual_args| InvokesSatisfiedStubbing.new(@@stubbings[cls]).invoke(method, actual_args) end end Gimme.on_reset do if cls.respond_to?(hidden_method_name) .instance_eval { define_method method, cls.method(hidden_method_name) } .send(:remove_method, hidden_method_name) else .send(:remove_method, method) end @@stubbings.clear end end |
Instance Attribute Details
#raises_no_method_error ⇒ Object
Returns the value of attribute raises_no_method_error.
4 5 6 |
# File 'lib/gimme/gives_class_methods.rb', line 4 def raises_no_method_error @raises_no_method_error end |