fabrication 2.20.2 → 2.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.markdown +1 -4
  3. data/Rakefile +8 -10
  4. data/lib/fabricate.rb +10 -13
  5. data/lib/fabrication/config.rb +28 -15
  6. data/lib/fabrication/cucumber/step_fabricator.rb +20 -13
  7. data/lib/fabrication/errors/duplicate_fabricator_error.rb +5 -3
  8. data/lib/fabrication/errors/infinite_recursion_error.rb +5 -3
  9. data/lib/fabrication/errors/misplaced_fabricate_error.rb +7 -3
  10. data/lib/fabrication/errors/unfabricatable_error.rb +5 -4
  11. data/lib/fabrication/errors/unknown_fabricator_error.rb +5 -5
  12. data/lib/fabrication/generator/active_record.rb +18 -15
  13. data/lib/fabrication/generator/base.rb +93 -81
  14. data/lib/fabrication/generator/data_mapper.rb +14 -12
  15. data/lib/fabrication/generator/sequel.rb +29 -27
  16. data/lib/fabrication/railtie.rb +1 -3
  17. data/lib/fabrication/schematic/attribute.rb +69 -62
  18. data/lib/fabrication/schematic/definition.rb +140 -134
  19. data/lib/fabrication/schematic/evaluator.rb +61 -54
  20. data/lib/fabrication/schematic/manager.rb +67 -59
  21. data/lib/fabrication/schematic/runner.rb +12 -9
  22. data/lib/fabrication/sequencer.rb +23 -22
  23. data/lib/fabrication/support.rb +63 -54
  24. data/lib/fabrication/syntax/make.rb +0 -1
  25. data/lib/fabrication/transform.rb +34 -36
  26. data/lib/fabrication/version.rb +1 -1
  27. data/lib/fabrication.rb +6 -4
  28. data/lib/rails/generators/fabrication/cucumber_steps/cucumber_steps_generator.rb +2 -3
  29. data/lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb +10 -10
  30. data/lib/rails/generators/fabrication/model/model_generator.rb +14 -9
  31. data/lib/rails/generators/fabrication/model/templates/fabricator.erb +5 -1
  32. data/lib/tasks/defined_fabricators.rake +11 -11
  33. metadata +7 -8
  34. data/lib/fabrication/generator/mongoid.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5d3cb9fdb13ee5c47eaa85198882cdc4928a48830ac9a9ca029becffefc4e1a
4
- data.tar.gz: af6d6f702f0b2b45965cf9b366017c32227ce292bc3fb96e4d5c83e32f69a9c4
3
+ metadata.gz: af00199fb86206bfaecee18e88668f63446301f7bfff776831ad26a18c092187
4
+ data.tar.gz: b63b7eb25cf5e98f9051befe7ffdb2c994a07c149a32af93e4f48b183f17fdfe
5
5
  SHA512:
6
- metadata.gz: 30f5250a089513d1a206ca9db5935172ae30923a140522a9709398a3b84484a2079a5efd5dc70c79e8048db44eb2e390d52ec0ba04321742cc711873d2983073
7
- data.tar.gz: cd0cc5c4410730e4303001ac369aa23a047b2924f52092ee74dfc96981d5ebfd978fd07f20064e34e1fdf1cbdda3a1575475ee6c3a129e60f0ae4489f39b9e96
6
+ metadata.gz: bfc49c073e8a19855863b5da27ff6c45aa45cd80eea134611b40174fb9c4ac2f9df6ee44e8dd3613001c7662a4e1ece7db0d2df78a8b025d647546d0f0fa137d
7
+ data.tar.gz: b8802428e05897ec7a0b6dc0a0a01f4d0c33437d5b3d4a895e116664e00fd7f79904143cfd9a6c8db4fb135d6f0c1c4c54754fb7b35fc47d7962209a1fe7eb6a
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 Ruby 2.2 and above.
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 "rubygems"
2
- require "bundler/setup"
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
3
 
4
4
  Bundler.require
5
5
 
6
- require "rspec/core/rake_task"
6
+ require 'rspec/core/rake_task'
7
7
  RSpec::Core::RakeTask.new(:spec) do |spec|
8
- spec.pattern = "spec/**/*_spec.rb"
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
- if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
17
- task default: [:cucumber, :appraisal]
18
- else
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.new(name))
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.new(name) if Fabrication.manager.initializing?
46
+ raise Fabrication::MisplacedFabricateError, name if Fabrication.manager.initializing?
49
47
  end
50
-
51
48
  end
@@ -2,14 +2,16 @@ module Fabrication
2
2
  module Config
3
3
  extend self
4
4
 
5
- def configure; yield self end
5
+ def configure
6
+ yield self
7
+ end
6
8
 
7
9
  def reset_defaults
8
10
  @fabricator_path =
9
11
  @path_prefix =
10
- @sequence_start =
11
- @generators =
12
- nil
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 "DEPRECATION WARNING: Fabrication::Config.fabricator_dir has been replaced by Fabrication::Config.fabricator_path"
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 = (Array.new << folders).flatten
29
+ @fabricator_path = ([] << folders).flatten
27
30
  end
28
31
 
29
32
  def fabricator_dir=(folders)
30
- puts "DEPRECATION WARNING: Fabrication::Config.fabricator_dir has been replaced by Fabrication::Config.fabricator_path"
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
- def sequence_start; @sequence_start ||= 0 end
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 = (Array.new << folders).flatten
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; @generators ||= [] end
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; @recursion_limit ||= 20 end
58
- def recursion_limit=(limit); @recursion_limit = limit end
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.tap {|o| remember(o) }
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
- @@fabrications = {}
89
+ def self.fabrications
90
+ @fabrications ||= {}
91
+ end
85
92
 
86
93
  def self.[](fabricator)
87
- @@fabrications[fabricator.to_sym]
94
+ fabrications[fabricator.to_sym]
88
95
  end
89
96
 
90
97
  def self.[]=(fabricator, fabrication)
91
- @@fabrications[fabricator.to_sym] = fabrication
98
+ fabrications[fabricator.to_sym] = fabrication
92
99
  end
93
100
  end
94
101
  end
@@ -1,5 +1,7 @@
1
- class Fabrication::DuplicateFabricatorError < StandardError
2
- def initialize(string)
3
- super("'#{string}' is already defined")
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
- class Fabrication::InfiniteRecursionError < StandardError
2
- def initialize(name)
3
- super("You appear to have infinite recursion with the `#{name}` fabricator")
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
- class Fabrication::MisplacedFabricateError < StandardError
2
- def initialize(name)
3
- super("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.")
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
- class Fabrication::UnfabricatableError < StandardError
2
-
3
- def initialize(name, original_error)
4
- super("No class found for '#{name}' (original exception: #{original_error.message})")
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
- class Fabrication::UnknownFabricatorError < StandardError
2
-
3
- def initialize(name)
4
- super("No Fabricator defined for '#{name}'")
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,19 +1,22 @@
1
- class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
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
- def self.supports?(klass)
4
- # Some gems will declare an ActiveRecord module for their own purposes
5
- # so we can't assume because we have the ActiveRecord module that we also
6
- # have ActiveRecord::Base. Because defined? can return nil we ensure that nil
7
- # becomes false.
8
- defined?(ActiveRecord) && defined?(ActiveRecord::Base) && klass.ancestors.include?(ActiveRecord::Base) || false
9
- end
10
-
11
- def build_instance
12
- if _klass.respond_to?(:protected_attributes)
13
- self._instance = _klass.new(_attributes, without_protection: true)
14
- else
15
- 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
16
20
  end
17
21
  end
18
-
19
22
  end
@@ -1,103 +1,115 @@
1
- class Fabrication::Generator::Base
2
-
3
- def self.supports?(_klass); true end
1
+ module Fabrication
2
+ module Generator
3
+ class Base
4
+ def self.supports?(_klass)
5
+ true
6
+ end
4
7
 
5
- def build(attributes=[], callbacks={})
6
- process_attributes(attributes)
8
+ def build(attributes = [], callbacks = {})
9
+ process_attributes(attributes)
7
10
 
8
- if callbacks[:initialize_with]
9
- build_instance_with_constructor_override(callbacks[:initialize_with])
10
- elsif callbacks[:on_init]
11
- build_instance_with_init_callback(callbacks[:on_init])
12
- else
13
- build_instance
14
- end
15
- execute_callbacks(callbacks[:after_build])
16
- _instance
17
- end
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
- def create(attributes=[], callbacks=[])
20
- build(attributes, callbacks)
21
- execute_callbacks(callbacks[:before_validation])
22
- execute_callbacks(callbacks[:after_validation])
23
- execute_callbacks(callbacks[:before_save])
24
- execute_callbacks(callbacks[:before_create])
25
- persist
26
- execute_callbacks(callbacks[:after_create])
27
- execute_callbacks(callbacks[:after_save])
28
- _instance
29
- end
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
- def execute_callbacks(callbacks)
32
- callbacks.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) } if callbacks
33
- end
34
+ def execute_callbacks(callbacks)
35
+ callbacks&.each { |callback| _instance.instance_exec(_instance, _transient_attributes, &callback) }
36
+ end
34
37
 
35
- def to_params(attributes=[])
36
- process_attributes(attributes)
37
- _attributes.respond_to?(:with_indifferent_access) ? _attributes.with_indifferent_access : _attributes
38
- end
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
- def to_hash(attributes=[], callbacks=[])
41
- process_attributes(attributes)
42
- Fabrication::Support.hash_class.new.tap do |hash|
43
- _attributes.map do |name, value|
44
- if value && value.respond_to?(:id)
45
- hash["#{name}_id"] = value.id
46
- else
47
- hash[name] = value
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
- def build_instance_with_constructor_override(callback)
54
- self._instance = instance_eval(&callback)
55
- set_attributes
56
- end
56
+ def build_instance_with_constructor_override(callback)
57
+ self._instance = instance_eval(&callback)
58
+ set_attributes
59
+ end
57
60
 
58
- def build_instance_with_init_callback(callback)
59
- self._instance = _klass.new(*callback.call)
60
- set_attributes
61
- end
61
+ def build_instance_with_init_callback(callback)
62
+ self._instance = _klass.new(*callback.call)
63
+ set_attributes
64
+ end
62
65
 
63
- def build_instance
64
- self._instance = _klass.new
65
- set_attributes
66
- end
66
+ def build_instance
67
+ self._instance = _klass.new
68
+ set_attributes
69
+ end
67
70
 
68
- def set_attributes
69
- _attributes.each do |k,v|
70
- _instance.send("#{k}=", v)
71
- end
72
- end
71
+ def set_attributes
72
+ if _instance.respond_to?(:attributes=)
73
+ _instance.attributes = _attributes
74
+ else
75
+ _attributes.each do |k, v|
76
+ _instance.send("#{k}=", v)
77
+ end
78
+ end
79
+ end
73
80
 
74
- def initialize(klass)
75
- self._klass = klass
76
- end
81
+ def initialize(klass)
82
+ self._klass = klass
83
+ end
77
84
 
78
- def method_missing(method_name, *args, &block)
79
- _attributes[method_name] || super
80
- end
85
+ def respond_to_missing?(method_name, _include_private = false)
86
+ _attributes.key?(method_name)
87
+ end
81
88
 
82
- protected
89
+ def method_missing(method_name, *args, &block)
90
+ _attributes.fetch(method_name) { super }
91
+ end
83
92
 
84
- attr_accessor :_klass, :_instance, :_transient_attributes
93
+ protected
85
94
 
86
- def _attributes
87
- @_attributes ||= {}
88
- end
95
+ attr_accessor :_klass, :_instance, :_transient_attributes
89
96
 
90
- def persist
91
- _instance.save! if _instance.respond_to?(:save!)
92
- end
97
+ def _attributes
98
+ @_attributes ||= {}
99
+ end
93
100
 
94
- def process_attributes(attributes)
95
- self._transient_attributes = Hash.new
96
- attributes.each do |attribute|
97
- _attributes[attribute.name] = attribute.processed_value(_attributes)
98
- _transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
101
+ def persist
102
+ _instance.save! if _instance.respond_to?(:save!)
103
+ end
104
+
105
+ def process_attributes(attributes)
106
+ self._transient_attributes = ({})
107
+ attributes.each do |attribute|
108
+ _attributes[attribute.name] = attribute.processed_value(_attributes)
109
+ _transient_attributes[attribute.name] = _attributes[attribute.name] if attribute.transient?
110
+ end
111
+ _attributes.reject! { |k| _transient_attributes.keys.include?(k) }
112
+ end
99
113
  end
100
- _attributes.reject! { |k| _transient_attributes.keys.include?(k) }
101
114
  end
102
-
103
115
  end
@@ -1,17 +1,19 @@
1
- class Fabrication::Generator::DataMapper < Fabrication::Generator::Base
1
+ module Fabrication
2
+ module Generator
3
+ class DataMapper < Fabrication::Generator::Base
4
+ def self.supports?(klass)
5
+ defined?(::DataMapper) && klass.ancestors.include?(::DataMapper::Hook)
6
+ end
2
7
 
3
- def self.supports?(klass)
4
- defined?(DataMapper) && klass.ancestors.include?(DataMapper::Hook)
5
- end
6
-
7
- def build_instance
8
- self._instance = _klass.new(_attributes)
9
- end
8
+ def build_instance
9
+ self._instance = _klass.new(_attributes)
10
+ end
10
11
 
11
- protected
12
+ protected
12
13
 
13
- def persist
14
- _instance.save
14
+ def persist
15
+ _instance.save
16
+ end
17
+ end
15
18
  end
16
-
17
19
  end