notahat-machinist 0.1.7 → 0.2.0

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 (3) hide show
  1. data/lib/machinist.rb +69 -25
  2. data/lib/sham.rb +1 -7
  3. metadata +2 -2
data/lib/machinist.rb CHANGED
@@ -24,22 +24,24 @@ module Machinist
24
24
 
25
25
  module ClassMethods
26
26
  def blueprint(&blueprint)
27
- @blueprint = blueprint
27
+ @blueprint = blueprint if block_given?
28
+ @blueprint
28
29
  end
29
30
 
30
- def make(attributes = {})
31
- raise "No blueprint for class #{self}" if @blueprint.nil?
32
- lathe = Lathe.new(self, attributes)
33
- lathe.instance_eval(&@blueprint)
31
+ def make(attributes = {}, &block)
32
+ lathe = Lathe.run(self.new, attributes)
34
33
  unless Machinist.nerfed?
35
34
  lathe.object.save!
36
35
  lathe.object.reload
37
36
  end
38
- returning(lathe.object) do |object|
39
- yield object if block_given?
40
- end
37
+ lathe.object(&block)
41
38
  end
42
-
39
+
40
+ def plan(attributes = {})
41
+ lathe = Lathe.run(self.new, attributes)
42
+ lathe.assigned_attributes
43
+ end
44
+
43
45
  def make_unsaved(attributes = {})
44
46
  returning(Machinist.with_save_nerfed { make(attributes) }) do |object|
45
47
  yield object if block_given?
@@ -48,11 +50,38 @@ module Machinist
48
50
  end
49
51
  end
50
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
+
63
+ def plan(attributes = {})
64
+ lathe = Machinist::Lathe.run(self.build, attributes)
65
+ lathe.assigned_attributes
66
+ end
67
+ end
68
+
51
69
  class Lathe
52
- def initialize(klass, attributes = {})
53
- @object = klass.new
54
- attributes.each {|key, value| @object.send("#{key}=", value) }
55
- @assigned_attributes = attributes.keys.map(&:to_sym)
70
+ def self.run(object, attributes = {})
71
+ blueprint = object.class.blueprint
72
+ raise "No blueprint for class #{object.class}" if blueprint.nil?
73
+ returning self.new(object, attributes) do |lathe|
74
+ lathe.instance_eval(&blueprint)
75
+ end
76
+ end
77
+
78
+ def initialize(object, attributes = {})
79
+ @object = object
80
+ @assigned_attributes = {}
81
+ attributes.each do |key, value|
82
+ @object.send("#{key}=", value)
83
+ @assigned_attributes[key.to_sym] = value
84
+ end
56
85
  end
57
86
 
58
87
  # Undef a couple of methods that are common ActiveRecord attributes.
@@ -60,23 +89,35 @@ module Machinist
60
89
  undef_method :id
61
90
  undef_method :type
62
91
 
63
- attr_reader :object
64
-
92
+ def object
93
+ yield @object if block_given?
94
+ @object
95
+ end
96
+
97
+ attr_reader :assigned_attributes
98
+
65
99
  def method_missing(symbol, *args, &block)
66
- if @assigned_attributes.include?(symbol)
100
+ if @assigned_attributes.has_key?(symbol)
101
+ @object.send(symbol)
102
+ elsif @object.class.reflect_on_association(symbol) && !@object.send(symbol).nil?
67
103
  @object.send(symbol)
68
104
  else
69
- value = if block
70
- block.call
71
- elsif args.first.is_a?(Hash) || args.empty?
72
- symbol.to_s.camelize.constantize.make(args.first || {})
73
- else
74
- args.first
75
- end
76
- @object.send("#{symbol}=", value)
77
- @assigned_attributes << symbol
105
+ @object.send("#{symbol}=", generate_attribute(symbol, args, &block))
78
106
  end
79
107
  end
108
+
109
+ def generate_attribute(symbol, args)
110
+ value = if block_given?
111
+ yield
112
+ elsif args.first.is_a?(Hash) || args.empty?
113
+ klass = @object.class.reflect_on_association(symbol).class_name.constantize
114
+ klass.make(args.first || {})
115
+ else
116
+ args.first
117
+ end
118
+ @assigned_attributes[symbol] = value
119
+ end
120
+
80
121
  end
81
122
  end
82
123
 
@@ -84,3 +125,6 @@ class ActiveRecord::Base
84
125
  include Machinist::ActiveRecordExtensions
85
126
  end
86
127
 
128
+ class ActiveRecord::Associations::AssociationProxy
129
+ include Machinist::ActiveRecordAssociationExtensions
130
+ end
data/lib/sham.rb CHANGED
@@ -25,13 +25,7 @@ class Sham
25
25
  end
26
26
 
27
27
  def self.define(&block)
28
- definer = Object.new
29
- class << definer
30
- def method_missing(*args, &block)
31
- Sham.send(*args, &block)
32
- end
33
- end
34
- definer.instance_eval(&block)
28
+ Sham.instance_eval(&block)
35
29
  end
36
30
 
37
31
  def initialize(name, options = {}, &block)
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.1.7
4
+ version: 0.2.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: 2008-12-22 00:00:00 -08:00
12
+ date: 2009-02-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15