active_mocker 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efd0c09e9b3b692f45b3b44d3585075fa656c078
4
- data.tar.gz: 1efef179992c2a8f2f85d713829e31451fc6c021
3
+ metadata.gz: 17ea758ca27dc137a61902c20f54977a6210635e
4
+ data.tar.gz: eaf4e9d11079825f6e086475fdd60722a190264a
5
5
  SHA512:
6
- metadata.gz: 82951efa29250007b6fda13d228005ef03d84035df9276fb1a48bbb5e08940445bb151e59505dafc9867f79a59b2d10216a1366662b9ef4edfacc22f5a760188
7
- data.tar.gz: 3d0324bd594d7a2d67b573b14aa61ab161bf4fff5d5eea6bae014d958f22f7f19fd65a768ef1f7db7744f5d5e794fa0852dbec3096402f5ecca23e9cba03e137
6
+ metadata.gz: 34471978c024f9efd19a4a8bd302871f7f0dac2d167c7997b0b06c667ed4dfb258e2181bbf04839ad225a91c1f333c9d48e7f97539b84e1ea7b613b98afd1f10
7
+ data.tar.gz: d24788dd0753955fb8676301fc1860347281ce7a1366be4a9c09ac8b5c51bd13b4bce0750a1cb83d3629cedb0fb8e1d1ff1bf3795c90ba5e29b56653ac72ab7d
data/README.md CHANGED
@@ -41,7 +41,7 @@ Or install it yourself as:
41
41
  belongs_to :account
42
42
 
43
43
  def bar(name, type=nil)
44
- puts bar
44
+ puts name
45
45
  end
46
46
 
47
47
  end
@@ -49,6 +49,7 @@ Or install it yourself as:
49
49
  require 'active_mocker'
50
50
 
51
51
  ActiveMocker::Base.configure do |config|
52
+ # Required Options
52
53
  config.schema_file = "#{APP_ROOT}/db/schema.rb"
53
54
  config.model_dir = "#{APP_ROOT}/app/models"
54
55
  # Dependency injection
@@ -64,10 +65,7 @@ Or install it yourself as:
64
65
  config.log_level = Logger::WARN #default
65
66
  end
66
67
 
67
- mocker = ActiveMocker::Base.new({schema: {path: [path to schema.rb file]},
68
- model: {path: [dir of rails models]}}
69
-
70
- mocker.mock('Person')
68
+ ActiveMocker::Base.mock('Person')
71
69
  => PersonMock
72
70
 
73
71
  PersonMock.column_names
@@ -123,7 +121,7 @@ Or install it yourself as:
123
121
  belongs_to :account
124
122
 
125
123
  def bar(name)
126
- puts bar
124
+ puts name
127
125
  end
128
126
 
129
127
  end
@@ -132,13 +130,54 @@ Or install it yourself as:
132
130
  => ArgumentError: wrong number of arguments (2 for 1)
133
131
 
134
132
 
133
+ app/models/person.rb
134
+
135
+ class Person < ActiveRecord::Base
136
+ belongs_to :account
137
+
138
+ def foo(name, type=nil)
139
+ puts name
140
+ end
141
+
142
+ end
143
+
144
+ mock_class.mock_instance_method(:bar) do |name, type=nil|
145
+ "Now implemented with #{name} and #{type}"
146
+ end
147
+ => NameError: undefined method `bar' for class `PersonMock'
148
+
149
+ ### Enable ActiveHash support
150
+ ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.
151
+ [zilkey/active_hash](https://github.com/zilkey/active_hash)
152
+
153
+
154
+
155
+ ActiveMocker::Base.configure do |config|
156
+ config.active_hash_as_base = true
157
+ end
158
+
159
+ ActiveMocker::Base.mock('Person').superclass
160
+ => ActiveHash::Base
161
+
162
+ dustin = PersonMock.create(first_name: 'Dustin')
163
+ => #<PersonMock @attributes={:first_name=>"Dustin" :id=>1}>
164
+
165
+ PersonMock.all
166
+ => [#<PersonMock: @attributes={:first_name=>"Dustin", :id=>1}>]
167
+
168
+ dustin.last_name = 'Zeisler'
169
+ => "Zeisler"
170
+
171
+ dustin.save
172
+ => true
135
173
 
136
- ### When the schema changes
174
+ ### Known Limitations
137
175
 
176
+ ::mock model names and table names must follow the default ActiveRecord naming pattern.
138
177
 
139
178
  ## Contributing
140
179
 
141
- 1. Fork it ( http://github.com/<my-github-username>/active_mocker/fork )
180
+ 1. Fork it ( http://github.com/zeisler/active_mocker/fork )
142
181
  2. Create your feature branch (`git checkout -b my-new-feature`)
143
182
  3. Commit your changes (`git commit -am 'Add some feature'`)
144
183
  4. Push to the branch (`git push origin my-new-feature`)
@@ -18,14 +18,17 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency "activesupport", "~>4.0"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.5"
22
24
  spec.add_development_dependency "rake", "~>10.1"
23
25
  spec.add_development_dependency "rspec", "~>2.14"
24
26
  spec.add_development_dependency "i18n", "~>0.6"
25
- spec.add_development_dependency "activesupport", "~>4.0"
26
27
  spec.add_development_dependency "active_hash", "~>1.3"
27
28
 
28
- #spec.add_development_dependency "debase", "~>0.0"
29
- #spec.add_development_dependency "ruby-debug-ide", "~>0.4"
29
+ if ENV['DEBUG'] == '1'
30
+ spec.add_development_dependency "debase", "~>0.0"
31
+ spec.add_development_dependency "ruby-debug-ide", "~>0.4"
32
+ end
30
33
 
31
34
  end
@@ -57,10 +57,8 @@ module ActiveMocker
57
57
 
58
58
  add_method_mock_of
59
59
  if model_methods
60
- add_instance_methods
61
- add_mock_instance_method
62
- add_mock_class_method
63
60
  add_class_methods
61
+ add_instance_methods
64
62
  end
65
63
 
66
64
  end
@@ -68,10 +66,8 @@ module ActiveMocker
68
66
  def plain_mock_class
69
67
  add_method_mock_of
70
68
  if model_methods
71
- add_instance_methods
72
- add_mock_instance_method
73
- add_mock_class_method
74
69
  add_class_methods
70
+ add_instance_methods
75
71
  end
76
72
  add_relationships if model_relationships
77
73
  add_column_names_method if schema_attributes
@@ -118,11 +114,14 @@ module ActiveMocker
118
114
  m = method.keys.first
119
115
  params = Reparameterize.call(method.values.first)
120
116
  params_pass = Reparameterize.call(method.values.first, true)
121
- klass.instance_variable_set("@instance_#{m}", eval_lambda(params, %Q[raise "##{m} is not Implemented for Class: #{klass.name}"]))
122
- block = eval_lambda(params, %Q[ self.class.instance_variable_get("@instance_#{m}").call(#{params_pass})])
123
- klass.instance_eval do
124
- define_method(m, block)
125
- end
117
+
118
+ klass.send(:model_methods_template)[m] = eval_lambda(params, %Q[raise "##{m} is not Implemented for Class: #{klass.name}"])
119
+
120
+ klass.class_eval <<-eos, __FILE__, __LINE__+1
121
+ def #{m}(#{params})
122
+ model_instance_methods[#{m.inspect}].call(#{params_pass})
123
+ end
124
+ eos
126
125
  end
127
126
  end
128
127
 
@@ -132,39 +131,19 @@ module ActiveMocker
132
131
  m = method.keys.first
133
132
  params = Reparameterize.call(method.values.first)
134
133
  params_pass = Reparameterize.call(method.values.first, true)
135
- klass.class_variable_set("@@klass_#{m}", eval_lambda(params, %Q[raise "::#{m} is not Implemented for Class: #{klass.name}"]))
136
- block = eval_lambda(params, %Q[ class_variable_get("@@klass_#{m}").call(#{params_pass})])
137
- klass.singleton_class.class_eval do
138
- define_method(m, block)
139
- end
140
- end
141
- end
142
134
 
143
- def eval_lambda(arguments, block)
144
- eval(%Q[ ->(#{arguments}){ #{block} }])
145
- end
135
+ klass.send(:model_class_methods)[m] = eval_lambda(params, %Q[raise "::#{m} is not Implemented for Class: #{klass.name}"])
146
136
 
147
- def add_mock_instance_method
148
- klass = create_klass
149
- klass.singleton_class.class_eval do
150
- define_method(:mock_instance_method) do |method, &block|
151
- klass.instance_variable_set("@instance_#{method.to_s}", block)
152
- end
153
- end
154
- klass.instance_eval do
155
- define_method(:mock_instance_method) do |method, &block|
156
- klass.instance_variable_set("@instance_#{method.to_s}", block)
157
- end
137
+ klass.class_eval <<-eos, __FILE__, __LINE__+1
138
+ def self.#{m}(#{params})
139
+ model_class_methods[#{m.inspect}].call(#{params_pass})
140
+ end
141
+ eos
158
142
  end
159
143
  end
160
144
 
161
- def add_mock_class_method
162
- klass = create_klass
163
- klass.singleton_class.class_eval do
164
- define_method(:mock_class_method) do |method, &block|
165
- class_variable_set("@@klass_#{method.to_s}", block)
166
- end
167
- end
145
+ def eval_lambda(arguments, block)
146
+ eval(%Q[ ->(#{arguments}){ #{block} }])
168
147
  end
169
148
 
170
149
  def add_column_names_method
@@ -183,8 +162,11 @@ module ActiveMocker
183
162
 
184
163
  def const_class
185
164
  remove_const(mock_class_name) if class_exists? mock_class_name
186
- return Object.const_set(mock_class_name ,Class.new(ActiveHash::Base)) if active_hash_as_base
187
- return Object.const_set(mock_class_name ,Class.new()) unless active_hash_as_base
165
+ klass = Object.const_set(mock_class_name ,Class.new(ActiveHash::Base)) if active_hash_as_base
166
+ klass = Object.const_set(mock_class_name ,Class.new()) unless active_hash_as_base
167
+ klass.extend ModelClassMethods
168
+ klass.include ModelInstanceMethods
169
+ klass
188
170
  end
189
171
 
190
172
  def remove_const(class_name)
@@ -204,5 +186,42 @@ module ActiveMocker
204
186
 
205
187
  end
206
188
 
189
+ module ModelInstanceMethods
190
+
191
+ def mock_instance_method(method, &block)
192
+ model_instance_methods[method] = block
193
+ end
194
+
195
+ private
196
+
197
+ def model_instance_methods
198
+ @model_instance_methods ||= self.class.send(:model_methods_template).dup
199
+ end
200
+
201
+ end
202
+
203
+ module ModelClassMethods
204
+
205
+ def mock_instance_method(method, &block)
206
+ model_methods_template[method] = block
207
+ end
208
+
209
+ def mock_class_method(method, &block)
210
+ model_class_methods[method] = block
211
+ end
212
+
213
+ private
214
+
215
+ def model_class_methods
216
+ @model_class_methods ||= HashWithIndifferentAccess.new
217
+ end
218
+
219
+ def model_methods_template
220
+ @model_methods_template ||= HashWithIndifferentAccess.new
221
+ end
222
+
223
+ end
224
+
225
+
207
226
  end
208
227
 
@@ -14,19 +14,25 @@ module ActiveMocker
14
14
  :model_file_reader
15
15
 
16
16
  def config
17
- default
17
+ @@first_load ||= reload_default
18
18
  yield self
19
+ check_required_settings
19
20
  end
20
21
 
21
- def default
22
- schema_file = nil
23
- model_dir = nil
24
- active_hash_as_base = false
25
- schema_attributes = true
26
- model_relationships = true
27
- model_methods = true
28
- mass_assignment = true
29
- log_level = Logger::WARN
22
+ def reload_default
23
+ @schema_file = nil
24
+ @model_dir = nil
25
+ @active_hash_as_base = false
26
+ @schema_attributes = true
27
+ @model_relationships = true
28
+ @model_methods = true
29
+ @mass_assignment = true
30
+ @log_level = Logger::WARN
31
+ end
32
+
33
+ def check_required_settings
34
+ raise 'schema_file must be specified'if schema_file.nil?
35
+ raise 'model_dir must be specified'if model_dir.nil?
30
36
  end
31
37
 
32
38
  def log_level=(level)
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -21,23 +21,28 @@ describe ActiveMocker::Base do
21
21
  before(:each) do
22
22
 
23
23
  ActiveMocker::Base.configure do |config|
24
+ # Required Options
24
25
  config.schema_file = 'file is being inject as string'
25
26
  config.model_dir = 'file is being inject as string'
26
27
  # Dependency injection
27
28
  config.schema_file_reader = schema_file
28
29
  config.model_file_reader = model_file
29
30
  # Additional Options
30
- config.active_hash_as_base = false #default
31
- config.schema_attributes = true #default
32
- config.model_relationships = true #default
33
- config.model_methods = true #default
34
- config.mass_assignment = true #default
31
+ #config.active_hash_as_base = false #default
32
+ #config.schema_attributes = true #default
33
+ #config.model_relationships = true #default
34
+ #config.model_methods = true #default
35
+ #config.mass_assignment = true #default
35
36
  # Logging
36
37
  config.log_level = Logger::WARN #default
37
38
  end
38
39
 
39
40
  end
40
41
 
42
+ after(:each) do
43
+ ActiveMocker::Base.reload_default
44
+ end
45
+
41
46
  let(:model_file){
42
47
  StringReader.new <<-eos
43
48
  class Person < ActiveRecord::Base
@@ -248,17 +253,7 @@ describe ActiveMocker::Base do
248
253
  before(:each) do
249
254
 
250
255
  ActiveMocker::Base.configure do |config|
251
- config.schema_file = 'file is being inject as string'
252
- config.model_dir = 'file is being inject as string'
253
- # Depenency injection
254
- config.schema_file_reader = schema_file
255
- config.model_file_reader = model_file
256
- # Additional Options
257
256
  config.active_hash_as_base = true
258
- config.schema_attributes = false
259
- config.model_relationships = false
260
- config.model_methods = true
261
- config.mass_assignment = false
262
257
  end
263
258
 
264
259
  end
@@ -306,15 +301,6 @@ describe ActiveMocker::Base do
306
301
  expect{mock_class.new.bar}.to raise_error '#bar is not Implemented for Class: PersonMock'
307
302
  end
308
303
 
309
- it 'instance method can be mocked' do
310
-
311
- mock_class.instance_variable_set(:@instance_bar, ->(){ "Now implemented" })
312
- result = mock_class.new
313
- result = result.bar
314
- expect(result).to eq "Now implemented"
315
-
316
- end
317
-
318
304
  end
319
305
 
320
306
  describe 'false' do
@@ -329,4 +315,28 @@ describe ActiveMocker::Base do
329
315
 
330
316
  end
331
317
 
318
+ describe '::configure' do
319
+
320
+ it 'requires schema_file' do
321
+ ActiveMocker::Base.reload_default
322
+ expect{
323
+ ActiveMocker::Base.configure {
324
+ }
325
+ }.to raise_error
326
+
327
+ end
328
+
329
+
330
+ it 'requires model_dir' do
331
+ ActiveMocker::Base.reload_default
332
+ expect{
333
+ ActiveMocker::Base.configure { |c|
334
+ c.schema_file = 'dir'
335
+ }
336
+ }.to raise_error
337
+
338
+ end
339
+
340
+ end
341
+
332
342
  end
@@ -1,5 +1,7 @@
1
1
  require 'rspec'
2
2
  $:.unshift File.expand_path('../../', __FILE__)
3
+ require 'logger'
4
+ require 'active_mocker/logger'
3
5
  require 'string_reader'
4
6
  require 'file_reader'
5
7
  require 'active_mocker/logger'
@@ -24,7 +26,7 @@ describe ActiveMocker::ModelReader do
24
26
  describe '#class_methods' do
25
27
 
26
28
  it 'returns all public class methods' do
27
- expect(subject.class_methods).to eq([:duper, :named, :foo])
29
+ expect(subject.class_methods).to eq([:duper, :named, :foo, :bang!])
28
30
  end
29
31
 
30
32
  end
@@ -48,7 +50,7 @@ describe ActiveMocker::ModelReader do
48
50
  describe '#class_methods_with_arguments' do
49
51
 
50
52
  it 'returns all public instance methods' do
51
- expect(subject.class_methods_with_arguments).to eq( [{:duper=>[[:req, :value], [:rest, :args]]}, {:named=>[[:req, :name], [:opt, :value], [:opt, :options]]}, {:foo=>[]}] )
53
+ expect(subject.class_methods_with_arguments).to eq( [{:duper=>[[:req, :value], [:rest, :args]]}, {:named=>[[:req, :name], [:opt, :value], [:opt, :options]]}, {:foo=>[]}, {:bang! =>[]}])
52
54
  end
53
55
 
54
56
  end
@@ -25,6 +25,10 @@ class Model < ActiveRecord::Base
25
25
 
26
26
  end
27
27
 
28
+ def self.bang!
29
+
30
+ end
31
+
28
32
  private
29
33
 
30
34
  def bar
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +80,6 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0.6'
69
- - !ruby/object:Gem::Dependency
70
- name: activesupport
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '4.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '4.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: active_hash
85
85
  requirement: !ruby/object:Gem::Requirement