Skip to content
This repository

Added ability to set a base serial number #115

Open
wants to merge 2 commits into from

1 participant

Austin Putman
Austin Putman

This is for using machinist alongside pre-existing fixtures, for example with the gem FixtureBuilder https://github.com/rdy/fixture_builder.

In this case, fixtures (including serial numbers) may be cached and re-used during multiple test runs. Blueprinted models created during the run where cached fixtures are used should not have the same serial numbers as those stored in the fixture files. A global base serial number value can resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jun 22, 2012
Austin Putman Added support for user-definable starting value for serial numbers ac…
…ross all models
3a2d52f
Austin Putman Use a class variable, not a class instance variable, to ensure inheri…
…tance to AR-based models
ab5db1a
This page is out of date. Refresh to see the latest.
13 lib/machinist/blueprint.rb
@@ -2,6 +2,7 @@ module Machinist
2 2
3 3 # A Blueprint defines a method of constructing objects of a particular class.
4 4 class Blueprint
  5 + @@serial_number_base = 0
5 6
6 7 # Construct a blueprint for the given +klass+.
7 8 #
@@ -66,13 +67,21 @@ def new_serial_number #:nodoc:
66 67 if parent_blueprint
67 68 parent_blueprint.new_serial_number
68 69 else
69   - @serial_number ||= 0
  70 + @serial_number ||= self.class.serial_number_base
70 71 @serial_number += 1
71 72 sprintf("%04d", @serial_number)
72 73 end
73 74 end
74 75
75   - private
  76 + def self.serial_number_base
  77 + @@serial_number_base || 0
  78 + end
  79 +
  80 + def self.serial_number_base=(value)
  81 + @@serial_number_base = value || 0
  82 + end
  83 +
  84 + private
76 85
77 86 def find_blueprint_in_superclass_chain(klass)
78 87 until has_blueprint?(klass) || klass.nil?
13 spec/active_record_spec.rb
@@ -94,6 +94,19 @@
94 94 post.author.should_not be_new_record
95 95 post.author.username.should =~ /^post_author_\d+$/
96 96 end
  97 +
  98 + it "handles serial number base override" do
  99 + User.blueprint do
  100 + username { "user_#{sn}" }
  101 + end
  102 + Machinist::Blueprint.serial_number_base = 10000
  103 +
  104 + User.make!.username.should == "user_10001"
  105 + User.make!.username.should == "user_10002"
  106 + Machinist::Blueprint.serial_number_base = 500
  107 + User.make!.username.should == "user_10003"
  108 + Machinist::Blueprint.serial_number_base = nil
  109 + end
97 110 end
98 111
99 112 context "error handling" do
10 spec/blueprint_spec.rb
@@ -43,6 +43,16 @@
43 43 blueprint.make.name.should == "Fred 0002"
44 44 end
45 45
  46 + it "allows serial number start value to be overridden" do
  47 + Machinist::Blueprint.serial_number_base = 1000
  48 + blueprint = Machinist::Blueprint.new(OpenStruct) do
  49 + name { "Fred #{sn}" }
  50 + end
  51 + blueprint.make.name.should == "Fred 1001"
  52 + blueprint.make.name.should == "Fred 1002"
  53 + Machinist::Blueprint.serial_number_base = nil
  54 + end
  55 +
46 56 it "provides access to the object being constructed within the blueprint" do
47 57 blueprint = Machinist::Blueprint.new(OpenStruct) do
48 58 title { "Test" }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.