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

Style cleanup

This commit is contained in:
Joshua Clayton 2012-05-05 01:14:21 -04:00
parent 092cde4953
commit 7234f9f07e
17 changed files with 56 additions and 62 deletions

View file

@ -1,16 +1,16 @@
require "set" require 'set'
require "active_support/core_ext/module/delegation" require 'active_support/core_ext/module/delegation'
require "active_support/notifications" require 'active_support/notifications'
require 'factory_girl/errors' require 'factory_girl/errors'
require 'factory_girl/factory_runner' require 'factory_girl/factory_runner'
require 'factory_girl/strategy_syntax_method_registrar' require 'factory_girl/strategy_syntax_method_registrar'
require 'factory_girl/strategy_calculator' require 'factory_girl/strategy_calculator'
require "factory_girl/strategy/build" require 'factory_girl/strategy/build'
require "factory_girl/strategy/create" require 'factory_girl/strategy/create'
require "factory_girl/strategy/attributes_for" require 'factory_girl/strategy/attributes_for'
require "factory_girl/strategy/stub" require 'factory_girl/strategy/stub'
require "factory_girl/strategy/null" require 'factory_girl/strategy/null'
require 'factory_girl/disallows_duplicates_registry' require 'factory_girl/disallows_duplicates_registry'
require 'factory_girl/registry' require 'factory_girl/registry'
require 'factory_girl/null_factory' require 'factory_girl/null_factory'
@ -39,7 +39,7 @@ require 'factory_girl/version'
module FactoryGirl module FactoryGirl
def self.factories def self.factories
@factories ||= DisallowsDuplicatesRegistry.new(Registry.new("Factory")) @factories ||= DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
end end
def self.register_factory(factory) def self.register_factory(factory)
@ -54,7 +54,7 @@ module FactoryGirl
end end
def self.sequences def self.sequences
@sequences ||= DisallowsDuplicatesRegistry.new(Registry.new("Sequence")) @sequences ||= DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
end end
def self.register_sequence(sequence) def self.register_sequence(sequence)
@ -69,7 +69,7 @@ module FactoryGirl
end end
def self.traits def self.traits
@traits ||= DisallowsDuplicatesRegistry.new(Registry.new("Trait")) @traits ||= DisallowsDuplicatesRegistry.new(Registry.new('Trait'))
end end
def self.register_trait(trait) def self.register_trait(trait)
@ -84,7 +84,7 @@ module FactoryGirl
end end
def self.strategies def self.strategies
@strategies ||= Registry.new("Strategy") @strategies ||= Registry.new('Strategy')
end end
def self.register_strategy(strategy_name, strategy_class) def self.register_strategy(strategy_name, strategy_class)

View file

@ -1,7 +1,7 @@
require "factory_girl/attribute/static" require 'factory_girl/attribute/static'
require "factory_girl/attribute/dynamic" require 'factory_girl/attribute/dynamic'
require "factory_girl/attribute/association" require 'factory_girl/attribute/association'
require "factory_girl/attribute/sequence" require 'factory_girl/attribute/sequence'
module FactoryGirl module FactoryGirl
class Attribute #:nodoc: class Attribute #:nodoc:

View file

@ -1,6 +1,5 @@
module FactoryGirl module FactoryGirl
class Attribute class Attribute
class Sequence < Attribute class Sequence < Attribute
def initialize(name, sequence, ignored) def initialize(name, sequence, ignored)
super(name, ignored) super(name, ignored)
@ -12,6 +11,5 @@ module FactoryGirl
-> { FactoryGirl.generate(sequence) } -> { FactoryGirl.generate(sequence) }
end end
end end
end end
end end

View file

@ -1,7 +1,7 @@
require "factory_girl/declaration/static" require 'factory_girl/declaration/static'
require "factory_girl/declaration/dynamic" require 'factory_girl/declaration/dynamic'
require "factory_girl/declaration/association" require 'factory_girl/declaration/association'
require "factory_girl/declaration/implicit" require 'factory_girl/declaration/implicit'
module FactoryGirl module FactoryGirl
class Declaration class Declaration

View file

@ -33,7 +33,7 @@ module FactoryGirl
# * value: +Object+ # * value: +Object+
# If no block is given, this value will be used for this attribute. # If no block is given, this value will be used for this attribute.
def add_attribute(name, value = nil, &block) def add_attribute(name, value = nil, &block)
raise AttributeDefinitionError, "Both value and block given" if value && block_given? raise AttributeDefinitionError, 'Both value and block given' if value && block_given?
declaration = if block_given? declaration = if block_given?
Declaration::Dynamic.new(name, @ignore, block) Declaration::Dynamic.new(name, @ignore, block)
@ -86,7 +86,7 @@ module FactoryGirl
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory) elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
association(name, *args) association(name, *args)
elsif FactoryGirl.callback_names.include?(name) elsif FactoryGirl.callback_names.include?(name)
callback_when, callback_name = name.to_s.split("_", 2) callback_when, callback_name = name.to_s.split('_', 2)
ActiveSupport::Deprecation.warn "Calling #{name} is deprecated; use the syntax #{callback_when}(:#{callback_name}) {}", caller ActiveSupport::Deprecation.warn "Calling #{name} is deprecated; use the syntax #{callback_when}(:#{callback_name}) {}", caller
@definition.add_callback(Callback.new(name, block)) @definition.add_callback(Callback.new(name, block))
else else

View file

@ -1,4 +1,4 @@
require "observer" require 'observer'
module FactoryGirl module FactoryGirl
class Evaluation class Evaluation
@ -9,12 +9,12 @@ module FactoryGirl
@to_create = to_create @to_create = to_create
end end
delegate :object, :hash, to: :@attribute_assigner
def create(result_instance) def create(result_instance)
@to_create[result_instance] @to_create[result_instance]
end end
delegate :object, :hash, to: :@attribute_assigner
def notify(name, result_instance) def notify(name, result_instance)
changed changed
notify_observers(name, result_instance) notify_observers(name, result_instance)

View file

@ -1,5 +1,5 @@
require "active_support/core_ext/hash/keys" require 'active_support/core_ext/hash/keys'
require "active_support/inflector" require 'active_support/inflector'
module FactoryGirl module FactoryGirl
class Factory class Factory

View file

@ -19,7 +19,7 @@ module FactoryGirl
instrumentation_payload = { name: @name, strategy: runner_strategy } instrumentation_payload = { name: @name, strategy: runner_strategy }
ActiveSupport::Notifications.instrument("factory_girl.run_factory", instrumentation_payload) do ActiveSupport::Notifications.instrument('factory_girl.run_factory', instrumentation_payload) do
factory.run(runner_strategy, @overrides, &block) factory.run(runner_strategy, @overrides, &block)
end end
end end

View file

@ -11,7 +11,7 @@ module FactoryGirl
options = args.extract_options! options = args.extract_options!
@value = args.first || 1 @value = args.first || 1
@aliases = options[:aliases] || [] @aliases = options.fetch(:aliases) { [] }
end end
def next def next

View file

@ -36,23 +36,23 @@ module FactoryGirl
end end
def save(*args) def save(*args)
raise "stubbed models are not allowed to access the database" raise 'stubbed models are not allowed to access the database'
end end
def destroy(*args) def destroy(*args)
raise "stubbed models are not allowed to access the database" raise 'stubbed models are not allowed to access the database'
end end
def connection def connection
raise "stubbed models are not allowed to access the database" raise 'stubbed models are not allowed to access the database'
end end
def reload def reload
raise "stubbed models are not allowed to access the database" raise 'stubbed models are not allowed to access the database'
end end
def update_attribute(*args) def update_attribute(*args)
raise "stubbed models are not allowed to access the database" raise 'stubbed models are not allowed to access the database'
end end
end end
end end

View file

@ -1,7 +1,8 @@
require "active_support/deprecation" require 'active_support/deprecation'
require "factory_girl/syntax/methods"
require "factory_girl/syntax/default" require 'factory_girl/syntax/methods'
require "factory_girl/syntax/vintage" require 'factory_girl/syntax/default'
require 'factory_girl/syntax/vintage'
module FactoryGirl module FactoryGirl
# Provides alternate syntaxes for factory_girl. If you don't like the default # Provides alternate syntaxes for factory_girl. If you don't like the default

View file

@ -18,23 +18,19 @@ module FactoryGirl
# This syntax was derived from Pete Yandell's machinist. # This syntax was derived from Pete Yandell's machinist.
module Blueprint module Blueprint
module ActiveRecord #:nodoc: module ActiveRecord #:nodoc:
def self.included(base) # :nodoc: def self.included(base) # :nodoc:
base.extend ClassMethods base.extend ClassMethods
end end
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
def blueprint(&block) def blueprint(&block)
ActiveSupport::Deprecation.warn "Model.blueprint is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.blueprint is deprecated; use the FactoryGirl.define syntax instead', caller
instance = Factory.new(name.underscore, class: self) instance = Factory.new(name.underscore, class: self)
proxy = FactoryGirl::DefinitionProxy.new(instance) proxy = FactoryGirl::DefinitionProxy.new(instance)
proxy.instance_eval(&block) proxy.instance_eval(&block)
FactoryGirl.register_factory(instance) FactoryGirl.register_factory(instance)
end end
end end
end end
end end
end end

View file

@ -34,14 +34,13 @@ module FactoryGirl
# object_daddy. # object_daddy.
module Generate module Generate
module ActiveRecord #:nodoc: module ActiveRecord #:nodoc:
def self.included(base) # :nodoc: def self.included(base) # :nodoc:
base.extend ClassMethods base.extend ClassMethods
end end
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
def generate(overrides = {}, &block) def generate(overrides = {}, &block)
ActiveSupport::Deprecation.warn "Model.generate is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.generate is deprecated; use the FactoryGirl.define syntax instead', caller
instance = FactoryRunner.new(name.underscore, :build, [overrides]).run instance = FactoryRunner.new(name.underscore, :build, [overrides]).run
instance.save instance.save
yield(instance) if block_given? yield(instance) if block_given?
@ -49,14 +48,14 @@ module FactoryGirl
end end
def generate!(overrides = {}, &block) def generate!(overrides = {}, &block)
ActiveSupport::Deprecation.warn "Model.generate! is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.generate! is deprecated; use the FactoryGirl.define syntax instead', caller
instance = FactoryRunner.new(name.underscore, :create, [overrides]).run instance = FactoryRunner.new(name.underscore, :create, [overrides]).run
yield(instance) if block_given? yield(instance) if block_given?
instance instance
end end
def spawn(overrides = {}, &block) def spawn(overrides = {}, &block)
ActiveSupport::Deprecation.warn "Model.spawn is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.spawn is deprecated; use the FactoryGirl.define syntax instead', caller
instance = FactoryRunner.new(name.underscore, :build, [overrides]).run instance = FactoryRunner.new(name.underscore, :build, [overrides]).run
yield(instance) if block_given? yield(instance) if block_given?
instance instance

View file

@ -26,12 +26,12 @@ module FactoryGirl
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
def make(overrides = {}) def make(overrides = {})
ActiveSupport::Deprecation.warn "Model.make is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.make is deprecated; use the FactoryGirl.define syntax instead', caller
FactoryRunner.new(name.underscore, :build, [overrides]).run FactoryRunner.new(name.underscore, :build, [overrides]).run
end end
def make!(overrides = {}) def make!(overrides = {})
ActiveSupport::Deprecation.warn "Model.make! is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Model.make! is deprecated; use the FactoryGirl.define syntax instead', caller
FactoryRunner.new(name.underscore, :create, [overrides]).run FactoryRunner.new(name.underscore, :create, [overrides]).run
end end
end end

View file

@ -26,7 +26,7 @@ module FactoryGirl
module Sham #:nodoc: module Sham #:nodoc:
def self.method_missing(name, *args, &block) def self.method_missing(name, *args, &block)
if block_given? if block_given?
ActiveSupport::Deprecation.warn "Sham.sequence is deprecated; use the FactoryGirl.define syntax instead", caller ActiveSupport::Deprecation.warn 'Sham.sequence is deprecated; use the FactoryGirl.define syntax instead', caller
start_value = args.first start_value = args.first
FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block)) FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block))
else else

View file

@ -21,7 +21,7 @@ module FactoryGirl
# Yields: +Factory+ # Yields: +Factory+
# The newly created factory. # The newly created factory.
def self.define(name, options = {}) def self.define(name, options = {})
ActiveSupport::Deprecation.warn "Factory.define is deprecated; use the FactoryGirl.define block syntax to declare your factory.", caller ActiveSupport::Deprecation.warn 'Factory.define is deprecated; use the FactoryGirl.define block syntax to declare your factory.', caller
factory = FactoryGirl::Factory.new(name, options) factory = FactoryGirl::Factory.new(name, options)
proxy = FactoryGirl::DefinitionProxy.new(factory) proxy = FactoryGirl::DefinitionProxy.new(factory)
yield(proxy) yield(proxy)
@ -44,7 +44,7 @@ module FactoryGirl
# #
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" } # Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
def self.sequence(name, start_value = 1, &block) def self.sequence(name, start_value = 1, &block)
ActiveSupport::Deprecation.warn "Factory.sequence is deprecated; use the FactoryGirl.define block syntax to declare your sequence.", caller ActiveSupport::Deprecation.warn 'Factory.sequence is deprecated; use the FactoryGirl.define block syntax to declare your sequence.', caller
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block)) FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
end end
@ -57,7 +57,7 @@ module FactoryGirl
# Returns: # Returns:
# The next value in the sequence. (Object) # The next value in the sequence. (Object)
def self.next(name) def self.next(name)
ActiveSupport::Deprecation.warn "Factory.next is deprecated; use FactoryGirl.generate instead.", caller ActiveSupport::Deprecation.warn 'Factory.next is deprecated; use FactoryGirl.generate instead.', caller
FactoryGirl.generate(name) FactoryGirl.generate(name)
end end
@ -86,31 +86,31 @@ module FactoryGirl
# # will be used instead. # # will be used instead.
# Factory(:post, user_id: 1) # Factory(:post, user_id: 1)
def self.alias(pattern, replace) def self.alias(pattern, replace)
ActiveSupport::Deprecation.warn "Factory.alias is deprecated; use FactoryGirl.aliases << [pattern, replace] instead.", caller ActiveSupport::Deprecation.warn 'Factory.alias is deprecated; use FactoryGirl.aliases << [pattern, replace] instead.', caller
FactoryGirl.aliases << [pattern, replace] FactoryGirl.aliases << [pattern, replace]
end end
# Alias for FactoryGirl.attributes_for # Alias for FactoryGirl.attributes_for
def self.attributes_for(name, overrides = {}) def self.attributes_for(name, overrides = {})
ActiveSupport::Deprecation.warn "Factory.attributes_for is deprecated; use FactoryGirl.attributes_for instead.", caller ActiveSupport::Deprecation.warn 'Factory.attributes_for is deprecated; use FactoryGirl.attributes_for instead.', caller
FactoryGirl.attributes_for(name, overrides) FactoryGirl.attributes_for(name, overrides)
end end
# Alias for FactoryGirl.build # Alias for FactoryGirl.build
def self.build(name, overrides = {}) def self.build(name, overrides = {})
ActiveSupport::Deprecation.warn "Factory.build is deprecated; use FactoryGirl.build instead.", caller ActiveSupport::Deprecation.warn 'Factory.build is deprecated; use FactoryGirl.build instead.', caller
FactoryGirl.build(name, overrides) FactoryGirl.build(name, overrides)
end end
# Alias for FactoryGirl.create # Alias for FactoryGirl.create
def self.create(name, overrides = {}) def self.create(name, overrides = {})
ActiveSupport::Deprecation.warn "Factory.create is deprecated; use FactoryGirl.create instead.", caller ActiveSupport::Deprecation.warn 'Factory.create is deprecated; use FactoryGirl.create instead.', caller
FactoryGirl.create(name, overrides) FactoryGirl.create(name, overrides)
end end
# Alias for FactoryGirl.build_stubbed. # Alias for FactoryGirl.build_stubbed.
def self.stub(name, overrides = {}) def self.stub(name, overrides = {})
ActiveSupport::Deprecation.warn "Factory.stub is deprecated; use FactoryGirl.build_stubbed instead.", caller ActiveSupport::Deprecation.warn 'Factory.stub is deprecated; use FactoryGirl.build_stubbed instead.', caller
FactoryGirl.build_stubbed(name, overrides) FactoryGirl.build_stubbed(name, overrides)
end end
end end
@ -120,7 +120,7 @@ module FactoryGirl
# Example: # Example:
# Factory(:user, name: 'Joe') # Factory(:user, name: 'Joe')
def Factory(name, attrs = {}) def Factory(name, attrs = {})
ActiveSupport::Deprecation.warn "Factory(:name) is deprecated; use FactoryGirl.create(:name) instead.", caller ActiveSupport::Deprecation.warn 'Factory(:name) is deprecated; use FactoryGirl.create(:name) instead.', caller
FactoryGirl.create(name, attrs) FactoryGirl.create(name, attrs)
end end
end end

View file

@ -1,3 +1,3 @@
module FactoryGirl module FactoryGirl
VERSION = "3.2.0" VERSION = '3.2.0'
end end