fabrication 2.20.1 → 3.0.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.markdown +1 -4
- data/Rakefile +8 -10
- data/lib/fabricate.rb +10 -13
- data/lib/fabrication.rb +6 -4
- data/lib/fabrication/config.rb +28 -15
- data/lib/fabrication/cucumber/step_fabricator.rb +20 -13
- data/lib/fabrication/errors/duplicate_fabricator_error.rb +5 -3
- data/lib/fabrication/errors/infinite_recursion_error.rb +5 -3
- data/lib/fabrication/errors/misplaced_fabricate_error.rb +7 -3
- data/lib/fabrication/errors/unfabricatable_error.rb +5 -4
- data/lib/fabrication/errors/unknown_fabricator_error.rb +5 -5
- data/lib/fabrication/generator/active_record.rb +18 -11
- data/lib/fabrication/generator/base.rb +89 -81
- data/lib/fabrication/generator/mongoid.rb +13 -11
- data/lib/fabrication/generator/sequel.rb +29 -27
- data/lib/fabrication/railtie.rb +1 -3
- data/lib/fabrication/schematic/attribute.rb +69 -62
- data/lib/fabrication/schematic/definition.rb +140 -134
- data/lib/fabrication/schematic/evaluator.rb +61 -54
- data/lib/fabrication/schematic/manager.rb +67 -59
- data/lib/fabrication/schematic/runner.rb +12 -9
- data/lib/fabrication/sequencer.rb +23 -22
- data/lib/fabrication/support.rb +57 -54
- data/lib/fabrication/syntax/make.rb +0 -1
- data/lib/fabrication/transform.rb +34 -36
- data/lib/fabrication/version.rb +1 -1
- data/lib/rails/generators/fabrication/cucumber_steps/cucumber_steps_generator.rb +2 -3
- data/lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb +10 -10
- data/lib/rails/generators/fabrication/model/model_generator.rb +14 -9
- data/lib/rails/generators/fabrication/model/templates/fabricator.erb +5 -1
- data/lib/tasks/defined_fabricators.rake +11 -11
- metadata +10 -12
- data/lib/fabrication/generator/data_mapper.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 20a02618a5107ba5850de6da2bd58ae428d33d8163ab3c71f9233574780998df
|
4
|
+
data.tar.gz: 33bac0b9d796da2d94adf3a8eb8940ff8766c36c9435429a03a1da0dd1efb8ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0d2aa56f7153981b3be287dde1204a1050b964e5bdba14314d7f7cda4d7af7a4960cb2670bd2c58b7615768fdea84c4dbd15da2d1db95b5b4371e86091b336d
|
7
|
+
data.tar.gz: a182aecb4c3d38e36cd0e8f282dfd575b7234427a15e7f575cdbc84ca090848f162de9becc0989cef3baaa76c986a946c467b53adb937c5011932af774735b25
|
data/README.markdown
CHANGED
@@ -6,10 +6,7 @@ Fabrication is an object generation framework for Ruby.
|
|
6
6
|
|
7
7
|
## Compatibility
|
8
8
|
|
9
|
-
Fabrication is tested against
|
10
|
-
|
11
|
-
[![Build Status](https://secure.travis-ci.org/paulelliott/fabrication.png)](http://travis-ci.org/paulelliott/fabrication)
|
12
|
-
[![Code Climate](https://codeclimate.com/github/paulelliott/fabrication.png)](https://codeclimate.com/github/paulelliott/fabrication)
|
9
|
+
Fabrication is tested against all officially supported versions of Ruby.
|
13
10
|
|
14
11
|
## Documentation
|
15
12
|
|
data/Rakefile
CHANGED
@@ -1,20 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
3
|
|
4
4
|
Bundler.require
|
5
5
|
|
6
|
-
require
|
6
|
+
require 'rspec/core/rake_task'
|
7
7
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
-
spec.pattern =
|
8
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
9
9
|
end
|
10
10
|
|
11
11
|
desc 'All cucumber features with kitchen sink appraisal'
|
12
12
|
task :cucumber do
|
13
|
-
system('appraisal kitchen-sink cucumber -f progress')
|
13
|
+
system('appraisal kitchen-sink cucumber -f progress --publish-quiet')
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
task default: :spec
|
20
|
-
end
|
16
|
+
default_task = ENV['APPRAISAL_INITIALIZED'] ? :spec : %i[cucumber appraisal]
|
17
|
+
|
18
|
+
task default: default_task
|
data/lib/fabricate.rb
CHANGED
@@ -1,51 +1,48 @@
|
|
1
1
|
class Fabricate
|
2
|
-
def self.times(count, name, overrides={}, &block)
|
2
|
+
def self.times(count, name, overrides = {}, &block)
|
3
3
|
count.times.map { Fabricate(name, overrides, &block) }
|
4
4
|
end
|
5
5
|
|
6
|
-
def self.build_times(count, name, overrides={}, &block)
|
6
|
+
def self.build_times(count, name, overrides = {}, &block)
|
7
7
|
count.times.map { Fabricate.build(name, overrides, &block) }
|
8
8
|
end
|
9
9
|
|
10
|
-
def self.attributes_for_times(count, name, overrides={}, &block)
|
10
|
+
def self.attributes_for_times(count, name, overrides = {}, &block)
|
11
11
|
count.times.map { Fabricate.attributes_for(name, overrides, &block) }
|
12
12
|
end
|
13
13
|
|
14
|
-
def self.attributes_for(name, overrides={}, &block)
|
14
|
+
def self.attributes_for(name, overrides = {}, &block)
|
15
15
|
fail_if_initializing(name)
|
16
16
|
schematic(name).to_attributes(overrides, &block)
|
17
17
|
end
|
18
18
|
|
19
|
-
def self.to_params(name, overrides={}, &block)
|
19
|
+
def self.to_params(name, overrides = {}, &block)
|
20
20
|
fail_if_initializing(name)
|
21
21
|
schematic(name).to_params(overrides, &block)
|
22
22
|
end
|
23
23
|
|
24
|
-
def self.build(name, overrides={}, &block)
|
24
|
+
def self.build(name, overrides = {}, &block)
|
25
25
|
fail_if_initializing(name)
|
26
26
|
schematic(name).build(overrides, &block).tap do |object|
|
27
27
|
Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
def self.create(name, overrides={}, &block)
|
31
|
+
def self.create(name, overrides = {}, &block)
|
32
32
|
fail_if_initializing(name)
|
33
33
|
schematic(name).fabricate(overrides, &block)
|
34
34
|
end
|
35
35
|
|
36
|
-
def self.sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
|
36
|
+
def self.sequence(name = Fabrication::Sequencer::DEFAULT, start = nil, &block)
|
37
37
|
Fabrication::Sequencer.sequence(name, start, &block)
|
38
38
|
end
|
39
39
|
|
40
40
|
def self.schematic(name)
|
41
41
|
Fabrication.manager.load_definitions if Fabrication.manager.empty?
|
42
|
-
Fabrication.manager[name] || raise(Fabrication::UnknownFabricatorError
|
42
|
+
Fabrication.manager[name] || raise(Fabrication::UnknownFabricatorError, name)
|
43
43
|
end
|
44
44
|
|
45
|
-
private
|
46
|
-
|
47
45
|
def self.fail_if_initializing(name)
|
48
|
-
raise Fabrication::MisplacedFabricateError
|
46
|
+
raise Fabrication::MisplacedFabricateError, name if Fabrication.manager.initializing?
|
49
47
|
end
|
50
|
-
|
51
48
|
end
|
data/lib/fabrication.rb
CHANGED
@@ -32,7 +32,6 @@ module Fabrication
|
|
32
32
|
module Generator
|
33
33
|
autoload :ActiveRecord, 'fabrication/generator/active_record'
|
34
34
|
autoload :ActiveRecord4, 'fabrication/generator/active_record_4'
|
35
|
-
autoload :DataMapper, 'fabrication/generator/data_mapper'
|
36
35
|
autoload :Mongoid, 'fabrication/generator/mongoid'
|
37
36
|
autoload :Sequel, 'fabrication/generator/sequel'
|
38
37
|
autoload :Base, 'fabrication/generator/base'
|
@@ -52,20 +51,23 @@ module Fabrication
|
|
52
51
|
end
|
53
52
|
|
54
53
|
def self.schematics
|
55
|
-
puts
|
54
|
+
puts 'DEPRECATION WARNING: Fabrication.schematics has been replaced by '\
|
55
|
+
'Fabrication.manager and will be removed in 3.0.0.'
|
56
56
|
manager
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
|
60
|
+
# rubocop:disable Naming/MethodName
|
61
|
+
def Fabricator(name, options = {}, &block)
|
61
62
|
Fabrication.manager.register(name, options, &block)
|
62
63
|
end
|
63
64
|
|
64
|
-
def Fabricate(name, overrides={}, &block)
|
65
|
+
def Fabricate(name, overrides = {}, &block)
|
65
66
|
Fabricate.create(name, overrides, &block).tap do |object|
|
66
67
|
Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
|
67
68
|
end
|
68
69
|
end
|
70
|
+
# rubocop:enable Naming/MethodName
|
69
71
|
|
70
72
|
module FabricationMethods
|
71
73
|
def fabrications
|
data/lib/fabrication/config.rb
CHANGED
@@ -2,14 +2,16 @@ module Fabrication
|
|
2
2
|
module Config
|
3
3
|
extend self
|
4
4
|
|
5
|
-
def configure
|
5
|
+
def configure
|
6
|
+
yield self
|
7
|
+
end
|
6
8
|
|
7
9
|
def reset_defaults
|
8
10
|
@fabricator_path =
|
9
11
|
@path_prefix =
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
@sequence_start =
|
13
|
+
@generators =
|
14
|
+
nil
|
13
15
|
end
|
14
16
|
|
15
17
|
def fabricator_path
|
@@ -18,43 +20,54 @@ module Fabrication
|
|
18
20
|
alias fabricator_paths fabricator_path
|
19
21
|
|
20
22
|
def fabricator_dir
|
21
|
-
puts
|
23
|
+
puts 'DEPRECATION WARNING: Fabrication::Config.fabricator_dir has been ' \
|
24
|
+
'replaced by Fabrication::Config.fabricator_path'
|
22
25
|
fabricator_path
|
23
26
|
end
|
24
27
|
|
25
28
|
def fabricator_path=(folders)
|
26
|
-
@fabricator_path = (
|
29
|
+
@fabricator_path = ([] << folders).flatten
|
27
30
|
end
|
28
31
|
|
29
32
|
def fabricator_dir=(folders)
|
30
|
-
puts
|
33
|
+
puts 'DEPRECATION WARNING: Fabrication::Config.fabricator_dir has been ' \
|
34
|
+
'replaced by Fabrication::Config.fabricator_path'
|
31
35
|
self.fabricator_path = folders
|
32
36
|
end
|
33
37
|
|
34
|
-
attr_writer :sequence_start
|
35
|
-
|
38
|
+
attr_writer :sequence_start, :register_with_steps
|
39
|
+
|
40
|
+
def sequence_start
|
41
|
+
@sequence_start ||= 0
|
42
|
+
end
|
36
43
|
|
37
44
|
def path_prefix=(folders)
|
38
|
-
@path_prefix = (
|
45
|
+
@path_prefix = ([] << folders).flatten
|
39
46
|
end
|
40
47
|
|
41
48
|
def path_prefix
|
42
|
-
@path_prefix ||= [defined?(Rails) ? Rails.root :
|
49
|
+
@path_prefix ||= [defined?(Rails) ? Rails.root : '.']
|
43
50
|
end
|
44
51
|
alias path_prefixes path_prefix
|
45
52
|
|
46
|
-
attr_writer :register_with_steps
|
47
53
|
def register_with_steps?
|
48
54
|
@register_with_steps ||= nil
|
49
55
|
end
|
50
56
|
|
51
|
-
def generators
|
57
|
+
def generators
|
58
|
+
@generators ||= []
|
59
|
+
end
|
52
60
|
|
53
61
|
def generator_for(default_generators, klass)
|
54
62
|
(generators + default_generators).detect { |gen| gen.supports?(klass) }
|
55
63
|
end
|
56
64
|
|
57
|
-
def recursion_limit
|
58
|
-
|
65
|
+
def recursion_limit
|
66
|
+
@recursion_limit ||= 20
|
67
|
+
end
|
68
|
+
|
69
|
+
def recursion_limit=(limit)
|
70
|
+
@recursion_limit = limit
|
71
|
+
end
|
59
72
|
end
|
60
73
|
end
|
@@ -3,24 +3,27 @@ module Fabrication
|
|
3
3
|
class StepFabricator
|
4
4
|
attr_reader :model
|
5
5
|
|
6
|
-
def initialize(model_name, opts ={})
|
6
|
+
def initialize(model_name, opts = {})
|
7
7
|
@model = dehumanize(model_name)
|
8
8
|
@fabricator = Fabrication::Support.singularize(@model).to_sym
|
9
9
|
@parent_name = opts.delete(:parent)
|
10
10
|
end
|
11
11
|
|
12
|
-
def from_table(table, extra={})
|
12
|
+
def from_table(table, extra = {})
|
13
13
|
hashes = singular? ? [table.rows_hash] : table.hashes
|
14
|
-
hashes.map do |hash|
|
14
|
+
transformed_hashes = hashes.map do |hash|
|
15
15
|
transformed_hash = Fabrication::Transform.apply_to(@model, parameterize_hash(hash))
|
16
16
|
make(transformed_hash.merge(extra))
|
17
|
-
end
|
17
|
+
end
|
18
|
+
remember(transformed_hashes)
|
19
|
+
transformed_hashes
|
18
20
|
end
|
19
21
|
|
20
|
-
def n(count, attrs={})
|
21
|
-
count.times.map { make(attrs) }.tap {|o| remember(o) }
|
22
|
+
def n(count, attrs = {})
|
23
|
+
count.times.map { make(attrs) }.tap { |o| remember(o) }
|
22
24
|
end
|
23
25
|
|
26
|
+
# rubocop:disable Naming/PredicateName
|
24
27
|
def has_many(children)
|
25
28
|
instance = Fabrications[@fabricator]
|
26
29
|
children = dehumanize(children)
|
@@ -29,9 +32,11 @@ module Fabrication
|
|
29
32
|
child.respond_to?(:save!) && child.save!
|
30
33
|
end
|
31
34
|
end
|
35
|
+
# rubocop:enable Naming/PredicateName
|
32
36
|
|
33
37
|
def parent
|
34
38
|
return unless @parent_name
|
39
|
+
|
35
40
|
Fabrications[dehumanize(@parent_name)]
|
36
41
|
end
|
37
42
|
|
@@ -54,19 +59,20 @@ module Fabrication
|
|
54
59
|
end
|
55
60
|
|
56
61
|
def dehumanize(string)
|
57
|
-
string.gsub(/\W+/,'_').downcase
|
62
|
+
string.gsub(/\W+/, '_').downcase
|
58
63
|
end
|
59
64
|
|
60
65
|
def parameterize_hash(hash)
|
61
|
-
hash.inject({}) {|h,(k,v)| h.update(dehumanize(k).to_sym => v)}
|
66
|
+
hash.inject({}) { |h, (k, v)| h.update(dehumanize(k).to_sym => v) }
|
62
67
|
end
|
63
68
|
|
64
|
-
def make(attrs={})
|
69
|
+
def make(attrs = {})
|
65
70
|
Fabricate(@fabricator, attrs.merge(parentship))
|
66
71
|
end
|
67
72
|
|
68
73
|
def parentship
|
69
74
|
return {} unless parent
|
75
|
+
|
70
76
|
parent_class_name = parent.class.to_s.underscore
|
71
77
|
|
72
78
|
parent_instance = parent
|
@@ -77,18 +83,19 @@ module Fabrication
|
|
77
83
|
|
78
84
|
{ parent_class_name => parent_instance }
|
79
85
|
end
|
80
|
-
|
81
86
|
end
|
82
87
|
|
83
88
|
module Fabrications
|
84
|
-
|
89
|
+
def self.fabrications
|
90
|
+
@fabrications ||= {}
|
91
|
+
end
|
85
92
|
|
86
93
|
def self.[](fabricator)
|
87
|
-
|
94
|
+
fabrications[fabricator.to_sym]
|
88
95
|
end
|
89
96
|
|
90
97
|
def self.[]=(fabricator, fabrication)
|
91
|
-
|
98
|
+
fabrications[fabricator.to_sym] = fabrication
|
92
99
|
end
|
93
100
|
end
|
94
101
|
end
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Fabrication
|
2
|
+
class DuplicateFabricatorError < StandardError
|
3
|
+
def initialize(string)
|
4
|
+
super("'#{string}' is already defined")
|
5
|
+
end
|
4
6
|
end
|
5
7
|
end
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Fabrication
|
2
|
+
class InfiniteRecursionError < StandardError
|
3
|
+
def initialize(name)
|
4
|
+
super("You appear to have infinite recursion with the `#{name}` fabricator")
|
5
|
+
end
|
4
6
|
end
|
5
7
|
end
|
@@ -1,5 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Fabrication
|
2
|
+
class MisplacedFabricateError < StandardError
|
3
|
+
def initialize(name)
|
4
|
+
super("You tried to fabricate `#{name}` while Fabricators were still loading. " \
|
5
|
+
"Check your fabricator files and make sure you didn't accidentally type " \
|
6
|
+
'`Fabricate` instead of `Fabricator` in there somewhere.')
|
7
|
+
end
|
4
8
|
end
|
5
9
|
end
|
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Fabrication
|
2
|
+
class UnfabricatableError < StandardError
|
3
|
+
def initialize(name, original_error)
|
4
|
+
super("No class found for '#{name}' (original exception: #{original_error.message})")
|
5
|
+
end
|
5
6
|
end
|
6
7
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Fabrication
|
2
|
+
class UnknownFabricatorError < StandardError
|
3
|
+
def initialize(name)
|
4
|
+
super("No Fabricator defined for '#{name}'")
|
5
|
+
end
|
5
6
|
end
|
6
|
-
|
7
7
|
end
|
@@ -1,15 +1,22 @@
|
|
1
|
-
|
1
|
+
module Fabrication
|
2
|
+
module Generator
|
3
|
+
class ActiveRecord < Fabrication::Generator::Base
|
4
|
+
def self.supports?(klass)
|
5
|
+
# Some gems will declare an ActiveRecord module for their own purposes
|
6
|
+
# so we can't assume because we have the ActiveRecord module that we also
|
7
|
+
# have ActiveRecord::Base. Because defined? can return nil we ensure that nil
|
8
|
+
# becomes false.
|
9
|
+
defined?(::ActiveRecord) && defined?(::ActiveRecord::Base) &&
|
10
|
+
klass.ancestors.include?(::ActiveRecord::Base) || false
|
11
|
+
end
|
2
12
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
self._instance = _klass.new(_attributes)
|
13
|
+
def build_instance
|
14
|
+
self._instance = if _klass.respond_to?(:protected_attributes)
|
15
|
+
_klass.new(_attributes, without_protection: true)
|
16
|
+
else
|
17
|
+
_klass.new(_attributes)
|
18
|
+
end
|
19
|
+
end
|
12
20
|
end
|
13
21
|
end
|
14
|
-
|
15
22
|
end
|
@@ -1,103 +1,111 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Fabrication
|
2
|
+
module Generator
|
3
|
+
class Base
|
4
|
+
def self.supports?(_klass)
|
5
|
+
true
|
6
|
+
end
|
4
7
|
|
5
|
-
|
6
|
-
|
8
|
+
def build(attributes = [], callbacks = {})
|
9
|
+
process_attributes(attributes)
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
if callbacks[:initialize_with]
|
12
|
+
build_instance_with_constructor_override(callbacks[:initialize_with])
|
13
|
+
elsif callbacks[:on_init]
|
14
|
+
build_instance_with_init_callback(callbacks[:on_init])
|
15
|
+
else
|
16
|
+
build_instance
|
17
|
+
end
|
18
|
+
execute_callbacks(callbacks[:after_build])
|
19
|
+
_instance
|
20
|
+
end
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
def create(attributes = [], callbacks = [])
|
23
|
+
build(attributes, callbacks)
|
24
|
+
execute_callbacks(callbacks[:before_validation])
|
25
|
+
execute_callbacks(callbacks[:after_validation])
|
26
|
+
execute_callbacks(callbacks[:before_save])
|
27
|
+
execute_callbacks(callbacks[:before_create])
|
28
|
+
persist
|
29
|
+
execute_callbacks(callbacks[:after_create])
|
30
|
+
execute_callbacks(callbacks[:after_save])
|
31
|
+
_instance
|
32
|
+
end
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
def execute_callbacks(callbacks)
|
35
|
+
callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) }
|
36
|
+
end
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
38
|
+
def to_params(attributes = [])
|
39
|
+
process_attributes(attributes)
|
40
|
+
_attributes.respond_to?(:with_indifferent_access) ? _attributes.with_indifferent_access : _attributes
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
def to_hash(attributes = [], _callbacks = [])
|
44
|
+
process_attributes(attributes)
|
45
|
+
Fabrication::Support.hash_class.new.tap do |hash|
|
46
|
+
_attributes.map do |name, value|
|
47
|
+
if value.respond_to?(:id)
|
48
|
+
hash["#{name}_id"] = value.id
|
49
|
+
else
|
50
|
+
hash[name] = value
|
51
|
+
end
|
52
|
+
end
|
48
53
|
end
|
49
54
|
end
|
50
|
-
end
|
51
|
-
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
def build_instance_with_constructor_override(callback)
|
57
|
+
self._instance = instance_eval(&callback)
|
58
|
+
set_attributes
|
59
|
+
end
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
def build_instance_with_init_callback(callback)
|
62
|
+
self._instance = _klass.new(*callback.call)
|
63
|
+
set_attributes
|
64
|
+
end
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
66
|
+
def build_instance
|
67
|
+
self._instance = _klass.new
|
68
|
+
set_attributes
|
69
|
+
end
|
67
70
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
71
|
+
def set_attributes
|
72
|
+
_attributes.each do |k, v|
|
73
|
+
_instance.send("#{k}=", v)
|
74
|
+
end
|
75
|
+
end
|
73
76
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
def initialize(klass)
|
78
|
+
self._klass = klass
|
79
|
+
end
|
77
80
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
+
def respond_to_missing?(method_name, _include_private = false)
|
82
|
+
_attributes.key?(method_name)
|
83
|
+
end
|
81
84
|
|
82
|
-
|
85
|
+
def method_missing(method_name, *args, &block)
|
86
|
+
_attributes.fetch(method_name) { super }
|
87
|
+
end
|
83
88
|
|
84
|
-
|
89
|
+
protected
|
85
90
|
|
86
|
-
|
87
|
-
@_attributes ||= {}
|
88
|
-
end
|
91
|
+
attr_accessor :_klass, :_instance, :_transient_attributes
|
89
92
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
+
def _attributes
|
94
|
+
@_attributes ||= {}
|
95
|
+
end
|
93
96
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
def persist
|
98
|
+
_instance.save! if _instance.respond_to?(:save!)
|
99
|
+
end
|
100
|
+
|
101
|
+
def process_attributes(attributes)
|
102
|
+
self._transient_attributes = ({})
|
103
|
+
attributes.each do |attribute|
|
104
|
+
_attributes[attribute.name] = attribute.processed_value(_attributes)
|
105
|
+
_transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
|
106
|
+
end
|
107
|
+
_attributes.reject! { |k| _transient_attributes.keys.include?(k) }
|
108
|
+
end
|
99
109
|
end
|
100
|
-
_attributes.reject! { |k| _transient_attributes.keys.include?(k) }
|
101
110
|
end
|
102
|
-
|
103
111
|
end
|