fakes 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,14 +4,37 @@ module Fakes
4
4
  attr_reader :klass
5
5
 
6
6
  def initialize(fully_qualified_klass,replacement,options ={})
7
- klass_parts = fully_qualified_klass.to_s
8
- parts = klass_parts.split('::')
9
- the_module = parts.count == 1 ? Object : eval(parts.slice(0,parts.count - 1).join('::'))
10
- @klass = parts[parts.count - 1].to_s.to_sym
7
+ modules = get_modules(fully_qualified_klass)
8
+ @klass = modules.keys.last.to_sym
9
+
10
+ class_root = modules.keys[modules.keys.count - 2]
11
+ class_root = modules[class_root.to_sym]
11
12
 
12
13
  @replacement = replacement
13
- @remove_strategy = options.fetch(:remove_strategy,lambda{|klass_symbol| the_module.send(:remove_const,klass_symbol)})
14
- @set_strategy = options.fetch(:set_strategy,lambda{|klass_symbol,new_value| the_module.const_set(klass_symbol,new_value)})
14
+
15
+ @remove_strategy = options.fetch(:remove_strategy, Proc.new do |klass|
16
+ class_root.send(:remove_const, klass)
17
+ end)
18
+
19
+ @set_strategy = options.fetch(:set_strategy, Proc.new do |klass, new_value|
20
+ class_root.const_set(klass.to_sym, new_value)
21
+ end)
22
+
23
+ end
24
+
25
+ def get_modules(fully_qualified_klass)
26
+ klass_parts = fully_qualified_klass.to_s.split("::")
27
+ root = Object
28
+ modules = {}
29
+ modules[:Object] = root
30
+
31
+ klass_parts.each do |part|
32
+ class_or_module = root.const_get(part.to_sym)
33
+ modules[part.to_sym] = class_or_module
34
+ root = class_or_module
35
+ end
36
+
37
+ modules
15
38
  end
16
39
 
17
40
  def initiate
@@ -10,7 +10,7 @@ module Fakes
10
10
  end
11
11
 
12
12
  def add_fake_for(klass,the_fake)
13
- symbol = klass.to_s.to_sym
13
+ symbol = klass.name.to_sym
14
14
  ensure_swap_does_not_already_exist_for(symbol)
15
15
  swap = ClassSwap.new(klass,the_fake)
16
16
  @swaps[symbol] = swap
@@ -1,3 +1,3 @@
1
1
  module Fakes
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
@@ -102,6 +102,7 @@ module Fakes
102
102
  ClassSwaps.instance.reset
103
103
  Dir.should_not == replacement
104
104
  end
105
+
105
106
  it 'should be able to swap class values in another module' do
106
107
  ClassSwaps.instance.add_fake_for(SomeModule::ClassInAModule,replacement)
107
108
  SomeModule::ClassInAModule.should == replacement
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-01 00:00:00.000000000 Z
12
+ date: 2013-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -132,18 +132,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  - - ! '>='
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
- segments:
136
- - 0
137
- hash: -4322059409981481306
138
135
  required_rubygems_version: !ruby/object:Gem::Requirement
139
136
  none: false
140
137
  requirements:
141
138
  - - ! '>='
142
139
  - !ruby/object:Gem::Version
143
140
  version: '0'
144
- segments:
145
- - 0
146
- hash: -4322059409981481306
147
141
  requirements: []
148
142
  rubyforge_project: fakes
149
143
  rubygems_version: 1.8.25