imitate 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/imitate.gemspec +1 -1
  3. data/lib/imitate.rb +47 -14
  4. metadata +10 -10
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/imitate.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "imitate"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Petr Janda"]
data/lib/imitate.rb CHANGED
@@ -4,32 +4,65 @@ module Imitate
4
4
 
5
5
  class << self
6
6
 
7
+ #
8
+ # Create class with a given name. If name contains any package, which
9
+ # doesn't exist yet, its created along the way.
10
+ #
11
+ # Example:
12
+ # mock_class('ActiveRecord::Base')
13
+ # mock_class('Foo') do
14
+ # def bar
15
+ # 'bar'
16
+ # end
17
+ # end
18
+ #
19
+ # Foo.new.bar #= 'bar'
20
+ #
21
+ #
22
+ # @param [String] Name of the class to be created.
23
+ # @param [Proc] Optional block to be instance evaluated on new class.
24
+ #
7
25
  def mock_class(name, &block)
8
- tokens = name.split('::')
9
- parent = Object
10
-
11
- while tokens.length > 1
12
- parent = create_const(Module, parent, tokens.shift)
13
- end
14
-
15
- create_const(Class, parent, tokens.shift, &block)
26
+ process_name(name) {|parent, token, tokens|
27
+ if tokens.length > 0
28
+ create_const(Module, parent, token)
29
+ else
30
+ create_const(Class, parent, token, &block)
31
+ end
32
+ }
16
33
  end
17
34
 
35
+ #
36
+ # Unmock previously mocked class. It doesn't affect classes created other
37
+ # way then using Imitate.
38
+ #
39
+ # @param [String] Name of the class to be removed.
40
+ #
18
41
  def unmock(name)
19
- tokens = name.split('::')
20
- parent = Object
21
-
22
- while token = tokens.shift
23
- parent = self.send(was_mocked?(parent, token) ? :remove_const : :get_const, parent, token)
24
- end
42
+ process_name(name) {|parent, token|
43
+ self.send(was_mocked?(parent, token) ? :remove_const : :get_const, parent, token)
44
+ }
25
45
  end
26
46
 
47
+ #
48
+ # Unmock all previously mocked classes. It doesn't affect classes created other
49
+ # way then using Imitate.
50
+ #
27
51
  def unmock_all
28
52
  MOCKED.each {|mock| remove_const(mock[:parent], mock[:name])}
29
53
  end
30
54
 
31
55
  private
32
56
 
57
+ def process_name(name)
58
+ tokens = name.split('::')
59
+ parent = Object
60
+
61
+ while token = tokens.shift
62
+ parent = yield(parent, token, tokens)
63
+ end
64
+ end
65
+
33
66
  def was_mocked?(parent, name)
34
67
  MOCKED.index({:parent => parent, :name => name})
35
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imitate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2157117920 !ruby/object:Gem::Requirement
16
+ requirement: &2159204420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157117920
24
+ version_requirements: *2159204420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &2157117320 !ruby/object:Gem::Requirement
27
+ requirement: &2159203920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2157117320
35
+ version_requirements: *2159203920
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2157116720 !ruby/object:Gem::Requirement
38
+ requirement: &2159203400 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2157116720
46
+ version_requirements: *2159203400
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2157116120 !ruby/object:Gem::Requirement
49
+ requirement: &2159202880 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.8.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2157116120
57
+ version_requirements: *2159202880
58
58
  description: Simple class mock
59
59
  email: petrjanda@me.com
60
60
  executables: []
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  segments:
92
92
  - 0
93
- hash: -1839012386666507265
93
+ hash: 2996185596802046611
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements: