factories 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49462fc87b70c002eec2a5bf71e8a2392da86996
4
- data.tar.gz: bccbaf079b5b0dc8737f321857c6df240238d9ba
3
+ metadata.gz: 577970216f6b30979e8c42ea55938faf27e10f77
4
+ data.tar.gz: c815d216346373943ef008780a7b271e973dc62d
5
5
  SHA512:
6
- metadata.gz: 31361b6aff21b1f452a8d6ffbc691d5b804ba2147e1f1f97ab397ae5cada9b19bccc39908be44aa0dab3d53403b9038da07868b86661218d4d800d0eca7a45fc
7
- data.tar.gz: db6ac7360322730335a5d79d0cf9f368cb8fb957f7140ceb6d99236f93cf11b7ac777e7155a14545b39aad68aeeab52453a1fd9fe8e0ad2d7942e0a9041ef045
6
+ metadata.gz: 1b37ea3cbeae780d09ac5047e93f1185de6c5265c5fcfd51c1ae54d8aa085ba654e4cd57f7076382097d8de1de080afdb0f70d9e2c59196df6f194f434dd1470
7
+ data.tar.gz: de1b4c1da9aefff43879aca225009a250ec376636832664241682bfcfcb1b6bbe1f5d07a1c1cca0b9d1d75ae6ccc7a244d2ad50215e5f97023c3f715b3d011eb
data/README.md CHANGED
@@ -25,11 +25,9 @@ class User < ActiveRecord::Base
25
25
  end
26
26
 
27
27
  Factories.gen :user do
28
- def defaults
29
- {
30
- name: "Nathan"
31
- }
32
- end
28
+ defaults {{
29
+ name: "Nathan"
30
+ }}
33
31
  end
34
32
 
35
33
  user = Factories.build(:user)
@@ -88,3 +86,62 @@ describe User do
88
86
  end
89
87
  end
90
88
  ```
89
+
90
+ ### Procs
91
+
92
+ To delay execution time and get a hold of the model instance a proc can
93
+ be provided. An example that shows a problem this might solve are
94
+ circular `belong_to`s:
95
+
96
+ ```ruby
97
+ class Account
98
+ belongs_to :owner, class_name: 'User'
99
+ end
100
+
101
+ class User
102
+ belongs_to :account
103
+ end
104
+ ```
105
+
106
+ This can be expressed in factories by delaying the default values with procs:
107
+
108
+ ```ruby
109
+ Factories.gen :user do
110
+ defaults {{
111
+ account: ->(m) { Factories.build(:account, owner: m) },
112
+ name: Faker::Name.name,
113
+ email: Faker::Internet.email
114
+ }}
115
+ end
116
+
117
+ Factories.gen :account do
118
+ defaults {{
119
+ owner: ->(m) { Factories.build(:user, account: m) },
120
+ name: Faker::Company.name
121
+ }}
122
+ end
123
+
124
+ Factories.create(:account) # => #<Account id: ...>
125
+ Factories.create(:user) # => #<User id: ...>
126
+ ```
127
+
128
+ This works because when creating an account the `owner` field's default
129
+ is overriden inside the lambda value for the account of the user. The
130
+ same is true when creating a user: the owner for the account is
131
+ overriden inside the lambda value for the account. Yeah.
132
+
133
+ ### Override magic class name
134
+
135
+ You can provide your own class to create as many factories as you like:
136
+
137
+ ```ruby
138
+ Factorie.gen :owner do
139
+ model_class User
140
+
141
+ defaults {{
142
+ name: '...'
143
+ }}
144
+ end
145
+
146
+ Factories.create(:owner) # => #<User id: ...>
147
+ ```
@@ -10,8 +10,12 @@ module Factories
10
10
  new(opts).build
11
11
  end
12
12
 
13
- def self.model_class
14
- Inflecto.constantize(Inflecto.demodulize(name.gsub(/Factory$/, '')))
13
+ def self.model_class(klass = nil)
14
+ if klass
15
+ @model_class = klass
16
+ else
17
+ @model_class ||= Inflecto.constantize(Inflecto.demodulize(name.gsub(/Factory$/, '')))
18
+ end
15
19
  end
16
20
 
17
21
  def model_class
@@ -22,6 +26,10 @@ module Factories
22
26
  raise "not implimented"
23
27
  end
24
28
 
29
+ def self.defaults(&blk)
30
+ define_method(:defaults, &blk)
31
+ end
32
+
25
33
  def initialize(opts = {})
26
34
  @opts = opts
27
35
  @attributes = defaults.merge(opts)
@@ -32,7 +40,28 @@ module Factories
32
40
  end
33
41
 
34
42
  def build
35
- model_class.new(@attributes)
43
+ model_class.new.tap do |instance|
44
+ attrs = expand_procs @attributes, instance
45
+ instance.assign_attributes attrs
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def expand_procs(hash, instance)
52
+ hash.each_with_object({}) do |(k, v), h|
53
+ v = if v.is_a?(Proc)
54
+ if v.arity == 1
55
+ v.call(instance)
56
+ else
57
+ v.call
58
+ end
59
+ else
60
+ v
61
+ end
62
+
63
+ h[k] = v
64
+ end
36
65
  end
37
66
  end
38
67
  end
@@ -1,3 +1,3 @@
1
1
  module Factories
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factories
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Herald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-02 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler