Skip to content

Commit

Permalink
thoughtbot#19: removed ActiveSupport as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Dec 11, 2008
1 parent 3eafc97 commit d550e70
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 24 deletions.
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand Down
9 changes: 3 additions & 6 deletions factory_girl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
4 changes: 3 additions & 1 deletion lib/factory_girl/aliases.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Factory

cattr_accessor :aliases #:nodoc:
class << self
attr_accessor :aliases #:nodoc:
end
self.aliases = [
[/(.*)_id/, '\1'],
[/(.*)/, '\1_id']
Expand Down
58 changes: 46 additions & 12 deletions lib/factory_girl/factory.rb
Original file line number Diff line number Diff line change
@@ -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.
attr_accessor :definition_file_paths
end

# 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
self.factories = {}
self.definition_file_paths = %w(factories test/factories spec/factories)

attr_reader :factory_name
Expand Down Expand Up @@ -36,7 +39,7 @@ def build_class #:nodoc:
end

def initialize (name, options = {}) #:nodoc:
options.assert_valid_keys(:class)
assert_valid_options(options)
@factory_name = factory_name_for(name)
@options = options
@attributes = []
Expand Down Expand Up @@ -111,7 +114,7 @@ def method_missing (name, *args, &block)
# 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) }
Expand Down Expand Up @@ -203,7 +206,7 @@ def factory_by_name (name)
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)
Expand All @@ -225,7 +228,7 @@ def build_instance (override, strategy)

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
Expand All @@ -235,12 +238,43 @@ 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_or_to_s.to_s.underscore.to_sym
class_name_to_variable_name(class_or_to_s).to_sym
end
end

def attribute_defined? (name)
!@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
4 changes: 3 additions & 1 deletion lib/factory_girl/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def next

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.
Expand Down
2 changes: 1 addition & 1 deletion test/factory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def self.should_instantiate_class

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
Expand Down
2 changes: 1 addition & 1 deletion test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup
end

def teardown
Factory.send(:class_variable_get, "@@factories").clear
Factory.factories.clear
end

context "a generated attributes hash" do
Expand Down

0 comments on commit d550e70

Please sign in to comment.