notahat-machinist 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/machinist.rb +8 -74
- metadata +1 -1
data/lib/machinist.rb
CHANGED
@@ -1,71 +1,13 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'active_record'
|
3
3
|
require 'sham'
|
4
|
+
require 'machinist/active_record'
|
4
5
|
|
5
6
|
module Machinist
|
6
|
-
def self.with_save_nerfed
|
7
|
-
begin
|
8
|
-
@@nerfed = true
|
9
|
-
yield
|
10
|
-
ensure
|
11
|
-
@@nerfed = false
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
@@nerfed = false
|
16
|
-
def self.nerfed?
|
17
|
-
@@nerfed
|
18
|
-
end
|
19
|
-
|
20
|
-
module ActiveRecordExtensions
|
21
|
-
def self.included(base)
|
22
|
-
base.extend(ClassMethods)
|
23
|
-
end
|
24
|
-
|
25
|
-
module ClassMethods
|
26
|
-
def blueprint(&blueprint)
|
27
|
-
@blueprint = blueprint if block_given?
|
28
|
-
@blueprint
|
29
|
-
end
|
30
|
-
|
31
|
-
def make(attributes = {}, &block)
|
32
|
-
lathe = Lathe.run(self.new, attributes)
|
33
|
-
unless Machinist.nerfed?
|
34
|
-
lathe.object.save!
|
35
|
-
lathe.object.reload
|
36
|
-
end
|
37
|
-
lathe.object(&block)
|
38
|
-
end
|
39
|
-
|
40
|
-
def plan(attributes = {})
|
41
|
-
lathe = Lathe.run(self.new, attributes)
|
42
|
-
lathe.assigned_attributes
|
43
|
-
end
|
44
|
-
|
45
|
-
def make_unsaved(attributes = {})
|
46
|
-
returning(Machinist.with_save_nerfed { make(attributes) }) do |object|
|
47
|
-
yield object if block_given?
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
module ActiveRecordAssociationExtensions
|
54
|
-
def make(attributes = {}, &block)
|
55
|
-
lathe = Machinist::Lathe.run(self.build, attributes)
|
56
|
-
unless Machinist.nerfed?
|
57
|
-
lathe.object.save!
|
58
|
-
lathe.object.reload
|
59
|
-
end
|
60
|
-
lathe.object(&block)
|
61
|
-
end
|
62
7
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
8
|
+
# A Lathe is used to execute the blueprint and construct an object.
|
9
|
+
#
|
10
|
+
# The blueprint is instance_eval'd against the Lathe.
|
69
11
|
class Lathe
|
70
12
|
def self.run(object, attributes = {})
|
71
13
|
blueprint = object.class.blueprint
|
@@ -106,25 +48,17 @@ module Machinist
|
|
106
48
|
end
|
107
49
|
end
|
108
50
|
|
109
|
-
def generate_attribute(
|
51
|
+
def generate_attribute(attribute, args)
|
110
52
|
value = if block_given?
|
111
53
|
yield
|
112
|
-
elsif args.
|
113
|
-
klass = @object.class.reflect_on_association(
|
54
|
+
elsif args.empty?
|
55
|
+
klass = @object.class.reflect_on_association(attribute).class_name.constantize
|
114
56
|
klass.make(args.first || {})
|
115
57
|
else
|
116
58
|
args.first
|
117
59
|
end
|
118
|
-
@assigned_attributes[
|
60
|
+
@assigned_attributes[attribute] = value
|
119
61
|
end
|
120
62
|
|
121
63
|
end
|
122
64
|
end
|
123
|
-
|
124
|
-
class ActiveRecord::Base
|
125
|
-
include Machinist::ActiveRecordExtensions
|
126
|
-
end
|
127
|
-
|
128
|
-
class ActiveRecord::Associations::AssociationProxy
|
129
|
-
include Machinist::ActiveRecordAssociationExtensions
|
130
|
-
end
|