modelizer 5.0.1 → 5.0.2

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/CHANGELOG.rdoc +4 -0
  2. data/lib/modelizer.rb +42 -29
  3. metadata +4 -4
@@ -1,3 +1,7 @@
1
+ === 5.0.2 // 2011-09-15
2
+
3
+ * Support circular factory initialization.
4
+
1
5
  === 5.0.1 // 2011-09-14
2
6
 
3
7
  * Include fixture name and errors when fixtures can't be saved.
@@ -2,52 +2,47 @@ require "zlib"
2
2
 
3
3
  module Modelizer
4
4
 
5
- VERSION = "5.0.1"
5
+ VERSION = "5.0.2"
6
6
 
7
- def build name, overrides = nil, &block
8
- model, *initializers = Modelizer.factories[name]
9
- raise "Can't find the \"#{name}\" factory." unless model
7
+ module Helpers
8
+ def build name, overrides = nil, &block
9
+ model, *initializers = Modelizer.factories[name]
10
+ raise "Can't find the \"#{name}\" factory." unless model
10
11
 
11
- obj = model.new
12
+ obj = model.new
13
+ ctx = FactoryContext.new overrides || {}
12
14
 
13
- initializers << block if block_given?
14
- initializers.each { |i| instance_exec obj, &i }
15
+ initializers << block if block_given?
15
16
 
16
- overrides.each { |k, v| obj.send "#{k}=", v } if overrides
17
-
18
- obj
19
- end
20
-
21
- def create name, overrides = nil, &block
22
- obj = build name, overrides, &block
23
-
24
- obj.save!
25
-
26
- obj
27
- end
17
+ initializers.each { |i| ctx.instance_exec obj, &i }
18
+ overrides.each { |k, v| obj.send "#{k}=", v } if overrides
19
+
20
+ obj
21
+ end
28
22
 
29
- def use name
30
- model, id = Modelizer.ids[name]
31
- raise "Can't find the \"#{name}\" fixture." unless model
23
+ def create name, overrides = nil, &block
24
+ obj = build name, overrides, &block
32
25
 
33
- model.find id
34
- end
26
+ obj.save!
35
27
 
36
- class Context < Struct.new(:instances)
37
- def identify name
38
- Modelizer.identify name
28
+ obj
39
29
  end
40
30
 
41
31
  def use name
42
- instances[name] or raise "Can't find the \"#{name}\" fixture."
32
+ model, id = Modelizer.ids[name]
33
+ raise "Can't find the \"#{name}\" fixture." unless model
34
+
35
+ model.find id
43
36
  end
44
37
  end
45
38
 
39
+ include Helpers
40
+
46
41
  def self.included klass
47
42
  Dir[glob].sort.each { |f| instance_eval File.read(f), f, 1 }
48
43
 
49
44
  instances = {}
50
- context = Context.new instances
45
+ context = FixtureContext.new instances
51
46
 
52
47
  fixtures.each do |name, value|
53
48
  instances[name] = value.first.new
@@ -103,4 +98,22 @@ module Modelizer
103
98
  def self.ids
104
99
  @ids ||= {}
105
100
  end
101
+
102
+ class FactoryContext < Struct.new(:overrides)
103
+ include Helpers
104
+
105
+ def build name, *args, &block
106
+ self.overrides[name] || super
107
+ end
108
+ end
109
+
110
+ class FixtureContext < Struct.new(:instances)
111
+ def identify name
112
+ Modelizer.identify name
113
+ end
114
+
115
+ def use name
116
+ instances[name] or raise "Can't find the \"#{name}\" fixture."
117
+ end
118
+ end
106
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modelizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-14 00:00:00.000000000Z
12
+ date: 2011-09-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hoe
16
- requirement: &70313184032840 !ruby/object:Gem::Requirement
16
+ requirement: &70187039960200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.12'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70313184032840
24
+ version_requirements: *70187039960200
25
25
  description: ! 'Need a simple, consistent way to create model instances and check
26
26
 
27
27
  validations in your ActiveRecord 3.1+ tests? Use the Modelizer. Just