fabrication 2.21.0 → 3.0.0.beta.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) 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.rb +4 -9
  6. data/lib/fabrication/config.rb +23 -22
  7. data/lib/fabrication/cucumber/step_fabricator.rb +20 -13
  8. data/lib/fabrication/errors/duplicate_fabricator_error.rb +5 -3
  9. data/lib/fabrication/errors/infinite_recursion_error.rb +5 -3
  10. data/lib/fabrication/errors/misplaced_fabricate_error.rb +7 -3
  11. data/lib/fabrication/errors/unfabricatable_error.rb +5 -4
  12. data/lib/fabrication/errors/unknown_fabricator_error.rb +5 -5
  13. data/lib/fabrication/generator/active_record.rb +18 -15
  14. data/lib/fabrication/generator/base.rb +93 -81
  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 +139 -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 +57 -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/rails/generators/fabrication/cucumber_steps/cucumber_steps_generator.rb +2 -3
  28. data/lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb +10 -10
  29. data/lib/rails/generators/fabrication/model/model_generator.rb +9 -9
  30. data/lib/tasks/defined_fabricators.rake +11 -11
  31. metadata +10 -12
  32. data/lib/fabrication/generator/data_mapper.rb +0 -17
  33. data/lib/fabrication/generator/mongoid.rb +0 -15
@@ -1,70 +1,77 @@
1
- class Fabrication::Schematic::Evaluator < BasicObject
1
+ module Fabrication
2
+ module Schematic
3
+ class Evaluator < BasicObject
4
+ def process(definition, &block)
5
+ @_definition = definition
6
+ instance_eval(&block)
7
+ end
2
8
 
3
- def process(definition, &block)
4
- @_definition = definition
5
- instance_eval(&block)
6
- end
9
+ def respond_to_missing?(_method_name, _include_private = false)
10
+ true
11
+ end
7
12
 
8
- def method_missing(method_name, *args, &block)
9
- params = ::Fabrication::Support.extract_options!(args)
10
- value = args.first
11
- block = @_definition.generate_value(method_name, params) if args.empty? && !block
12
- @_definition.append_or_update_attribute(method_name, value, params, &block)
13
- end
13
+ def method_missing(method_name, *args, &block)
14
+ params = ::Fabrication::Support.extract_options!(args)
15
+ value = args.first
16
+ block = @_definition.generate_value(method_name, params) if args.empty? && !block
17
+ @_definition.append_or_update_attribute(method_name, value, params, &block)
18
+ end
14
19
 
15
- def after_build(&block)
16
- @_definition.callbacks[:after_build] ||= []
17
- @_definition.callbacks[:after_build] << block
18
- end
20
+ def after_build(&block)
21
+ @_definition.callbacks[:after_build] ||= []
22
+ @_definition.callbacks[:after_build] << block
23
+ end
19
24
 
20
- def before_validation(&block)
21
- @_definition.callbacks[:before_validation] ||= []
22
- @_definition.callbacks[:before_validation] << block
23
- end
25
+ def before_validation(&block)
26
+ @_definition.callbacks[:before_validation] ||= []
27
+ @_definition.callbacks[:before_validation] << block
28
+ end
24
29
 
25
- def after_validation(&block)
26
- @_definition.callbacks[:after_validation] ||= []
27
- @_definition.callbacks[:after_validation] << block
28
- end
30
+ def after_validation(&block)
31
+ @_definition.callbacks[:after_validation] ||= []
32
+ @_definition.callbacks[:after_validation] << block
33
+ end
29
34
 
30
- def before_save(&block)
31
- @_definition.callbacks[:before_save] ||= []
32
- @_definition.callbacks[:before_save] << block
33
- end
35
+ def before_save(&block)
36
+ @_definition.callbacks[:before_save] ||= []
37
+ @_definition.callbacks[:before_save] << block
38
+ end
34
39
 
35
- def before_create(&block)
36
- @_definition.callbacks[:before_create] ||= []
37
- @_definition.callbacks[:before_create] << block
38
- end
40
+ def before_create(&block)
41
+ @_definition.callbacks[:before_create] ||= []
42
+ @_definition.callbacks[:before_create] << block
43
+ end
39
44
 
40
- def after_create(&block)
41
- @_definition.callbacks[:after_create] ||= []
42
- @_definition.callbacks[:after_create] << block
43
- end
45
+ def after_create(&block)
46
+ @_definition.callbacks[:after_create] ||= []
47
+ @_definition.callbacks[:after_create] << block
48
+ end
44
49
 
45
- def after_save(&block)
46
- @_definition.callbacks[:after_save] ||= []
47
- @_definition.callbacks[:after_save] << block
48
- end
50
+ def after_save(&block)
51
+ @_definition.callbacks[:after_save] ||= []
52
+ @_definition.callbacks[:after_save] << block
53
+ end
49
54
 
50
- def on_init(&block)
51
- @_definition.callbacks[:on_init] = block
52
- end
55
+ def on_init(&block)
56
+ @_definition.callbacks[:on_init] = block
57
+ end
53
58
 
54
- def initialize_with(&block)
55
- @_definition.callbacks[:initialize_with] = block
56
- end
59
+ def initialize_with(&block)
60
+ @_definition.callbacks[:initialize_with] = block
61
+ end
57
62
 
58
- def init_with(*args)
59
- args
60
- end
63
+ def init_with(*args)
64
+ args
65
+ end
61
66
 
62
- def transient(*field_names)
63
- field_names.each do |field_name|
64
- if field_name.kind_of?(::Hash)
65
- field_name.each_pair { |name, value| @_definition.append_or_update_attribute(name, value, transient: true) }
66
- else
67
- @_definition.append_or_update_attribute(field_name, nil, transient: true)
67
+ def transient(*field_names)
68
+ field_names.each do |field_name|
69
+ if field_name.is_a?(::Hash)
70
+ field_name.each_pair { |name, value| @_definition.append_or_update_attribute(name, value, transient: true) }
71
+ else
72
+ @_definition.append_or_update_attribute(field_name, nil, transient: true)
73
+ end
74
+ end
68
75
  end
69
76
  end
70
77
  end
@@ -1,80 +1,88 @@
1
1
  require 'singleton'
2
2
 
3
- class Fabrication::Schematic::Manager
4
- include Singleton
3
+ module Fabrication
4
+ module Schematic
5
+ class Manager
6
+ include Singleton
7
+
8
+ def preinitialize
9
+ @initializing = true
10
+ clear
11
+ end
5
12
 
6
- def preinitialize
7
- @initializing = true
8
- clear
9
- end
13
+ def initializing?
14
+ @initializing ||= nil
15
+ end
10
16
 
11
- def initializing?
12
- @initializing ||= nil
13
- end
17
+ def schematics
18
+ @schematics ||= {}
19
+ end
14
20
 
15
- def schematics
16
- @schematics ||= {}
17
- end
21
+ def clear
22
+ schematics.clear
23
+ end
18
24
 
19
- def clear; schematics.clear end
20
- def empty?; schematics.empty? end
25
+ def empty?
26
+ schematics.empty?
27
+ end
21
28
 
22
- def freeze
23
- @initializing = false
24
- end
29
+ def freeze
30
+ @initializing = false
31
+ end
25
32
 
26
- def register(name, options, &block)
27
- name = name.to_sym
28
- raise_if_registered(name)
29
- store(name, Array(options.delete(:aliases)), options, &block)
30
- end
33
+ def register(name, options, &block)
34
+ name = name.to_sym
35
+ raise_if_registered(name)
36
+ store(name, Array(options.delete(:aliases)), options, &block)
37
+ end
31
38
 
32
- def [](name)
33
- schematics[name.to_sym]
34
- end
39
+ def [](name)
40
+ schematics[name.to_sym]
41
+ end
35
42
 
36
- def create_stack
37
- @create_stack ||= []
38
- end
43
+ def create_stack
44
+ @create_stack ||= []
45
+ end
39
46
 
40
- def build_stack
41
- @build_stack ||= []
42
- end
47
+ def build_stack
48
+ @build_stack ||= []
49
+ end
43
50
 
44
- def to_params_stack
45
- @to_params_stack ||= []
46
- end
51
+ def to_params_stack
52
+ @to_params_stack ||= []
53
+ end
47
54
 
48
- def load_definitions
49
- preinitialize
50
- Fabrication::Config.path_prefixes.each do |prefix|
51
- Fabrication::Config.fabricator_paths.each do |folder|
52
- Dir.glob(File.join(prefix.to_s, folder, '**', '*.rb')).sort.each do |file|
53
- load file
55
+ def load_definitions
56
+ preinitialize
57
+ Fabrication::Config.path_prefixes.each do |prefix|
58
+ Fabrication::Config.fabricator_paths.each do |folder|
59
+ Dir.glob(File.join(prefix.to_s, folder, '**', '*.rb')).sort.each do |file|
60
+ load file
61
+ end
62
+ end
54
63
  end
64
+ rescue StandardError => e
65
+ raise e
66
+ ensure
67
+ freeze
55
68
  end
56
- end
57
- rescue Exception => e
58
- raise e
59
- ensure
60
- freeze
61
- end
62
69
 
63
- def prevent_recursion!
64
- (create_stack + build_stack + to_params_stack).group_by(&:to_sym).each do |name, values|
65
- raise Fabrication::InfiniteRecursionError.new(name) if values.length > Fabrication::Config.recursion_limit
66
- end
67
- end
70
+ def prevent_recursion!
71
+ (create_stack + build_stack + to_params_stack).group_by(&:to_sym).each do |name, values|
72
+ raise Fabrication::InfiniteRecursionError, name if values.length > Fabrication::Config.recursion_limit
73
+ end
74
+ end
68
75
 
69
- protected
76
+ protected
70
77
 
71
- def raise_if_registered(name)
72
- (raise Fabrication::DuplicateFabricatorError, name) if self[name]
73
- end
78
+ def raise_if_registered(name)
79
+ (raise Fabrication::DuplicateFabricatorError, name) if self[name]
80
+ end
74
81
 
75
- def store(name, aliases, options, &block)
76
- schematic = schematics[name] = Fabrication::Schematic::Definition.new(name, options, &block)
77
- aliases.each { |as| schematics[as.to_sym] = schematic }
82
+ def store(name, aliases, options, &block)
83
+ schematic = schematics[name] = Fabrication::Schematic::Definition.new(name, options, &block)
84
+ aliases.each { |as| schematics[as.to_sym] = schematic }
85
+ end
86
+ end
78
87
  end
79
-
80
88
  end
@@ -1,13 +1,16 @@
1
- class Fabrication::Schematic::Runner
1
+ module Fabrication
2
+ module Schematic
3
+ class Runner
4
+ attr_accessor :klass
2
5
 
3
- attr_accessor :klass
6
+ def initialize(klass)
7
+ self.klass = klass
8
+ end
4
9
 
5
- def initialize(klass)
6
- self.klass = klass
7
- end
8
-
9
- def sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
10
- name = "#{klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
11
- Fabrication::Sequencer.sequence(name, start, &block)
10
+ def sequence(name = Fabrication::Sequencer::DEFAULT, start = nil, &block)
11
+ name = "#{klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
12
+ Fabrication::Sequencer.sequence(name, start, &block)
13
+ end
14
+ end
12
15
  end
13
16
  end
@@ -1,29 +1,30 @@
1
- class Fabrication::Sequencer
1
+ module Fabrication
2
+ class Sequencer
3
+ DEFAULT = :_default
2
4
 
3
- DEFAULT = :_default
4
-
5
- def self.sequence(name=DEFAULT, start=nil, &block)
6
- idx = sequences[name] ||= start || Fabrication::Config.sequence_start
7
- if block_given?
8
- sequence_blocks[name] = block.to_proc
9
- else
10
- sequence_blocks[name] ||= lambda { |i| i }
11
- end.call(idx).tap do
12
- sequences[name] += 1
5
+ def self.sequence(name = DEFAULT, start = nil, &block)
6
+ idx = sequences[name] ||= start || Fabrication::Config.sequence_start
7
+ if block_given?
8
+ sequence_blocks[name] = block.to_proc
9
+ else
10
+ sequence_blocks[name] ||= ->(i) { i }
11
+ end.call(idx).tap do
12
+ sequences[name] += 1
13
+ end
13
14
  end
14
- end
15
15
 
16
- def self.sequences
17
- @sequences ||= {}
18
- end
16
+ def self.sequences
17
+ @sequences ||= {}
18
+ end
19
19
 
20
- def self.sequence_blocks
21
- @sequence_blocks ||= {}
22
- end
20
+ def self.sequence_blocks
21
+ @sequence_blocks ||= {}
22
+ end
23
23
 
24
- def self.reset
25
- Fabrication::Config.sequence_start = nil
26
- @sequences = nil
27
- @sequence_blocks = nil
24
+ def self.reset
25
+ Fabrication::Config.sequence_start = nil
26
+ @sequences = nil
27
+ @sequence_blocks = nil
28
+ end
28
29
  end
29
30
  end
@@ -1,70 +1,73 @@
1
- class Fabrication::Support
1
+ module Fabrication
2
+ class Support
3
+ class << self
4
+ def fabricatable?(name)
5
+ Fabrication.manager[name] || class_for(name)
6
+ end
2
7
 
3
- class << self
8
+ def class_for(class_or_to_s)
9
+ class_name = variable_name_to_class_name(class_or_to_s)
10
+ constantize(class_name)
11
+ rescue NameError => e
12
+ raise Fabrication::UnfabricatableError.new(class_or_to_s, e)
13
+ end
4
14
 
5
- def fabricatable?(name)
6
- Fabrication.manager[name] || class_for(name)
7
- end
15
+ def constantize(camel_cased_word)
16
+ names = camel_cased_word.split('::')
17
+ Object.const_get(camel_cased_word) if names.empty?
18
+ names.shift if names.size > 1 && names.first.empty?
19
+ names.inject(Object) do |constant, name|
20
+ if constant == Object
21
+ constant.const_get(name)
22
+ else
23
+ candidate = constant.const_get(name)
24
+ next candidate if constant.const_defined?(name, false)
25
+ next candidate unless Object.const_defined?(name)
8
26
 
9
- def class_for(class_or_to_s)
10
- class_name = variable_name_to_class_name(class_or_to_s)
11
- constantize(class_name)
12
- rescue NameError => original_error
13
- raise Fabrication::UnfabricatableError.new(class_or_to_s, original_error)
14
- end
27
+ constant = constant.ancestors.inject do |const, ancestor|
28
+ break const if ancestor == Object
29
+ break ancestor if ancestor.const_defined?(name, false)
15
30
 
16
- def constantize(camel_cased_word)
17
- names = camel_cased_word.split('::')
18
- Object.const_get(camel_cased_word) if names.empty?
19
- names.shift if names.size > 1 && names.first.empty?
20
- names.inject(Object) do |constant, name|
21
- if constant == Object
22
- constant.const_get(name)
23
- else
24
- candidate = constant.const_get(name)
25
- next candidate if constant.const_defined?(name, false)
26
- next candidate unless Object.const_defined?(name)
27
- constant = constant.ancestors.inject do |const, ancestor|
28
- break const if ancestor == Object
29
- break ancestor if ancestor.const_defined?(name, false)
30
- const
31
+ const
32
+ end
33
+ constant.const_get(name, false)
31
34
  end
32
- constant.const_get(name, false)
33
35
  end
34
36
  end
35
- end
36
37
 
37
- def extract_options!(args)
38
- args.last.is_a?(::Hash) ? args.pop : {}
39
- end
38
+ def extract_options!(args)
39
+ args.last.is_a?(::Hash) ? args.pop : {}
40
+ end
40
41
 
41
- def variable_name_to_class_name(name)
42
- name.to_s.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase}
43
- end
42
+ def variable_name_to_class_name(name)
43
+ name_string = name.to_s
44
44
 
45
- def find_definitions
46
- puts "DEPRECATION WARNING: Fabrication::Support.find_definitions has been replaced by Fabrication.manager.load_definitions and will be removed in 3.0.0."
47
- Fabrication.manager.load_definitions
48
- end
45
+ if name_string.respond_to?(:camelize)
46
+ name_string.camelize
47
+ else
48
+ name_string.gsub(%r{/(.?)}) do
49
+ "::#{Regexp.last_match(1).upcase}"
50
+ end.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
51
+ end
52
+ end
49
53
 
50
- def hash_class
51
- @hash_class ||= defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess : Hash
52
- end
54
+ def hash_class
55
+ @hash_class ||= defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess : Hash
56
+ end
53
57
 
54
- def singularize(string)
55
- string.singularize
56
- rescue
57
- string.end_with?('s') ? string[0..-2] : string
58
- end
58
+ def singularize(string)
59
+ string.singularize
60
+ rescue StandardError
61
+ string.end_with?('s') ? string[0..-2] : string
62
+ end
59
63
 
60
- def underscore(string)
61
- string.gsub(/::/, '/').
62
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
63
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
64
- tr("-", "_").
65
- downcase
64
+ def underscore(string)
65
+ string.gsub(/::/, '/')
66
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
67
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
68
+ .tr('-', '_')
69
+ .downcase
70
+ end
66
71
  end
67
-
68
72
  end
69
-
70
73
  end