fabrication 2.21.0 → 3.0.0.beta.1.1

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 (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,6 +1,5 @@
1
1
  module Fabrication
2
2
  module Syntax
3
-
4
3
  # Extends Fabrication to provide make/make! class methods, which are
5
4
  # shortcuts for Fabricate.build/Fabricate.
6
5
  #
@@ -1,39 +1,37 @@
1
- class Fabrication::Transform
2
-
3
- class << self
4
-
5
- def apply_to(schematic, attributes_hash)
6
- Fabrication.manager.load_definitions if Fabrication.manager.empty?
7
- attributes_hash.inject({}) {|h,(k,v)| h.update(k => apply_transform(schematic, k, v)) }
1
+ module Fabrication
2
+ class Transform
3
+ class << self
4
+ def apply_to(schematic, attributes_hash)
5
+ Fabrication.manager.load_definitions if Fabrication.manager.empty?
6
+ attributes_hash.inject({}) { |h, (k, v)| h.update(k => apply_transform(schematic, k, v)) }
7
+ end
8
+
9
+ def clear_all
10
+ @transforms = nil
11
+ @overrides = nil
12
+ end
13
+
14
+ def define(attribute, transform)
15
+ transforms[attribute] = transform
16
+ end
17
+
18
+ def only_for(schematic, attribute, transform)
19
+ overrides[schematic] = (overrides[schematic] || {}).merge!(attribute => transform)
20
+ end
21
+
22
+ private
23
+
24
+ def overrides
25
+ @overrides ||= {}
26
+ end
27
+
28
+ def apply_transform(schematic, attribute, value)
29
+ overrides.fetch(schematic, transforms)[attribute].call(value)
30
+ end
31
+
32
+ def transforms
33
+ @transforms ||= Hash.new(->(value) { value })
34
+ end
8
35
  end
9
-
10
- def clear_all
11
- @transforms = nil
12
- @overrides = nil
13
- end
14
-
15
- def define(attribute, transform)
16
- transforms[attribute] = transform
17
- end
18
-
19
- def only_for(schematic, attribute, transform)
20
- overrides[schematic] = (overrides[schematic] || {}).merge!(attribute => transform)
21
- end
22
-
23
- private
24
-
25
- def overrides
26
- @overrides ||= {}
27
- end
28
-
29
- def apply_transform(schematic, attribute, value)
30
- overrides.fetch(schematic, transforms)[attribute].call(value)
31
- end
32
-
33
- def transforms
34
- @transforms ||= Hash.new(lambda {|value| value})
35
- end
36
-
37
36
  end
38
-
39
37
  end
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.21.0'.freeze
2
+ VERSION = '3.0.0.beta.1.1'.freeze
3
3
  end
@@ -3,13 +3,12 @@ require 'rails/generators/base'
3
3
  module Fabrication
4
4
  module Generators
5
5
  class CucumberStepsGenerator < Rails::Generators::Base
6
-
7
6
  def generate
8
- template 'fabrication_steps.rb', "features/step_definitions/fabrication_steps.rb"
7
+ template 'fabrication_steps.rb', 'features/step_definitions/fabrication_steps.rb'
9
8
  end
10
9
 
11
10
  def self.source_root
12
- @_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
13
12
  end
14
13
  end
15
14
  end
@@ -7,44 +7,44 @@ def with_ivars(fabricator)
7
7
  model = @they.last.class.to_s.underscore
8
8
  instance_variable_set("@#{model.pluralize}", @they)
9
9
  instance_variable_set("@#{model.singularize}", @they.last)
10
- Fabrication::Cucumber::Fabrications[model.singularize.gsub(/\W+/,'_').downcase] = @they.last
10
+ Fabrication::Cucumber::Fabrications[model.singularize.gsub(/\W+/, '_').downcase] = @they.last
11
11
  end
12
12
 
13
- Given /^(\d+) ([^"]*)$/ do |count, model_name|
13
+ Given(/^(\d+) ([^"]*)$/) do |count, model_name|
14
14
  with_ivars Fabrication::Cucumber::StepFabricator.new(model_name) do |fab|
15
15
  fab.n(count.to_i)
16
16
  end
17
17
  end
18
18
 
19
- Given /^the following ([^"]*):$/ do |model_name, table|
19
+ Given(/^the following ([^"]*):$/) do |model_name, table|
20
20
  with_ivars Fabrication::Cucumber::StepFabricator.new(model_name) do |fab|
21
21
  fab.from_table(table)
22
22
  end
23
23
  end
24
24
 
25
- Given /^that ([^"]*) has the following ([^"]*):$/ do |parent, child, table|
26
- with_ivars Fabrication::Cucumber::StepFabricator.new(child, :parent => parent) do |fab|
25
+ Given(/^that ([^"]*) has the following ([^"]*):$/) do |parent, child, table|
26
+ with_ivars Fabrication::Cucumber::StepFabricator.new(child, parent: parent) do |fab|
27
27
  fab.from_table(table)
28
28
  end
29
29
  end
30
30
 
31
- Given /^that ([^"]*) has (\d+) ([^"]*)$/ do |parent, count, child|
32
- with_ivars Fabrication::Cucumber::StepFabricator.new(child, :parent => parent) do |fab|
31
+ Given(/^that ([^"]*) has (\d+) ([^"]*)$/) do |parent, count, child|
32
+ with_ivars Fabrication::Cucumber::StepFabricator.new(child, parent: parent) do |fab|
33
33
  fab.n(count.to_i)
34
34
  end
35
35
  end
36
36
 
37
- Given /^(?:that|those) (.*) belongs? to that (.*)$/ do |children, parent|
37
+ Given(/^(?:that|those) (.*) belongs? to that (.*)$/) do |children, parent|
38
38
  with_ivars Fabrication::Cucumber::StepFabricator.new(parent) do |fab|
39
39
  fab.has_many(children)
40
40
  end
41
41
  end
42
42
 
43
- Then /^I should see (\d+) ([^"]*) in the database$/ do |count, model_name|
43
+ Then(/^I should see (\d+) ([^"]*) in the database$/) do |count, model_name|
44
44
  expect(Fabrication::Cucumber::StepFabricator.new(model_name).klass.count).to eq(count.to_i)
45
45
  end
46
46
 
47
- Then /^I should see the following (.*) in the database:$/ do |model_name, table|
47
+ Then(/^I should see the following (.*) in the database:$/) do |model_name, table|
48
48
  klass = Fabrication::Cucumber::StepFabricator.new(model_name).klass
49
49
  expect(klass.where(table.rows_hash.symbolize_keys).count).to eq(1)
50
50
  end
@@ -3,22 +3,23 @@ require 'rails/generators/named_base'
3
3
  module Fabrication
4
4
  module Generators
5
5
  class ModelGenerator < Rails::Generators::NamedBase
6
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
- class_option :dir, :type => :string, :default => "spec/fabricators", :desc => "The directory where the fabricators should go"
8
- class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
6
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
7
+ class_option :dir, type: :string, default: 'spec/fabricators',
8
+ desc: 'The directory where the fabricators should go'
9
+ class_option :extension, type: :string, default: 'rb', desc: 'file extension name'
9
10
 
10
11
  def create_fabrication_file
11
12
  copy_attributes_from_model if attributes.empty?
12
13
  template_file = File.join(
13
14
  options[:dir],
14
15
  class_path,
15
- "#{file_name}_fabricator.#{options[:extension].to_s}"
16
+ "#{file_name}_fabricator.#{options[:extension]}"
16
17
  )
17
18
  template 'fabricator.erb', template_file
18
19
  end
19
20
 
20
21
  def self.source_root
21
- @_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
22
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
22
23
  end
23
24
 
24
25
  private
@@ -26,14 +27,13 @@ module Fabrication
26
27
  def copy_attributes_from_model
27
28
  model = class_name.constantize
28
29
  if defined?(ActiveRecord) && model < ActiveRecord::Base
29
- self.attributes = model.columns_hash.map { |name, column|
30
+ self.attributes = model.columns_hash.map do |name, column|
30
31
  Rails::Generators::GeneratedAttribute.new(name, column.type)
31
- }
32
+ end
32
33
  end
33
- rescue
34
+ rescue StandardError
34
35
  # no table? no problem!
35
36
  end
36
-
37
37
  end
38
38
  end
39
39
  end
@@ -1,30 +1,30 @@
1
1
  namespace :fabrication do
2
- desc "Display all registered fabricators by class"
3
- task :list => :environment do
2
+ desc 'Display all registered fabricators by class'
3
+ task list: :environment do
4
4
  Fabrication.manager.load_definitions if Fabrication.manager.empty?
5
5
 
6
6
  if Fabrication.manager.schematics.none?
7
- puts "No fabricators found"
7
+ puts 'No fabricators found'
8
8
  next
9
9
  end
10
10
 
11
- groups = Fabrication.manager.schematics.group_by do |name, fabdef|
11
+ schematic_groups = Fabrication.manager.schematics.group_by do |_name, fabdef|
12
12
  fabdef.klass.name
13
13
  end
14
14
 
15
15
  fabricators = {}
16
- groups.sort_by { |klass, _| klass }.each do |klass, groups|
17
- fabricators[klass] = groups.map(&:first).sort.join(", ")
16
+ schematic_groups.sort_by { |klass, _| klass }.each do |klass, groups|
17
+ fabricators[klass] = groups.map(&:first).sort.join(', ')
18
18
  end
19
19
 
20
- class_width = fabricators.keys.max_by { |v| v.size }.size + 3 # padding
21
- names_width = fabricators.values.max_by { |v| v.size }.size
20
+ class_width = fabricators.keys.max_by(&:size).size + 3 # padding
21
+ names_width = fabricators.values.max_by(&:size).size
22
22
  say = lambda do |f1, f2|
23
23
  printf "%-#{class_width}s%-#{names_width}s\n", f1, f2
24
24
  end
25
25
 
26
- say["Class", "Fabricator"]
27
- puts "-" * (names_width + class_width)
28
- fabricators.each { |klass, names| say[klass,names] }
26
+ say['Class', 'Fabricator']
27
+ puts '-' * (names_width + class_width)
28
+ fabricators.each { |klass, names| say[klass, names] }
29
29
  end
30
30
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabrication
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.21.0
4
+ version: 3.0.0.beta.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Elliott
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-25 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fabrication is an object generation framework for ActiveRecord, Mongoid,
14
- DataMapper, Sequel, or any other Ruby object.
14
+ Sequel, or any other Ruby object.
15
15
  email:
16
16
  - paul@codingfrontier.com
17
17
  executables: []
@@ -32,8 +32,6 @@ files:
32
32
  - lib/fabrication/errors/unknown_fabricator_error.rb
33
33
  - lib/fabrication/generator/active_record.rb
34
34
  - lib/fabrication/generator/base.rb
35
- - lib/fabrication/generator/data_mapper.rb
36
- - lib/fabrication/generator/mongoid.rb
37
35
  - lib/fabrication/generator/sequel.rb
38
36
  - lib/fabrication/railtie.rb
39
37
  - lib/fabrication/schematic/attribute.rb
@@ -55,7 +53,7 @@ homepage: http://fabricationgem.org
55
53
  licenses:
56
54
  - MIT
57
55
  metadata: {}
58
- post_install_message:
56
+ post_install_message:
59
57
  rdoc_options: []
60
58
  require_paths:
61
59
  - lib
@@ -63,15 +61,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
61
  requirements:
64
62
  - - ">="
65
63
  - !ruby/object:Gem::Version
66
- version: 2.2.0
64
+ version: 2.5.0
67
65
  required_rubygems_version: !ruby/object:Gem::Requirement
68
66
  requirements:
69
- - - ">="
67
+ - - ">"
70
68
  - !ruby/object:Gem::Version
71
- version: '0'
69
+ version: 1.3.1
72
70
  requirements: []
73
- rubygems_version: 3.0.3
74
- signing_key:
71
+ rubygems_version: 3.1.6
72
+ signing_key:
75
73
  specification_version: 4
76
74
  summary: Implementing the factory pattern in Ruby so you don't have to.
77
75
  test_files: []
@@ -1,17 +0,0 @@
1
- class Fabrication::Generator::DataMapper < Fabrication::Generator::Base
2
-
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
10
-
11
- protected
12
-
13
- def persist
14
- _instance.save
15
- end
16
-
17
- end
@@ -1,15 +0,0 @@
1
- class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
2
-
3
- def self.supports?(klass)
4
- defined?(Mongoid) && klass.ancestors.include?(Mongoid::Document)
5
- end
6
-
7
- def build_instance
8
- if _klass.respond_to?(:protected_attributes)
9
- self._instance = _klass.new(_attributes, without_protection: true)
10
- else
11
- self._instance = _klass.new(_attributes)
12
- end
13
- end
14
-
15
- end