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

#19: removed ActiveSupport as a dependency

This commit is contained in:
Joe Ferris 2008-12-11 15:54:33 -05:00
parent 3eafc97fc0
commit d550e709e4
7 changed files with 58 additions and 24 deletions

View file

@ -39,7 +39,7 @@ end
spec = Gem::Specification.new do |s|
s.name = %q{factory_girl}
s.version = "1.1.4"
s.version = "1.1.5"
s.summary = %q{factory_girl provides a framework and DSL for defining and
using model instance factories.}
s.description = %q{factory_girl provides a framework and DSL for defining and
@ -58,7 +58,6 @@ spec = Gem::Specification.new do |s|
s.email = %q{jferris@thoughtbot.com}
s.platform = Gem::Platform::RUBY
s.add_dependency(%q<activesupport>, [">= 1.0"])
end
Rake::GemPackageTask.new spec do |pkg|

View file

@ -2,15 +2,15 @@
Gem::Specification.new do |s|
s.name = %q{factory_girl}
s.version = "1.1.4"
s.version = "1.1.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Joe Ferris"]
s.date = %q{2008-11-28}
s.date = %q{2008-12-11}
s.description = %q{factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures.}
s.email = %q{jferris@thoughtbot.com}
s.extra_rdoc_files = ["README.textile"]
s.files = ["Changelog", "LICENSE", "Rakefile", "README.textile", "lib/factory_girl/aliases.rb", "lib/factory_girl/attribute.rb", "lib/factory_girl/attribute_proxy.rb", "lib/factory_girl/factory.rb", "lib/factory_girl/sequence.rb", "lib/factory_girl.rb", "test/aliases_test.rb", "test/attribute_proxy_test.rb", "test/attribute_test.rb", "test/factory_test.rb", "test/integration_test.rb", "test/models.rb", "test/sequence_test.rb", "test/test_helper.rb"]
s.files = ["Changelog", "CONTRIBUTION_GUIDELINES.rdoc", "LICENSE", "Rakefile", "README.textile", "lib/factory_girl/aliases.rb", "lib/factory_girl/attribute.rb", "lib/factory_girl/attribute_proxy.rb", "lib/factory_girl/factory.rb", "lib/factory_girl/sequence.rb", "lib/factory_girl.rb", "test/aliases_test.rb", "test/attribute_proxy_test.rb", "test/attribute_test.rb", "test/factory_test.rb", "test/integration_test.rb", "test/models.rb", "test/sequence_test.rb", "test/test_helper.rb"]
s.has_rdoc = true
s.rdoc_options = ["--line-numbers", "--inline-source", "--main", "README.textile"]
s.require_paths = ["lib"]
@ -23,11 +23,8 @@ Gem::Specification.new do |s|
s.specification_version = 2
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activesupport>, [">= 1.0"])
else
s.add_dependency(%q<activesupport>, [">= 1.0"])
end
else
s.add_dependency(%q<activesupport>, [">= 1.0"])
end
end

View file

@ -1,6 +1,8 @@
class Factory
cattr_accessor :aliases #:nodoc:
class << self
attr_accessor :aliases #:nodoc:
end
self.aliases = [
[/(.*)_id/, '\1'],
[/(.*)/, '\1_id']

View file

@ -1,13 +1,16 @@
class Factory
cattr_accessor :factories #:nodoc:
self.factories = {}
class << self
attr_accessor :factories #:nodoc:
# An Array of strings specifying locations that should be searched for
# factory definitions. By default, factory_girl will attempt to require
# "factories," "test/factories," and "spec/factories." Only the first
# existing file will be loaded.
cattr_accessor :definition_file_paths
# An Array of strings specifying locations that should be searched for
# factory definitions. By default, factory_girl will attempt to require
# "factories," "test/factories," and "spec/factories." Only the first
# existing file will be loaded.
attr_accessor :definition_file_paths
end
self.factories = {}
self.definition_file_paths = %w(factories test/factories spec/factories)
attr_reader :factory_name
@ -36,7 +39,7 @@ class Factory
end
def initialize (name, options = {}) #:nodoc:
options.assert_valid_keys(:class)
assert_valid_options(options)
@factory_name = factory_name_for(name)
@options = options
@attributes = []
@ -111,7 +114,7 @@ class Factory
# default use the "user" factory.
def association (name, options = {})
name = name.to_sym
options = options.symbolize_keys
options = symbolize_keys(options)
association_factory = options[:factory] || name
add_attribute(name) {|a| a.association(association_factory) }
@ -203,7 +206,7 @@ class Factory
private
def build_attributes_hash (values, strategy)
values = values.symbolize_keys
values = symbolize_keys(values)
passed_keys = values.keys.collect {|key| Factory.aliases_for(key) }.flatten
@attributes.each do |attribute|
unless passed_keys.include?(attribute.name)
@ -225,7 +228,7 @@ class Factory
def class_for (class_or_to_s)
if class_or_to_s.respond_to?(:to_sym)
class_or_to_s.to_s.pluralize.classify.constantize
Object.const_get(variable_name_to_class_name(class_or_to_s))
else
class_or_to_s
end
@ -235,7 +238,7 @@ class Factory
if class_or_to_s.respond_to?(:to_sym)
class_or_to_s.to_sym
else
class_or_to_s.to_s.underscore.to_sym
class_name_to_variable_name(class_or_to_s).to_sym
end
end
@ -243,4 +246,35 @@ class Factory
!@attributes.detect {|attr| attr.name == name }.nil?
end
def assert_valid_options(options)
invalid_keys = options.keys - [:class]
unless invalid_keys == []
raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}"
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
end

View file

@ -15,7 +15,9 @@ class Factory
end
cattr_accessor :sequences #:nodoc:
class << self
attr_accessor :sequences #:nodoc:
end
self.sequences = {}
# Defines a new sequence that can be used to generate unique values in a specific format.

View file

@ -337,7 +337,7 @@ class FactoryTest < Test::Unit::TestCase
end
should "raise an ActiveRecord::RecordInvalid error for invalid instances" do
should "raise an error for invalid instances" do
assert_raise(ActiveRecord::RecordInvalid) do
@factory.create(:first_name => nil)
end

View file

@ -28,7 +28,7 @@ class IntegrationTest < Test::Unit::TestCase
end
def teardown
Factory.send(:class_variable_get, "@@factories").clear
Factory.factories.clear
end
context "a generated attributes hash" do