1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Use activesupport to remove code duplicating its behavior

Closes #206
This commit is contained in:
Joshua Clayton 2011-09-23 15:00:00 -04:00
parent 58e75bcb3f
commit 450dbb2c5c
7 changed files with 25 additions and 60 deletions

View file

@ -2,6 +2,7 @@ PATH
remote: . remote: .
specs: specs:
factory_girl (2.1.1) factory_girl (2.1.1)
activesupport
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
@ -28,31 +29,31 @@ GEM
cucumber (>= 1.0.2) cucumber (>= 1.0.2)
rdiscount (>= 1.6.8) rdiscount (>= 1.6.8)
rspec (>= 2.6.0) rspec (>= 2.6.0)
bcat (0.6.1) bcat (0.6.2)
rack (~> 1.0) rack (~> 1.0)
bcrypt-ruby (3.0.0) bcrypt-ruby (3.0.1)
bluecloth (2.1.0) bluecloth (2.1.0)
bourne (1.0) bourne (1.0)
mocha (= 0.9.8) mocha (= 0.9.8)
builder (3.0.0) builder (3.0.0)
childprocess (0.2.2) childprocess (0.2.2)
ffi (~> 1.0.6) ffi (~> 1.0.6)
cucumber (1.0.2) cucumber (1.0.6)
builder (>= 2.1.2) builder (>= 2.1.2)
diff-lcs (>= 1.1.2) diff-lcs (>= 1.1.2)
gherkin (~> 2.4.5) gherkin (~> 2.4.18)
json (>= 1.4.6) json (>= 1.4.6)
term-ansicolor (>= 1.0.5) term-ansicolor (>= 1.0.6)
diff-lcs (1.1.3) diff-lcs (1.1.3)
ffi (1.0.9) ffi (1.0.9)
gherkin (2.4.16) gherkin (2.4.21)
json (>= 1.4.6) json (>= 1.4.6)
i18n (0.6.0) i18n (0.6.0)
json (1.5.4) json (1.6.1)
mocha (0.9.8) mocha (0.9.8)
rake rake
multi_json (1.0.3) multi_json (1.0.3)
rack (1.3.2) rack (1.3.3)
rake (0.9.2) rake (0.9.2)
rcov (0.9.10) rcov (0.9.10)
rdiscount (1.6.8) rdiscount (1.6.8)

View file

@ -20,6 +20,8 @@ Gem::Specification.new do |s|
s.email = %q{jferris@thoughtbot.com} s.email = %q{jferris@thoughtbot.com}
s.homepage = "https://github.com/thoughtbot/factory_girl" s.homepage = "https://github.com/thoughtbot/factory_girl"
s.add_dependency("activesupport")
s.add_development_dependency("rspec", "~> 2.0") s.add_development_dependency("rspec", "~> 2.0")
s.add_development_dependency("cucumber", "~> 1.0.0") s.add_development_dependency("cucumber", "~> 1.0.0")
s.add_development_dependency("timecop") s.add_development_dependency("timecop")

View file

@ -2,6 +2,7 @@ PATH
remote: /Users/joshuaclayton/dev/gems/factory_girl remote: /Users/joshuaclayton/dev/gems/factory_girl
specs: specs:
factory_girl (2.1.1) factory_girl (2.1.1)
activesupport
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/

View file

@ -2,6 +2,7 @@ PATH
remote: /Users/joshuaclayton/dev/gems/factory_girl remote: /Users/joshuaclayton/dev/gems/factory_girl
specs: specs:
factory_girl (2.1.1) factory_girl (2.1.1)
activesupport
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/

View file

@ -2,6 +2,7 @@ PATH
remote: /Users/joshuaclayton/dev/gems/factory_girl remote: /Users/joshuaclayton/dev/gems/factory_girl
specs: specs:
factory_girl (2.1.1) factory_girl (2.1.1)
activesupport
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/

View file

@ -2,6 +2,7 @@ PATH
remote: /Users/joshuaclayton/dev/gems/factory_girl remote: /Users/joshuaclayton/dev/gems/factory_girl
specs: specs:
factory_girl (2.1.1) factory_girl (2.1.1)
activesupport
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/

View file

@ -1,3 +1,6 @@
require "active_support/core_ext/hash/keys"
require "active_support/inflector"
module FactoryGirl module FactoryGirl
# Raised when a factory is defined that attempts to instantiate itself. # Raised when a factory is defined that attempts to instantiate itself.
class AssociationDefinitionError < RuntimeError class AssociationDefinitionError < RuntimeError
@ -24,7 +27,7 @@ module FactoryGirl
end end
def build_class #:nodoc: def build_class #:nodoc:
@build_class ||= class_for(class_name) @build_class ||= class_name.to_s.camelize.constantize
end end
def default_strategy #:nodoc: def default_strategy #:nodoc:
@ -33,7 +36,7 @@ module FactoryGirl
def initialize(name, options = {}) #:nodoc: def initialize(name, options = {}) #:nodoc:
assert_valid_options(options) assert_valid_options(options)
@name = factory_name_for(name) @name = name.to_s.underscore.to_sym
@parent = options[:parent] @parent = options[:parent]
@parent_factory = nil @parent_factory = nil
@options = options @options = options
@ -94,7 +97,7 @@ module FactoryGirl
def run(proxy_class, overrides) #:nodoc: def run(proxy_class, overrides) #:nodoc:
proxy = proxy_class.new(build_class) proxy = proxy_class.new(build_class)
callbacks.each { |callback| proxy.add_callback(callback) } callbacks.each { |callback| proxy.add_callback(callback) }
overrides = symbolize_keys(overrides) overrides = overrides.symbolize_keys
attributes.each do |attribute| attributes.each do |attribute|
factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) } factory_overrides = overrides.select { |attr, val| attribute.aliases_for?(attr) }
@ -109,7 +112,7 @@ module FactoryGirl
end end
def human_names def human_names
names.map {|name| name.to_s.gsub('_', ' ') } names.map {|name| name.to_s.humanize.downcase }
end end
def associations def associations
@ -195,30 +198,9 @@ module FactoryGirl
update_children if allow_overrides? update_children if allow_overrides?
end end
def class_for (class_or_to_s)
if class_or_to_s.respond_to?(:to_sym)
class_name = variable_name_to_class_name(class_or_to_s)
class_name.split('::').inject(Object) do |object, string|
object.const_get(string)
end
else
class_or_to_s
end
end
def factory_name_for(class_or_to_s)
if class_or_to_s.respond_to?(:to_sym)
class_or_to_s.to_sym
else
class_name_to_variable_name(class_or_to_s).to_sym
end
end
def assert_valid_options(options) def assert_valid_options(options)
invalid_keys = options.keys - [:class, :parent, :default_strategy, :aliases, :traits] options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)
unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
end
if options[:default_strategy] if options[:default_strategy]
assert_valid_strategy(options[:default_strategy]) assert_valid_strategy(options[:default_strategy])
puts "WARNING: default_strategy is deprecated." puts "WARNING: default_strategy is deprecated."
@ -227,35 +209,11 @@ module FactoryGirl
end end
def assert_valid_strategy(strategy) def assert_valid_strategy(strategy)
unless Proxy.const_defined? variable_name_to_class_name(strategy) unless Proxy.const_defined? strategy.to_s.camelize
raise ArgumentError, "Unknown strategy: #{strategy}" raise ArgumentError, "Unknown strategy: #{strategy}"
end end
end end
# Based on ActiveSupport's underscore inflector
def class_name_to_variable_name(name)
name.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
# Based on ActiveSupport's camelize inflector
def variable_name_to_class_name(name)
name.to_s.
gsub(/\/(.?)/) { "::#{$1.upcase}" }.
gsub(/(?:^|_)(.)/) { $1.upcase }
end
# From ActiveSupport
def symbolize_keys(hash)
hash.inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
options
end
end
def trait_for(name) def trait_for(name)
@defined_traits.detect {|trait| trait.name == name } @defined_traits.detect {|trait| trait.name == name }
end end