dfl-factories-and-workers 0.1.2 → 0.1.3
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.
- data/CHANGELOG +10 -4
- data/README.rdoc +0 -4
- data/lib/factories-and-workers/factory_builder.rb +11 -23
- data/lib/factories-and-workers/factory_worker.rb +6 -6
- data/rails/init.rb +5 -5
- data/test/factories.rb +15 -17
- data/test/factory_builder_test.rb +1 -1
- metadata +2 -2
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 =
|
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
|
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
|
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
|
91
|
-
Factory
|
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
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
2
|
-
include FactoriesAndWorkers::Factory
|
1
|
+
include FactoriesAndWorkers::Factory
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
factory :monkey, {
|
4
|
+
:name => "George",
|
5
|
+
:unique => "$UNIQ(10)",
|
6
|
+
:counter => "$COUNT",
|
7
|
+
:number => lambda{ increment! :foo }
|
8
|
+
}
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
factory :pirate, {
|
11
|
+
:catchphrase => "Ahhrrrr, Matey!",
|
12
|
+
:monkey => :belongs_to_model,
|
13
|
+
:created_on => lambda{ 1.day.ago }
|
14
|
+
}
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
factory :ninja_pirate, {
|
17
|
+
:catchphrase => "(silent)"
|
18
|
+
}, :class => Pirate, :chain => lambda{ valid_pirate_attributes( :monkey => nil ) }
|
20
19
|
|
21
|
-
# end
|
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.
|
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-
|
14
|
+
date: 2008-10-22 00:00:00 -07:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|