Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved the ActiveRecord extensions into their own model, to avoid poll…

…uting the ActiveRecord namespace any more than we need to.
  • Loading branch information...
commit 6c004dde4d110cf8d14565b5b8d9592cf02afbb2 1 parent 6b03bf0
@notahat notahat authored
Showing with 37 additions and 31 deletions.
  1. +1 −1  init.rb
  2. +35 −29 lib/machinist.rb
  3. +1 −1  spec/machinist_spec.rb
View
2  init.rb
@@ -5,6 +5,6 @@
require 'sham'
class ActiveRecord::Base
- include Machinist
+ include Machinist::ActiveRecordExtensions
end
end
View
64 lib/machinist.rb
@@ -1,41 +1,47 @@
require 'active_support'
module Machinist
- def self.included(base)
- base.extend(ClassMethods)
- base.cattr_accessor :nerfed
- end
-
- module ClassMethods
- def blueprint(&blueprint)
- @blueprint = blueprint
+ def self.with_save_nerfed
+ begin
+ @@nerfed = true
+ yield
+ ensure
+ @@nerfed = false
end
+ end
- def make(attributes = {})
- raise "No blueprint for class #{self}" if @blueprint.nil?
- lathe = Lathe.new(self.new, attributes)
- lathe.instance_eval(&@blueprint)
- unless nerfed
- lathe.object.save!
- lathe.object.reload
- end
- returning(lathe.object) do |object|
- yield object if block_given?
- end
+ @@nerfed = false
+ def self.nerfed?
+ @@nerfed
+ end
+
+ module ActiveRecordExtensions
+ def self.included(base)
+ base.extend(ClassMethods)
end
- def make_unsaved(attributes = {})
- returning(with_save_nerfed { make(attributes) }) do |object|
- yield object if block_given?
+ module ClassMethods
+ def blueprint(&blueprint)
+ @blueprint = blueprint
+ end
+
+ def make(attributes = {})
+ raise "No blueprint for class #{self}" if @blueprint.nil?
+ lathe = Lathe.new(self.new, attributes)
+ lathe.instance_eval(&@blueprint)
+ unless Machinist.nerfed?
+ lathe.object.save!
+ lathe.object.reload
+ end
+ returning(lathe.object) do |object|
+ yield object if block_given?
+ end
end
- end
- def with_save_nerfed
- begin
- self.nerfed = true
- yield
- ensure
- self.nerfed = false
+ def make_unsaved(attributes = {})
+ returning(Machinist.with_save_nerfed { make(attributes) }) do |object|
+ yield object if block_given?
+ end
end
end
end
View
2  spec/machinist_spec.rb
@@ -2,7 +2,7 @@
require 'machinist'
class Base
- include Machinist
+ include Machinist::ActiveRecordExtensions
def save!; @saved = true; end
def reload; @reloaded = true; self; end
Please sign in to comment.
Something went wrong with that request. Please try again.