moonmaster9000-dupe 0.2.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +10 -0
  2. data/lib/dupe/dupe.rb +38 -29
  3. metadata +2 -2
data/README.rdoc CHANGED
@@ -5,8 +5,18 @@ But what about prototyping ActiveResource records? That's where Dupe steps in.
5
5
 
6
6
  == Installation
7
7
 
8
+ First, install the gem:
9
+
8
10
  # gem install moonmaster9000-dupe
9
11
 
12
+ Then add this to your cucumber.rb environment (config/environments/cucumber.rb)
13
+
14
+ config.gem 'moonmaster9000-dupe', :lib => 'dupe'
15
+
16
+ You might also want to add a particular version number if you want to make sure a particular feature is there:
17
+
18
+ config.gem 'moonmaster9000-dupe', :lib => 'dupe', :version => '>=0.3.0'
19
+
10
20
  == Example
11
21
  Let's suppose your cuking a book search application for a library that consumes a RESTFUL book datastore service via ActiveResource.
12
22
  You might start by writing the following feature in <em>RAILS_ROOT/features/library/find_book.feature</em>:
data/lib/dupe/dupe.rb CHANGED
@@ -173,42 +173,50 @@ class Dupe
173
173
 
174
174
  # You can use this method to quickly stub out a large number of resources. For example:
175
175
  #
176
- # Dupe.stub(
177
- # :author,
178
- # :template => {:name => 'author'},
179
- # :count => 20
180
- # )
176
+ # Dupe.stub 20, :authors
181
177
  #
182
- # would generate 20 author records like:
183
178
  #
184
- # {:name => 'author 1', :id => 1}
179
+ # Assuming you had an :author resource definition like:
180
+ #
181
+ # Dupe.define :author {|author| author.name('default')}
182
+ #
183
+ #
184
+ # the stub would have generated 20 author records like:
185
+ #
186
+ # {:name => 'default', :id => 1}
185
187
  # ....
186
- # {:name => 'author 20', :id => 20}
188
+ # {:name => 'default', :id => 20}
189
+ #
190
+ # and it would also have mocked find(id) and find(:all) responses for these records (along with any other custom mocks you've
191
+ # setup via Dupe.define_mocks). (Had you not defined an author resource, then the stub would have generated 20 author records
192
+ # where the only attribute is the id).
187
193
  #
188
- # and it would also mock find(id) and find(:all) responses for these records (along with any other custom mocks you've
189
- # setup via Dupe.define_mocks).
194
+ # Of course, it's more likely that you wanted to dupe 20 <em>different</em> authors. You can accomplish this by simply doing:
195
+ #
196
+ # Dupe.stub 20, :authors, :like => {:name => proc {|n| "author #{n}"}}
197
+ #
198
+ # which would generate 20 author records like:
199
+ #
200
+ # {:name => 'author 1', :id => 1}
201
+ # ....
202
+ # {:name => 'author 20', :id => 20}
190
203
  #
191
- # You may override both the sequence starting value and the attribute to sequence:
204
+ # You may also override the sequence starting value:
192
205
  #
193
- # Dupe.stub(
194
- # :book,
195
- # :template => {:author => 'Arthur C. Clarke', :title => 'moonmaster'},
196
- # :count => 20,
197
- # :sequence_start_value => 9000,
198
- # :sequence => :title
199
- # )
206
+ # Dupe.stub 20, :authors, :like => {:name => proc {|n| "author #{n}"}}, :starting_with => 150
200
207
  #
201
- # This would generate 20 book records like:
208
+ # This would generate 20 author records like:
202
209
  #
203
- # {:id => 1, :author => 'Arthur C. Clarke', :title => 'moonmaster 9000'}
210
+ # {:name => 'author 150', :id => 1}
204
211
  # ....
205
- # {:id => 20, :author => 'Arthur C. Clarke', :title => 'moonmaster 9019'}
212
+ # {:name => 'author 169', :id => 20}
206
213
  #
207
214
  # Naturally, stub will consult the Dupe.define definitions for anything it's attempting to stub
208
215
  # and will honor those definitions (default values, transformations) as you would expect.
209
- def stub(factory, options)
216
+ def stub(count, factory, options={})
217
+ factory = factory.to_s.singularize.to_sym
210
218
  setup_factory(factory)
211
- @factories[factory].stub_services_with(options[:template], options[:count].to_i, (options[:sequence_start_value] || 1), options[:sequence])
219
+ @factories[factory].stub_services_with((options[:like] || {}), count.to_i, (options[:starting_with] || 1))
212
220
  end
213
221
 
214
222
  # === Global Configuration
@@ -402,9 +410,8 @@ class Dupe
402
410
  ActiveResource::HttpMock.reset_from_dupe!
403
411
  end
404
412
 
405
- def stub_services_with(record_template, count=1, starting_value=1, sequence_attribute=nil) #:nodoc:
406
- sequence_attribute ||= record_template.keys.first
407
- records = stub_records(record_template, count, starting_value, sequence_attribute)
413
+ def stub_services_with(record_template, count=1, starting_value=1) #:nodoc:
414
+ records = stub_records(record_template, count, starting_value)
408
415
  generate_services_for(records, true)
409
416
  end
410
417
 
@@ -459,10 +466,12 @@ class Dupe
459
466
  keys.each {|k| define_attribute(k.to_sym) unless @attributes[k.to_sym]}
460
467
  end
461
468
 
462
- def stub_records(record_template, count, starting_value, sequence_attribute)
463
- overrides = record_template.merge({sequence_attribute => (record_template[sequence_attribute].to_s + starting_value.to_s), :id => sequence})
469
+ def stub_records(record_template, count, stub_number)
470
+ overrides = record_template.merge({})
471
+ overrides.keys.each {|k| overrides[k] = overrides[k].call(stub_number) if overrides[k].respond_to?(:call)}
472
+ overrides = {:id => sequence}.merge(overrides) unless overrides[:id]
464
473
  return [generate_record(overrides)] if count <= 1
465
- [generate_record(overrides)] + stub_records(record_template, count-1, starting_value+1, sequence_attribute)
474
+ [generate_record(overrides)] + stub_records(record_template, count-1, stub_number+1)
466
475
  end
467
476
 
468
477
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonmaster9000-dupe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Parker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-16 00:00:00 -07:00
12
+ date: 2009-09-17 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency