fabrication 2.7.1 → 2.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fabrication.rb +8 -3
- data/lib/fabrication/cucumber/step_fabricator.rb +1 -1
- data/lib/fabrication/fabricator.rb +3 -3
- data/lib/fabrication/generator/active_record.rb +13 -5
- data/lib/fabrication/generator/base.rb +20 -20
- data/lib/fabrication/generator/data_mapper.rb +3 -3
- data/lib/fabrication/generator/keymaker.rb +2 -2
- data/lib/fabrication/generator/mongoid.rb +3 -3
- data/lib/fabrication/generator/sequel.rb +9 -9
- data/lib/fabrication/schematic/definition.rb +3 -4
- data/lib/fabrication/support.rb +3 -3
- data/lib/fabrication/transform.rb +1 -1
- data/lib/fabrication/version.rb +1 -1
- metadata +2 -3
- data/lib/fabrication/generator/active_record_4.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19952424220acd0d916eafbecf996fecc9870bd6
|
4
|
+
data.tar.gz: 443657b59d5cabc84f349a2063882304cb0b7d74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe4bf4524f3c8a7cf1d0a331e03a09a26e54cc77198ad0ea2e0b87d57a95da9915288c0f6f7bd209f7beffea8ce75b0549cd058785989633ec37543865751e2f
|
7
|
+
data.tar.gz: efd348378470208dd2b23539e97210024b34a16ceb3bba0c5584340adb136aeddb0353014d1633b8e59cae3ae9ead87aa3e049ef62471adb2fa9692da00f65de
|
data/lib/fabrication.rb
CHANGED
@@ -31,7 +31,7 @@ module Fabrication
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.clear_definitions
|
34
|
-
@
|
34
|
+
@manager = nil
|
35
35
|
Sequencer.sequences.clear
|
36
36
|
end
|
37
37
|
|
@@ -39,13 +39,18 @@ module Fabrication
|
|
39
39
|
Fabrication::Config.configure(&block)
|
40
40
|
end
|
41
41
|
|
42
|
+
def self.manager
|
43
|
+
@manager ||= Fabrication::Schematic::Manager.new
|
44
|
+
end
|
45
|
+
|
42
46
|
def self.schematics
|
43
|
-
|
47
|
+
puts "DEPRECATION WARNING: Fabrication.schematics has been replaced by Fabrication.manager"
|
48
|
+
manager
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
47
52
|
def Fabricator(name, options={}, &block)
|
48
|
-
Fabrication.
|
53
|
+
Fabrication.manager.register(name, options, &block)
|
49
54
|
end
|
50
55
|
|
51
56
|
def Fabricate(name, overrides={}, &block)
|
@@ -54,7 +54,7 @@ module Fabrication
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def schematic
|
57
|
-
Fabrication.
|
57
|
+
Fabrication.manager[@fabricator].tap do |schematic|
|
58
58
|
raise Fabrication::UnknownFabricatorError, "No Fabricator defined for '#{@model}'" unless schematic
|
59
59
|
end
|
60
60
|
end
|
@@ -20,12 +20,12 @@ class Fabrication::Fabricator
|
|
20
20
|
def self.fail_if_initializing(name)
|
21
21
|
raise Fabrication::MisplacedFabricateError.new(
|
22
22
|
"You tried to fabricate `#{name}` while Fabricators were still loading. Check your fabricator files and make sure you didn't accidentally type `Fabricate` instead of `Fabricator` in there somewhere."
|
23
|
-
) if Fabrication.
|
23
|
+
) if Fabrication.manager.initializing?
|
24
24
|
end
|
25
25
|
|
26
26
|
def self.schematic(name)
|
27
|
-
Fabrication::Support.find_definitions if Fabrication.
|
28
|
-
Fabrication.
|
27
|
+
Fabrication::Support.find_definitions if Fabrication.manager.empty?
|
28
|
+
Fabrication.manager[name].tap do |schematic|
|
29
29
|
raise Fabrication::UnknownFabricatorError, "No Fabricator defined for '#{name}'" unless schematic
|
30
30
|
end
|
31
31
|
end
|
@@ -4,14 +4,22 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
|
|
4
4
|
defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
|
5
5
|
end
|
6
6
|
|
7
|
-
|
7
|
+
def build_instance
|
8
|
+
if self.class.without_protection?
|
9
|
+
self._instance = _klass.new(_attributes, without_protection: true)
|
10
|
+
else
|
11
|
+
self._instance = _klass.new(_attributes)
|
12
|
+
end
|
13
|
+
end
|
8
14
|
|
9
|
-
|
10
|
-
|
15
|
+
protected
|
16
|
+
|
17
|
+
def self.without_protection?
|
18
|
+
Gem::Version.new(ActiveRecord::VERSION::STRING).between?(Gem::Version.new('3.1.0'), Gem::Version.new('3.9'))
|
11
19
|
end
|
12
20
|
|
13
|
-
def
|
14
|
-
|
21
|
+
def validate_instance
|
22
|
+
_instance.valid?
|
15
23
|
end
|
16
24
|
|
17
25
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class Fabrication::Generator::Base
|
2
2
|
|
3
|
-
def self.supports?(
|
3
|
+
def self.supports?(_klass); true end
|
4
4
|
|
5
5
|
def build(attributes=[], callbacks={})
|
6
6
|
process_attributes(attributes)
|
@@ -13,7 +13,7 @@ class Fabrication::Generator::Base
|
|
13
13
|
build_instance
|
14
14
|
end
|
15
15
|
execute_callbacks(callbacks[:after_build])
|
16
|
-
|
16
|
+
_instance
|
17
17
|
end
|
18
18
|
|
19
19
|
def create(attributes=[], callbacks=[])
|
@@ -26,17 +26,17 @@ class Fabrication::Generator::Base
|
|
26
26
|
persist
|
27
27
|
execute_callbacks(callbacks[:after_create])
|
28
28
|
execute_callbacks(callbacks[:after_save])
|
29
|
-
|
29
|
+
_instance
|
30
30
|
end
|
31
31
|
|
32
32
|
def execute_callbacks(callbacks)
|
33
|
-
callbacks.each { |callback|
|
33
|
+
callbacks.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) } if callbacks
|
34
34
|
end
|
35
35
|
|
36
36
|
def to_hash(attributes=[], callbacks=[])
|
37
37
|
process_attributes(attributes)
|
38
38
|
Fabrication::Support.hash_class.new.tap do |hash|
|
39
|
-
|
39
|
+
_attributes.map do |name, value|
|
40
40
|
if value && value.respond_to?(:id)
|
41
41
|
hash["#{name}_id"] = value.id
|
42
42
|
else
|
@@ -47,57 +47,57 @@ class Fabrication::Generator::Base
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def build_instance_with_constructor_override(callback)
|
50
|
-
self.
|
50
|
+
self._instance = instance_eval &callback
|
51
51
|
set_attributes
|
52
52
|
end
|
53
53
|
|
54
54
|
def build_instance_with_init_callback(callback)
|
55
|
-
self.
|
55
|
+
self._instance = _klass.new(*callback.call)
|
56
56
|
set_attributes
|
57
57
|
end
|
58
58
|
|
59
59
|
def build_instance
|
60
|
-
self.
|
60
|
+
self._instance = _klass.new
|
61
61
|
set_attributes
|
62
62
|
end
|
63
63
|
|
64
64
|
def set_attributes
|
65
|
-
|
66
|
-
|
65
|
+
_attributes.each do |k,v|
|
66
|
+
_instance.send("#{k}=", v)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
def initialize(klass)
|
71
|
-
self.
|
71
|
+
self._klass = klass
|
72
72
|
end
|
73
73
|
|
74
74
|
def method_missing(method_name, *args, &block)
|
75
|
-
|
75
|
+
_attributes[method_name] || super
|
76
76
|
end
|
77
77
|
|
78
78
|
def validate_instance; end
|
79
79
|
|
80
80
|
protected
|
81
81
|
|
82
|
-
attr_accessor :
|
82
|
+
attr_accessor :_klass, :_instance, :_transient_attributes
|
83
83
|
|
84
|
-
def
|
85
|
-
@
|
84
|
+
def _attributes
|
85
|
+
@_attributes ||= {}
|
86
86
|
end
|
87
87
|
|
88
88
|
def persist
|
89
|
-
|
89
|
+
_instance.save! if _instance.respond_to?(:save!)
|
90
90
|
end
|
91
91
|
|
92
92
|
def post_initialize; end
|
93
93
|
|
94
94
|
def process_attributes(attributes)
|
95
|
-
self.
|
95
|
+
self._transient_attributes = Hash.new
|
96
96
|
attributes.each do |attribute|
|
97
|
-
|
98
|
-
|
97
|
+
_attributes[attribute.name] = attribute.processed_value(_attributes)
|
98
|
+
_transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
|
99
99
|
end
|
100
|
-
|
100
|
+
_attributes.reject! { |k| _transient_attributes.keys.include?(k) }
|
101
101
|
end
|
102
102
|
|
103
103
|
end
|
@@ -5,17 +5,17 @@ class Fabrication::Generator::DataMapper < Fabrication::Generator::Base
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def build_instance
|
8
|
-
self.
|
8
|
+
self._instance = _klass.new(_attributes)
|
9
9
|
end
|
10
10
|
|
11
11
|
def validate_instance
|
12
|
-
|
12
|
+
_instance.valid?
|
13
13
|
end
|
14
14
|
|
15
15
|
protected
|
16
16
|
|
17
17
|
def persist
|
18
|
-
|
18
|
+
_instance.save
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
@@ -6,14 +6,14 @@ class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
|
|
6
6
|
|
7
7
|
def build_instance
|
8
8
|
if Gem::Version.new(Mongoid::VERSION).between?(Gem::Version.new('2.3.0'), Gem::Version.new('3.9'))
|
9
|
-
self.
|
9
|
+
self._instance = _klass.new(_attributes, without_protection: true)
|
10
10
|
else
|
11
|
-
self.
|
11
|
+
self._instance = _klass.new(_attributes)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
def validate_instance
|
16
|
-
|
16
|
+
_instance.valid?
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
@@ -10,30 +10,30 @@ class Fabrication::Generator::Sequel < Fabrication::Generator::Base
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def set_attributes
|
13
|
-
|
14
|
-
if (reflection =
|
15
|
-
|
16
|
-
|
17
|
-
value.each { |o|
|
13
|
+
_attributes.each do |key, value|
|
14
|
+
if (reflection = _klass.association_reflections[key]) && value.is_a?(Array)
|
15
|
+
_instance.associations[key] = value
|
16
|
+
_instance.after_save_hook do
|
17
|
+
value.each { |o| _instance.send(reflection.add_method, o) }
|
18
18
|
end
|
19
19
|
else
|
20
|
-
|
20
|
+
_instance.send("#{key}=", value)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
def persist
|
26
|
-
|
26
|
+
_instance.save
|
27
27
|
end
|
28
28
|
|
29
29
|
def validate_instance
|
30
|
-
|
30
|
+
_instance.valid?
|
31
31
|
end
|
32
32
|
|
33
33
|
private
|
34
34
|
|
35
35
|
def load_instance_hooks
|
36
|
-
klass =
|
36
|
+
klass = _klass.respond_to?(:cti_base_model) ? _klass.cti_base_model : _klass
|
37
37
|
klass.plugin :instance_hooks unless klass.new.respond_to? :after_save_hook
|
38
38
|
end
|
39
39
|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
class Fabrication::Schematic::Definition
|
2
2
|
|
3
3
|
GENERATORS = [
|
4
|
-
Fabrication::Generator::ActiveRecord4,
|
5
4
|
Fabrication::Generator::ActiveRecord,
|
6
5
|
Fabrication::Generator::DataMapper,
|
7
6
|
Fabrication::Generator::Sequel,
|
@@ -49,16 +48,16 @@ class Fabrication::Schematic::Definition
|
|
49
48
|
end
|
50
49
|
|
51
50
|
def build(overrides={}, &block)
|
52
|
-
Fabrication.
|
51
|
+
Fabrication.manager.build_stack << self
|
53
52
|
merge(overrides, &block).instance_eval do
|
54
53
|
generator.new(klass).build(attributes, callbacks)
|
55
54
|
end
|
56
55
|
ensure
|
57
|
-
Fabrication.
|
56
|
+
Fabrication.manager.build_stack.pop
|
58
57
|
end
|
59
58
|
|
60
59
|
def fabricate(overrides={}, &block)
|
61
|
-
if Fabrication.
|
60
|
+
if Fabrication.manager.build_stack.empty?
|
62
61
|
merge(overrides, &block).instance_eval do
|
63
62
|
generator.new(klass).create(attributes, callbacks)
|
64
63
|
end
|
data/lib/fabrication/support.rb
CHANGED
@@ -3,7 +3,7 @@ class Fabrication::Support
|
|
3
3
|
class << self
|
4
4
|
|
5
5
|
def fabricatable?(name)
|
6
|
-
Fabrication.
|
6
|
+
Fabrication.manager[name] || class_for(name)
|
7
7
|
end
|
8
8
|
|
9
9
|
def class_for(class_or_to_s)
|
@@ -27,13 +27,13 @@ class Fabrication::Support
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def find_definitions
|
30
|
-
Fabrication.
|
30
|
+
Fabrication.manager.preinitialize
|
31
31
|
Fabrication::Config.fabricator_dir.each do |folder|
|
32
32
|
Dir.glob(File.join(Fabrication::Config.path_prefix, folder, '**', '*.rb')).sort.each do |file|
|
33
33
|
load file
|
34
34
|
end
|
35
35
|
end
|
36
|
-
Fabrication.
|
36
|
+
Fabrication.manager.freeze
|
37
37
|
end
|
38
38
|
|
39
39
|
def hash_class
|
@@ -3,7 +3,7 @@ class Fabrication::Transform
|
|
3
3
|
class << self
|
4
4
|
|
5
5
|
def apply_to(schematic, attributes_hash)
|
6
|
-
Fabrication::Support.find_definitions if Fabrication.
|
6
|
+
Fabrication::Support.find_definitions if Fabrication.manager.empty?
|
7
7
|
attributes_hash.inject({}) {|h,(k,v)| h.update(k => apply_transform(schematic, k, v)) }
|
8
8
|
end
|
9
9
|
|
data/lib/fabrication/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fabrication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.7.
|
4
|
+
version: 2.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Elliott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -236,7 +236,6 @@ files:
|
|
236
236
|
- lib/fabrication/errors/unknown_fabricator_error.rb
|
237
237
|
- lib/fabrication/fabricator.rb
|
238
238
|
- lib/fabrication/generator/active_record.rb
|
239
|
-
- lib/fabrication/generator/active_record_4.rb
|
240
239
|
- lib/fabrication/generator/base.rb
|
241
240
|
- lib/fabrication/generator/data_mapper.rb
|
242
241
|
- lib/fabrication/generator/keymaker.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class Fabrication::Generator::ActiveRecord4 < Fabrication::Generator::ActiveRecord
|
2
|
-
|
3
|
-
def self.supports?(klass)
|
4
|
-
super && active_record_4?
|
5
|
-
end
|
6
|
-
|
7
|
-
def build_instance
|
8
|
-
self.__instance = __klass.new(__attributes)
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def self.active_record_4?
|
14
|
-
ActiveRecord::VERSION::MAJOR == 4
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|