dfl-factories-and-workers 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,7 +1,14 @@
1
+ * [10/22/2008] - 0.1.3
2
+
3
+ FactoryBuilder was using #classify which also calls #singularize... now uses camelize instead.
4
+ Removed use of const_get in favor of #constantize
5
+ Removed use of method missing in factory_builder
6
+ Removed TestFactories module namespace from factories.rb.. back to mixing into Object
7
+
1
8
  * [10/15/2008] - 0.1.1
2
9
 
3
10
  Fixed problems with ./script/generate migration and method missing
4
- factories.rb now requires a TestFactories module namespace!
11
+ factories.rb now requires a TestFactories module namespace
5
12
 
6
13
  * [10/14/2008] - 0.1.0
7
14
 
@@ -9,6 +16,5 @@
9
16
 
10
17
  * [08/05/2008] - 0.0.1 (gem release)
11
18
 
12
- Initial release as a gem.
13
-
14
- Removed some Rails specific code, but the gem is still dependent on activerecord.
19
+ Initial release as a gem.
20
+ Removed some Rails specific code, but the gem is still dependent on ActiveRecord.
data/README.rdoc CHANGED
@@ -34,11 +34,7 @@ It will be loaded during plugin initialization (see init.rb). You could also exp
34
34
 
35
35
  A minimal factories.rb file:
36
36
 
37
- module TestFactories # NOTE: this module namespace is a new addition from v0.1.1 onwards
38
- include FactoriesAndWorkers::Factory # mixin to Object, which is especially useful for script/console work; season to taste.
39
-
40
37
  factory :user, :name => "default name"
41
- end
42
38
 
43
39
  This would define methods named +build_user+, +create_user+, and +valid_user_attributes+ to be available in any Object in the project.
44
40
  If you wanted to override the default valid attributes, you could call:
@@ -2,28 +2,16 @@ require 'digest/sha1'
2
2
 
3
3
  module FactoriesAndWorkers
4
4
 
5
- module Factory
5
+ module Factory
6
6
  def self.included( base )
7
- base.extend ClassMethods
8
-
9
- # factory methods are defined as class methods; this delegation will allow them to also be called as instance methods
10
- def method_missing method, *args, &block
11
- if ClassMethods.method_defined?(method)
12
- self.class.send method, *args, &block
13
- else
14
- super
15
- end
16
- end
17
7
 
18
- end
19
-
20
- module ClassMethods
21
8
  def factory( kind, default_attrs, opts={}, &block )
22
9
  FactoryBuilder.new( kind, default_attrs, opts, self, &block )
23
10
  end
24
11
 
25
12
  # creates a random hex string, converts it to hexatridecimal, and truncates to desired length (max 30)
26
13
  def uniq len=10
14
+ raise ArgumentError, "Factory::uniq - length must be <= 30" if len > 30
27
15
  Digest::SHA1.hexdigest("#{rand(1<<64)}/#{Time.now.to_f}/#{Process.pid}").to_i(16).to_s(36)[1..len]
28
16
  end
29
17
 
@@ -36,21 +24,21 @@ module FactoriesAndWorkers
36
24
  def factory_initializers
37
25
  @@factory_initializers
38
26
  end
39
- end
40
27
 
28
+ end
41
29
  end
42
30
 
43
31
  class FactoryBuilder
44
32
  def initialize( factory, default_attrs, opts, from_klass, &block )
45
33
  raise ArgumentError, ":chain must be a lambda block!" if opts[:chain] && !opts[:chain].is_a?( Proc )
46
34
  opts.reverse_merge!( :class => factory )
47
-
48
- ar_klass = ActiveRecord.const_get( opts[:class].to_s.classify )
35
+
36
+ ar_klass = opts[:class].to_s.camelize.constantize
49
37
  from_klass.factory_initializers[ factory ] = block if block_given?
50
38
 
51
39
  # make the valid attributes method
52
40
  valid_attrs_method = :"valid_#{factory}_attributes"
53
- Factory::ClassMethods.send :define_method, valid_attrs_method do |*args|
41
+ Factory.send :define_method, valid_attrs_method do |*args|
54
42
  action = args.first.is_a?( TrueClass ) ? :create : :build
55
43
  attrs = default_attrs.symbolize_keys
56
44
  attr_overrides = args.extract_options!
@@ -77,7 +65,7 @@ module FactoriesAndWorkers
77
65
 
78
66
  # make the valid attribute method, which only fetches a single attribute
79
67
  valid_attr_method = :"valid_#{factory}_attribute"
80
- Factory::ClassMethods.send :define_method, valid_attr_method do |arg|
68
+ Factory.send :define_method, valid_attr_method do |arg|
81
69
  return unless arg.is_a?( Symbol )
82
70
  base = default_attrs.dup
83
71
  base.reverse_merge!( opts[:chain].call ) if opts[:chain]
@@ -87,21 +75,21 @@ module FactoriesAndWorkers
87
75
  end
88
76
 
89
77
  # alias default_*_attributes to valid_*_attributes, for semantic equivalency
90
- Factory::ClassMethods.send :alias_method, valid_attrs_method.to_s.gsub('valid','default').to_sym, valid_attrs_method
91
- Factory::ClassMethods.send :alias_method, valid_attr_method.to_s.gsub('valid','default').to_sym, valid_attr_method
78
+ Factory.send :alias_method, valid_attrs_method.to_s.gsub('valid','default').to_sym, valid_attrs_method
79
+ Factory.send :alias_method, valid_attr_method.to_s.gsub('valid','default').to_sym, valid_attr_method
92
80
 
93
81
 
94
82
  after_initialize_block = from_klass.factory_initializers[ factory ]
95
83
 
96
84
  # make the create method
97
- Factory::ClassMethods.send :define_method, :"create_#{factory}" do |*args|
85
+ Factory.send :define_method, :"create_#{factory}" do |*args|
98
86
  ar_klass.create!( self.send( valid_attrs_method, true, args.first ) ) do |obj|
99
87
  after_initialize_block.call( obj ) if after_initialize_block
100
88
  end
101
89
  end
102
90
 
103
91
  # make the build method
104
- Factory::ClassMethods.send :define_method, :"build_#{factory}" do |*args|
92
+ Factory.send :define_method, :"build_#{factory}" do |*args|
105
93
  ar_klass.new( self.send( valid_attrs_method, false, args.first ) ) do |obj|
106
94
  after_initialize_block.call( obj ) if after_initialize_block
107
95
  end
@@ -5,6 +5,12 @@ module FactoriesAndWorkers
5
5
  FactoryWorker.find_and_work worker_name
6
6
  end
7
7
 
8
+ module ClassMethods
9
+ def factory_worker( worker, &block )
10
+ FactoryWorker.new( worker, &block )
11
+ end
12
+ end
13
+
8
14
  def self.included( base )
9
15
  base.extend ClassMethods
10
16
  # factory_worker methods are defined as class methods; this delegation will allow them to also be called as instance methods
@@ -17,12 +23,6 @@ module FactoriesAndWorkers
17
23
  end
18
24
  end
19
25
 
20
- module ClassMethods
21
- def factory_worker( worker, &block )
22
- FactoryWorker.new( worker, &block )
23
- end
24
- end
25
-
26
26
  end
27
27
 
28
28
  class FactoryWorker
data/rails/init.rb CHANGED
@@ -2,12 +2,12 @@ require 'fileutils'
2
2
 
3
3
  config.after_initialize do
4
4
 
5
- if script_console_running = defined?(::IRB) && ::IRB.conf[:LOAD_MODULES] && ::IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
6
- # mixin to Object if we are running in ./script/console
5
+ # if script_console_running = defined?(::IRB) && ::IRB.conf[:LOAD_MODULES] && ::IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
6
+ # # mixin to Object if we are running in ./script/console
7
7
  obj = Object
8
- else # otherwise just mix into Test::Unit to play it safe
9
- obj = Test::Unit::TestCase
10
- end
8
+ # else # otherwise just mix into Test::Unit to play it safe
9
+ # obj = Test::Unit::TestCase
10
+ # end
11
11
 
12
12
  # mixin plugin
13
13
  obj.send :include, FactoriesAndWorkers::Factory
data/test/factories.rb CHANGED
@@ -1,21 +1,19 @@
1
- # module TestFactories
2
- include FactoriesAndWorkers::Factory
1
+ include FactoriesAndWorkers::Factory
3
2
 
4
- factory :monkey, {
5
- :name => "George",
6
- :unique => "$UNIQ(10)",
7
- :counter => "$COUNT",
8
- :number => lambda{ increment! :foo }
9
- }
3
+ factory :monkey, {
4
+ :name => "George",
5
+ :unique => "$UNIQ(10)",
6
+ :counter => "$COUNT",
7
+ :number => lambda{ increment! :foo }
8
+ }
10
9
 
11
- factory :pirate, {
12
- :catchphrase => "Ahhrrrr, Matey!",
13
- :monkey => :belongs_to_model,
14
- :created_on => lambda{ 1.day.ago }
15
- }
10
+ factory :pirate, {
11
+ :catchphrase => "Ahhrrrr, Matey!",
12
+ :monkey => :belongs_to_model,
13
+ :created_on => lambda{ 1.day.ago }
14
+ }
16
15
 
17
- factory :ninja_pirate, {
18
- :catchphrase => "(silent)"
19
- }, :class => Pirate, :chain => lambda{ valid_pirate_attributes( :monkey => nil ) }
16
+ factory :ninja_pirate, {
17
+ :catchphrase => "(silent)"
18
+ }, :class => Pirate, :chain => lambda{ valid_pirate_attributes( :monkey => nil ) }
20
19
 
21
- # end
@@ -10,7 +10,7 @@ class FactoryBuilderTest < Test::Unit::TestCase
10
10
  factory :foo, {}
11
11
  end
12
12
  end
13
- assert_equal "uninitialized constant ActiveRecord::Foo", e.message
13
+ assert_equal "uninitialized constant Foo", e.message
14
14
  end
15
15
 
16
16
  def test_factory_with_initializer
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dfl-factories-and-workers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Herald
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2008-10-16 00:00:00 -07:00
14
+ date: 2008-10-22 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies: []
17
17