Class: Gimme::GivesClassMethods

Inherits:
BlankSlate show all
Defined in:
lib/gimme/gives_class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  meta_class = meta_for(@cls)
  method = ResolvesMethods.new(meta_class,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)
    meta_class.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
  meta_class.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)
      meta_class.instance_eval { define_method method, cls.method(hidden_method_name) }
      meta_class.send(:remove_method, hidden_method_name)
    else
      meta_class.send(:remove_method, method)
    end
    @@stubbings.clear
  end
end

Instance Attribute Details

#raises_no_method_errorObject

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