fabrication 2.9.3 → 2.9.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d930b692979d30d6a650770510fd500e7aaa24a6
4
- data.tar.gz: d87145180ccca5bc06f330b7d515095ca345587b
3
+ metadata.gz: 60ffbcf29301152a8dafc8037856a47f52680f78
4
+ data.tar.gz: e02c67ec746e96b5c9649dc64d116a83736922e1
5
5
  SHA512:
6
- metadata.gz: 11e14b064efdf85febee306ff937efa7e64afb26472661d5c24403e6c9c626aa3a2d6cbb80334cc2f3a5d9a28b89786ce35d1a2326ac83ee9f04edcf2d093c8a
7
- data.tar.gz: dacff28966109aea4f14851bcddf7f24e36f8fd6a791d4ed5180f98ea235255b4a9b82477f0ffda7bc95613035fa53e684becd5536e5b6a92b079aba8640dc55
6
+ metadata.gz: e4fd4365c2eeb9aa1bdf8e7af6ec6529b6980678364b50602b23983509307ddd22f920318a31446054df76276ba0b8067f50a9f4c40710f9fb4e3b3b1ee2cd45
7
+ data.tar.gz: dd8f177414850efdbc18593b4a6c555c135d4513773fdf185f844987d9e57841cd28a5ff2830948767196750987e2dad3584ce4735050786c08ddf5d06f94585
@@ -1,16 +1,13 @@
1
- class Fabrication::Fabricator
2
-
3
- def self.build(name, overrides={}, &block)
4
- fail_if_initializing(name)
5
- schematic(name).build(overrides, &block)
1
+ class Fabricate
2
+ def self.times(count, name, overrides={}, &block)
3
+ count.times.map { Fabricate(name, overrides, &block) }
6
4
  end
7
5
 
8
- def self.fabricate(name, overrides={}, &block)
9
- fail_if_initializing(name)
10
- schematic(name).fabricate(overrides, &block)
6
+ def self.build_times(count, name, overrides={}, &block)
7
+ count.times.map { Fabricate.build(name, overrides, &block) }
11
8
  end
12
9
 
13
- def self.to_attributes(name, overrides={}, &block)
10
+ def self.attributes_for(name, overrides={}, &block)
14
11
  fail_if_initializing(name)
15
12
  schematic(name).to_attributes(overrides, &block)
16
13
  end
@@ -20,6 +17,22 @@ class Fabrication::Fabricator
20
17
  schematic(name).to_params(overrides, &block)
21
18
  end
22
19
 
20
+ def self.build(name, overrides={}, &block)
21
+ fail_if_initializing(name)
22
+ schematic(name).build(overrides, &block).tap do |object|
23
+ Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
24
+ end
25
+ end
26
+
27
+ def self.create(name, overrides={}, &block)
28
+ fail_if_initializing(name)
29
+ schematic(name).fabricate(overrides, &block)
30
+ end
31
+
32
+ def self.sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
33
+ Fabrication::Sequencer.sequence(name, start, &block)
34
+ end
35
+
23
36
  def self.schematic(name)
24
37
  Fabrication::Support.find_definitions if Fabrication.manager.empty?
25
38
  Fabrication.manager[name] || raise(Fabrication::UnknownFabricatorError.new(name))
data/lib/fabrication.rb CHANGED
@@ -1,3 +1,7 @@
1
+ autoload :Fabricate, 'fabricate'
2
+
3
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f } if defined?(Rake)
4
+
1
5
  module Fabrication
2
6
  autoload :DuplicateFabricatorError, 'fabrication/errors/duplicate_fabricator_error'
3
7
  autoload :UnfabricatableError, 'fabrication/errors/unfabricatable_error'
@@ -13,7 +17,6 @@ module Fabrication
13
17
  end
14
18
 
15
19
  autoload :Config, 'fabrication/config'
16
- autoload :Fabricator, 'fabrication/fabricator'
17
20
  autoload :Sequencer, 'fabrication/sequencer'
18
21
  autoload :Support, 'fabrication/support'
19
22
  autoload :Transform, 'fabrication/transform'
@@ -30,7 +33,7 @@ module Fabrication
30
33
  end
31
34
 
32
35
  def self.clear_definitions
33
- @manager = nil
36
+ manager.clear
34
37
  Sequencer.sequences.clear
35
38
  end
36
39
 
@@ -39,7 +42,7 @@ module Fabrication
39
42
  end
40
43
 
41
44
  def self.manager
42
- @manager ||= Fabrication::Schematic::Manager.new
45
+ @manager ||= Fabrication::Schematic::Manager.instance
43
46
  end
44
47
 
45
48
  def self.schematics
@@ -53,39 +56,11 @@ def Fabricator(name, options={}, &block)
53
56
  end
54
57
 
55
58
  def Fabricate(name, overrides={}, &block)
56
- Fabrication::Fabricator.fabricate(name, overrides, &block).tap do |object|
59
+ Fabricate.create(name, overrides, &block).tap do |object|
57
60
  Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
58
61
  end
59
62
  end
60
63
 
61
- class Fabricate
62
- def self.times(count, name, overrides={}, &block)
63
- count.times.map { Fabricate(name, overrides, &block) }
64
- end
65
-
66
- def self.build_times(count, name, overrides={}, &block)
67
- count.times.map { Fabricate.build(name, overrides, &block) }
68
- end
69
-
70
- def self.attributes_for(name, overrides={}, &block)
71
- Fabrication::Fabricator.to_attributes(name, overrides, &block)
72
- end
73
-
74
- def self.to_params(name, overrides={}, &block)
75
- Fabrication::Fabricator.to_params(name, overrides, &block)
76
- end
77
-
78
- def self.build(name, overrides={}, &block)
79
- Fabrication::Fabricator.build(name, overrides, &block).tap do |object|
80
- Fabrication::Cucumber::Fabrications[name] = object if Fabrication::Config.register_with_steps?
81
- end
82
- end
83
-
84
- def self.sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
85
- Fabrication::Sequencer.sequence(name, start, &block)
86
- end
87
- end
88
-
89
64
  module FabricationMethods
90
65
  def fabrications
91
66
  Fabrication::Cucumber::Fabrications
@@ -36,7 +36,7 @@ module Fabrication
36
36
  end
37
37
 
38
38
  def klass
39
- Fabrication::Fabricator.schematic(@fabricator).klass
39
+ Fabricate.schematic(@fabricator).klass
40
40
  end
41
41
 
42
42
  private
@@ -1 +1,5 @@
1
- class Fabrication::DuplicateFabricatorError < StandardError; end
1
+ class Fabrication::DuplicateFabricatorError < StandardError
2
+ def initialize(string)
3
+ super("'#{string}' is already defined")
4
+ end
5
+ end
@@ -5,7 +5,7 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
5
5
  end
6
6
 
7
7
  def build_instance
8
- if self.class.without_protection?
8
+ if _klass.respond_to?(:protected_attributes)
9
9
  self._instance = _klass.new(_attributes, without_protection: true)
10
10
  else
11
11
  self._instance = _klass.new(_attributes)
@@ -14,11 +14,6 @@ class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base
14
14
 
15
15
  protected
16
16
 
17
- def self.without_protection?
18
- @without_protection = Gem::Version.new(ActiveRecord::VERSION::STRING).between?(Gem::Version.new('3.1.0'), Gem::Version.new('3.9')) if @without_protection.nil?
19
- @without_protection
20
- end
21
-
22
17
  def validate_instance
23
18
  _instance.valid?
24
19
  end
@@ -5,7 +5,7 @@ class Fabrication::Generator::Mongoid < Fabrication::Generator::Base
5
5
  end
6
6
 
7
7
  def build_instance
8
- if Gem::Version.new(Mongoid::VERSION).between?(Gem::Version.new('2.3.0'), Gem::Version.new('3.9'))
8
+ if _klass.respond_to?(:protected_attributes)
9
9
  self._instance = _klass.new(_attributes, without_protection: true)
10
10
  else
11
11
  self._instance = _klass.new(_attributes)
@@ -1,4 +1,5 @@
1
1
  class Fabrication::Schematic::Manager
2
+ include Singleton
2
3
 
3
4
  def preinitialize
4
5
  @initializing = true
@@ -7,16 +8,13 @@ class Fabrication::Schematic::Manager
7
8
 
8
9
  def initializing?; @initializing end
9
10
 
10
- def freeze
11
- @initializing = false
12
- end
13
-
14
- def clear
15
- schematics.clear
11
+ def schematics
12
+ @schematics ||= {}
16
13
  end
14
+ delegate :clear, :empty?, to: :schematics
17
15
 
18
- def empty?
19
- schematics.empty?
16
+ def freeze
17
+ @initializing = false
20
18
  end
21
19
 
22
20
  def register(name, options, &block)
@@ -29,10 +27,6 @@ class Fabrication::Schematic::Manager
29
27
  schematics[name.to_sym]
30
28
  end
31
29
 
32
- def schematics
33
- @schematics ||= {}
34
- end
35
-
36
30
  def build_stack
37
31
  @build_stack ||= []
38
32
  end
@@ -44,9 +38,7 @@ class Fabrication::Schematic::Manager
44
38
  protected
45
39
 
46
40
  def raise_if_registered(name)
47
- if self[name]
48
- raise Fabrication::DuplicateFabricatorError, "'#{name}' is already defined"
49
- end
41
+ (raise Fabrication::DuplicateFabricatorError, name) if self[name]
50
42
  end
51
43
 
52
44
  def store(name, aliases, options, &block)
@@ -60,7 +52,7 @@ class Fabrication::Schematic::Manager
60
52
  (parent && parent.klass) ||
61
53
  options[:from] ||
62
54
  name
63
- ) || (raise Fabrication::UnfabricatableError.new(name))
55
+ )
64
56
  end
65
57
 
66
58
  def schematic_for(name, options, &block)
@@ -7,15 +7,13 @@ class Fabrication::Support
7
7
  end
8
8
 
9
9
  def class_for(class_or_to_s)
10
- if class_or_to_s.respond_to?(:to_sym)
11
- class_name = variable_name_to_class_name(class_or_to_s)
12
- class_name.split('::').inject(Object) do |object, string|
13
- object.const_get(string)
14
- end
15
- else
16
- class_or_to_s
10
+ class_name = variable_name_to_class_name(class_or_to_s)
11
+ klass = class_name.split('::').inject(Object) do |object, string|
12
+ object.const_get(string)
17
13
  end
18
14
  rescue NameError
15
+ ensure
16
+ raise Fabrication::UnfabricatableError.new(class_or_to_s) unless klass
19
17
  end
20
18
 
21
19
  def extract_options!(args)
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '2.9.3'
2
+ VERSION = '2.9.4'
3
3
  end
@@ -0,0 +1,30 @@
1
+ namespace :fabrication do
2
+ desc "Display all registered fabricators by class"
3
+ task :list => :environment do
4
+ Fabrication::Support.find_definitions if Fabrication.manager.empty?
5
+
6
+ if Fabrication.manager.schematics.none?
7
+ puts "No fabricators found"
8
+ next
9
+ end
10
+
11
+ groups = Fabrication.manager.schematics.group_by do |name, fabdef|
12
+ fabdef.klass.name
13
+ end
14
+
15
+ fabricators = {}
16
+ groups.sort_by { |klass, _| klass }.each do |klass, groups|
17
+ fabricators[klass] = groups.map(&:first).sort.join(", ")
18
+ end
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
22
+ say = lambda do |f1, f2|
23
+ printf "%-#{class_width}s%-#{names_width}s\n", f1, f2
24
+ end
25
+
26
+ say["Class", "Fabricator"]
27
+ puts "-" * (names_width + class_width)
28
+ fabricators.each { |klass, names| say[klass,names] }
29
+ end
30
+ end
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.9.3
4
+ version: 2.9.4
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-12 00:00:00.000000000 Z
11
+ date: 2013-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -140,16 +140,16 @@ dependencies:
140
140
  name: mongoid
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - '>='
143
+ - - ~>
144
144
  - !ruby/object:Gem::Version
145
- version: '0'
145
+ version: '3.0'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - '>='
150
+ - - ~>
151
151
  - !ruby/object:Gem::Version
152
- version: '0'
152
+ version: '3.0'
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: pry
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -214,13 +214,13 @@ executables: []
214
214
  extensions: []
215
215
  extra_rdoc_files: []
216
216
  files:
217
+ - lib/fabricate.rb
217
218
  - lib/fabrication/config.rb
218
219
  - lib/fabrication/cucumber/step_fabricator.rb
219
220
  - lib/fabrication/errors/duplicate_fabricator_error.rb
220
221
  - lib/fabrication/errors/misplaced_fabricate_error.rb
221
222
  - lib/fabrication/errors/unfabricatable_error.rb
222
223
  - lib/fabrication/errors/unknown_fabricator_error.rb
223
- - lib/fabrication/fabricator.rb
224
224
  - lib/fabrication/generator/active_record.rb
225
225
  - lib/fabrication/generator/base.rb
226
226
  - lib/fabrication/generator/data_mapper.rb
@@ -241,6 +241,7 @@ files:
241
241
  - lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb
242
242
  - lib/rails/generators/fabrication/model/model_generator.rb
243
243
  - lib/rails/generators/fabrication/model/templates/fabricator.rb
244
+ - lib/tasks/defined_fabricators.rake
244
245
  - LICENSE
245
246
  - README.markdown
246
247
  - Rakefile
@@ -264,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
264
265
  version: '0'
265
266
  requirements: []
266
267
  rubyforge_project:
267
- rubygems_version: 2.1.3
268
+ rubygems_version: 2.1.11
268
269
  signing_key:
269
270
  specification_version: 4
270
271
  summary: Fabrication provides a simple solution for test object generation.