notahat-machinist 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,13 +51,14 @@ module Machinist
51
51
  end
52
52
 
53
53
  module ClassMethods
54
- def blueprint(&blueprint)
55
- @blueprint = blueprint if block_given?
56
- @blueprint
54
+ def blueprint(name = :master, &blueprint)
55
+ @blueprints ||= {}
56
+ @blueprints[name] = blueprint if block_given?
57
+ @blueprints[name]
57
58
  end
58
59
 
59
- def make(attributes = {}, &block)
60
- lathe = Lathe.run(self.new, attributes)
60
+ def make(*args, &block)
61
+ lathe = Lathe.run(self.new, *args)
61
62
  unless Machinist::ActiveRecord.nerfed?
62
63
  lathe.object.save!
63
64
  lathe.object.reload
@@ -65,22 +66,22 @@ module Machinist
65
66
  lathe.object(&block)
66
67
  end
67
68
 
68
- def make_unsaved(attributes = {})
69
- returning(Machinist::ActiveRecord.with_save_nerfed { make(attributes) }) do |object|
69
+ def make_unsaved(*args)
70
+ returning(Machinist::ActiveRecord.with_save_nerfed { make(*args) }) do |object|
70
71
  yield object if block_given?
71
72
  end
72
73
  end
73
74
 
74
- def plan(attributes = {})
75
- lathe = Lathe.run(self.new, attributes)
75
+ def plan(*args)
76
+ lathe = Lathe.run(self.new, *args)
76
77
  Machinist::ActiveRecord.assigned_attributes_without_associations(lathe)
77
78
  end
78
79
  end
79
80
  end
80
81
 
81
82
  module BelongsToExtensions
82
- def make(attributes = {}, &block)
83
- lathe = Lathe.run(self.build, attributes)
83
+ def make(*args, &block)
84
+ lathe = Lathe.run(self.build, *args)
84
85
  unless Machinist::ActiveRecord.nerfed?
85
86
  lathe.object.save!
86
87
  lathe.object.reload
@@ -88,8 +89,8 @@ module Machinist
88
89
  lathe.object(&block)
89
90
  end
90
91
 
91
- def plan(attributes = {})
92
- lathe = Lathe.run(self.build, attributes)
92
+ def plan(*args)
93
+ lathe = Lathe.run(self.build, *args)
93
94
  Machinist::ActiveRecord.assigned_attributes_without_associations(lathe)
94
95
  end
95
96
  end
data/lib/machinist.rb CHANGED
@@ -9,10 +9,13 @@ module Machinist
9
9
  #
10
10
  # The blueprint is instance_eval'd against the Lathe.
11
11
  class Lathe
12
- def self.run(object, attributes = {})
13
- blueprint = object.class.blueprint
12
+ def self.run(object, *args)
13
+ blueprint = object.class.blueprint
14
+ named_blueprint = object.class.blueprint(args.shift) if args.first.is_a?(Symbol)
15
+ attributes = args.pop || {}
14
16
  raise "No blueprint for class #{object.class}" if blueprint.nil?
15
17
  returning self.new(object, attributes) do |lathe|
18
+ lathe.instance_eval(&named_blueprint) if named_blueprint
16
19
  lathe.instance_eval(&blueprint)
17
20
  end
18
21
  end
@@ -52,8 +55,12 @@ module Machinist
52
55
  value = if block_given?
53
56
  yield
54
57
  elsif args.empty?
55
- klass = @object.class.reflect_on_association(attribute).class_name.constantize
56
- klass.make(args.first || {})
58
+ association = @object.class.reflect_on_association(attribute)
59
+ if association
60
+ association.class_name.constantize.make(args.first || {})
61
+ else
62
+ Sham.send(attribute)
63
+ end
57
64
  else
58
65
  args.first
59
66
  end
data/lib/sham.rb CHANGED
@@ -19,6 +19,10 @@ class Sham
19
19
  sham.fetch_value
20
20
  end
21
21
  end
22
+
23
+ def self.clear
24
+ @@shams = {}
25
+ end
22
26
 
23
27
  def self.reset
24
28
  @@shams.values.each(&:reset)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notahat-machinist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-05 00:00:00 -08:00
12
+ date: 2009-02-13 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15