diff --git a/Rakefile b/Rakefile index e1357c2..adf6d25 100644 --- a/Rakefile +++ b/Rakefile @@ -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, [">= 1.0"]) end Rake::GemPackageTask.new spec do |pkg| diff --git a/factory_girl.gemspec b/factory_girl.gemspec index d8f31c2..8b4c184 100644 --- a/factory_girl.gemspec +++ b/factory_girl.gemspec @@ -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, [">= 1.0"]) else - s.add_dependency(%q, [">= 1.0"]) end else - s.add_dependency(%q, [">= 1.0"]) end end diff --git a/lib/factory_girl/aliases.rb b/lib/factory_girl/aliases.rb index 165a6ff..c9bd9c7 100644 --- a/lib/factory_girl/aliases.rb +++ b/lib/factory_girl/aliases.rb @@ -1,6 +1,8 @@ class Factory - cattr_accessor :aliases #:nodoc: + class << self + attr_accessor :aliases #:nodoc: + end self.aliases = [ [/(.*)_id/, '\1'], [/(.*)/, '\1_id'] diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 29b862f..0749c4f 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -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 diff --git a/lib/factory_girl/sequence.rb b/lib/factory_girl/sequence.rb index eb8689a..cccbe7b 100644 --- a/lib/factory_girl/sequence.rb +++ b/lib/factory_girl/sequence.rb @@ -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. diff --git a/test/factory_test.rb b/test/factory_test.rb index 7f41bf5..8e3bbf7 100644 --- a/test/factory_test.rb +++ b/test/factory_test.rb @@ -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 diff --git a/test/integration_test.rb b/test/integration_test.rb index d264cc7..798a297 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -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