notahat-machinist 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/machinist.rb +8 -74
  2. 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
- def plan(attributes = {})
64
- lathe = Machinist::Lathe.run(self.build, attributes)
65
- lathe.assigned_attributes
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(symbol, args)
51
+ def generate_attribute(attribute, args)
110
52
  value = if block_given?
111
53
  yield
112
- elsif args.first.is_a?(Hash) || args.empty?
113
- klass = @object.class.reflect_on_association(symbol).class_name.constantize
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[symbol] = value
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
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.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell