Class: Mocha::ClassMethod
Direct Known Subclasses
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#stubbee ⇒ Object
readonly
Returns the value of attribute stubbee.
Instance Method Summary collapse
- #define_new_method ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #hidden_method ⇒ Object
- #hide_original_method ⇒ Object
-
#initialize(stubbee, method) ⇒ ClassMethod
constructor
A new instance of ClassMethod.
- #macruby_method ⇒ Object
- #macruby_method? ⇒ Boolean
- #method_exists?(method) ⇒ Boolean
- #mock ⇒ Object
- #remove_new_method ⇒ Object
- #restore_original_method ⇒ Object
- #stub ⇒ Object
- #to_s ⇒ Object
- #unstub ⇒ Object
Constructor Details
#initialize(stubbee, method) ⇒ ClassMethod
Returns a new instance of ClassMethod.
9 10 11 12 |
# File 'lib/mocha/class_method.rb', line 9 def initialize(stubbee, method) @stubbee = stubbee @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/mocha/class_method.rb', line 7 def method @method end |
#stubbee ⇒ Object (readonly)
Returns the value of attribute stubbee.
7 8 9 |
# File 'lib/mocha/class_method.rb', line 7 def stubbee @stubbee end |
Instance Method Details
#define_new_method ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mocha/class_method.rb', line 39 def define_new_method body = if macruby_method? macruby_method else %{ def #{method}(*args, &block) mocha.method_missing(:#{method}, *args, &block) end } end stubbee..class_eval(body, __FILE__, __LINE__) end |
#eql?(other) ⇒ Boolean Also known as: ==
77 78 79 80 |
# File 'lib/mocha/class_method.rb', line 77 def eql?(other) return false unless (other.class == self.class) (stubbee.object_id == other.stubbee.object_id) and (method == other.method) end |
#hidden_method ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/mocha/class_method.rb', line 67 def hidden_method if RUBY_VERSION < '1.9' method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" } else method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" } end hidden_method = "__stubba__#{method_name}__stubba__" RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym end |
#hide_original_method ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/mocha/class_method.rb', line 29 def hide_original_method if method_exists?(method) begin stubbee..send(:alias_method, hidden_method, method) rescue NameError # deal with nasties like ActiveRecord::Associations::AssociationProxy end end end |
#macruby_method ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mocha/class_method.rb', line 96 def macruby_method parts = method.to_s.split(':') signature = "#{parts.shift}(param0" parts.each_with_index { |part, index| signature << ", #{part}: param#{index + 1}" } signature << ", &block)" %{ def #{signature} # def method(param0, withExtraParam: param1, &block) mocha.method_missing( # mocha.method_missing( '#{method}'.to_sym, # 'method:withExtraParam:'.to_sym, #{Array.new(parts.length + 1) { |index| "param#{index}" }.join(', ')}, # param0, param1, &block) # &block) end # end } end |
#macruby_method? ⇒ Boolean
92 93 94 |
# File 'lib/mocha/class_method.rb', line 92 def macruby_method? method.to_s.include?(':') end |
#method_exists?(method) ⇒ Boolean
88 89 90 |
# File 'lib/mocha/class_method.rb', line 88 def method_exists?(method) stubbee.respond_to?(method, true) end |
#mock ⇒ Object
25 26 27 |
# File 'lib/mocha/class_method.rb', line 25 def mock stubbee.mocha end |
#remove_new_method ⇒ Object
52 53 54 |
# File 'lib/mocha/class_method.rb', line 52 def remove_new_method stubbee..send(:remove_method, method) end |
#restore_original_method ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mocha/class_method.rb', line 56 def restore_original_method if method_exists?(hidden_method) begin stubbee..send(:alias_method, method, hidden_method) stubbee..send(:remove_method, hidden_method) rescue NameError # deal with nasties like ActiveRecord::Associations::AssociationProxy end end end |
#stub ⇒ Object
14 15 16 17 |
# File 'lib/mocha/class_method.rb', line 14 def stub hide_original_method define_new_method end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/mocha/class_method.rb', line 84 def to_s "#{stubbee}.#{method}" end |
#unstub ⇒ Object
19 20 21 22 23 |
# File 'lib/mocha/class_method.rb', line 19 def unstub remove_new_method restore_original_method stubbee.reset_mocha end |