Module: FactoryGirl

Extended by:
Syntax::Default
Defined in:
lib/factory_girl.rb,
lib/factory_girl/trait.rb,
lib/factory_girl/errors.rb,
lib/factory_girl/linter.rb,
lib/factory_girl/reload.rb,
lib/factory_girl/syntax.rb,
lib/factory_girl/aliases.rb,
lib/factory_girl/factory.rb,
lib/factory_girl/version.rb,
lib/factory_girl/callback.rb,
lib/factory_girl/registry.rb,
lib/factory_girl/sequence.rb,
lib/factory_girl/attribute.rb,
lib/factory_girl/decorator.rb,
lib/factory_girl/evaluator.rb,
lib/factory_girl/definition.rb,
lib/factory_girl/evaluation.rb,
lib/factory_girl/declaration.rb,
lib/factory_girl/null_object.rb,
lib/factory_girl/null_factory.rb,
lib/factory_girl/configuration.rb,
lib/factory_girl/strategy/null.rb,
lib/factory_girl/strategy/stub.rb,
lib/factory_girl/syntax_runner.rb,
lib/factory_girl/attribute_list.rb,
lib/factory_girl/factory_runner.rb,
lib/factory_girl/strategy/build.rb,
lib/factory_girl/syntax/default.rb,
lib/factory_girl/syntax/methods.rb,
lib/factory_girl/strategy/create.rb,
lib/factory_girl/attribute/static.rb,
lib/factory_girl/declaration_list.rb,
lib/factory_girl/definition_proxy.rb,
lib/factory_girl/find_definitions.rb,
lib/factory_girl/attribute/dynamic.rb,
lib/factory_girl/attribute/sequence.rb,
lib/factory_girl/attribute_assigner.rb,
lib/factory_girl/callbacks_observer.rb,
lib/factory_girl/declaration/static.rb,
lib/factory_girl/declaration/dynamic.rb,
lib/factory_girl/strategy_calculator.rb,
lib/factory_girl/declaration/implicit.rb,
lib/factory_girl/definition_hierarchy.rb,
lib/factory_girl/attribute/association.rb,
lib/factory_girl/declaration/association.rb,
lib/factory_girl/evaluator_class_definer.rb,
lib/factory_girl/strategy/attributes_for.rb,
lib/factory_girl/decorator/attribute_hash.rb,
lib/factory_girl/decorator/class_key_hash.rb,
lib/factory_girl/decorator/new_constructor.rb,
lib/factory_girl/decorator/invocation_tracker.rb,
lib/factory_girl/strategy_syntax_method_registrar.rb,
lib/factory_girl/decorator/disallows_duplicates_registry.rb

Defined Under Namespace

Modules: Strategy, Syntax Classes: AssociationDefinitionError, Attribute, AttributeAssigner, AttributeDefinitionError, AttributeList, Callback, CallbacksObserver, Configuration, Declaration, DeclarationList, Decorator, Definition, DefinitionHierarchy, DefinitionProxy, DuplicateDefinitionError, Evaluation, Evaluator, EvaluatorClassDefiner, Factory, FactoryRunner, InvalidCallbackNameError, InvalidFactoryError, Linter, MethodDefinitionError, NullFactory, NullObject, Registry, Sequence, SequenceAbuseError, StrategyCalculator, StrategySyntaxMethodRegistrar, SyntaxRunner, Trait

Constant Summary collapse

VERSION =
'4.9.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Syntax::Default

define, modify

Methods included from Syntax::Methods

#attributes_for, #attributes_for_list, #build, #build_list, #build_stubbed, #build_stubbed_list, #create, #create_list, #generate, #generate_list

Class Attribute Details

.aliasesObject

Returns the value of attribute aliases.



3
4
5
# File 'lib/factory_girl/aliases.rb', line 3

def aliases
  @aliases
end

.definition_file_pathsObject

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.



7
8
9
# File 'lib/factory_girl/find_definitions.rb', line 7

def definition_file_paths
  @definition_file_paths
end

Class Method Details

.aliases_for(attribute) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/factory_girl/aliases.rb', line 11

def self.aliases_for(attribute)
  aliases.map do |(pattern, replace)|
    if pattern.match(attribute.to_s)
      attribute.to_s.sub(pattern, replace).to_sym
    end
  end.compact << attribute
end

.configurationObject



50
51
52
# File 'lib/factory_girl.rb', line 50

def self.configuration
  @configuration ||= Configuration.new
end

.factory_by_name(name) ⇒ Object



99
100
101
# File 'lib/factory_girl.rb', line 99

def self.factory_by_name(name)
  factories.find(name)
end

.find_definitionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/factory_girl/find_definitions.rb', line 12

def self.find_definitions
  absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }

  absolute_definition_file_paths.uniq.each do |path|
    load("#{path}.rb") if File.exist?("#{path}.rb")

    if File.directory? path
      Dir[File.join(path, '**', '*.rb')].sort.each do |file|
        load file
      end
    end
  end
end

.lint(*args) ⇒ Object

Look for errors in factories and (optionally) their traits. Parameters: factories - which factories to lint; omit for all factories options:

traits: true - to lint traits as well as factories
strategy: :create - to specify the strategy for linting


64
65
66
67
68
69
70
# File 'lib/factory_girl.rb', line 64

def self.lint(*args)
  options = args.extract_options!
  factories_to_lint = args[0] || FactoryGirl.factories
  linting_strategy = options[:traits] ? :factory_and_traits : :factory
  factory_strategy = options[:strategy] || :create
  Linter.new(factories_to_lint, linting_strategy, factory_strategy).lint!
end

.register_callback(name) ⇒ Object



149
150
151
152
# File 'lib/factory_girl.rb', line 149

def self.register_callback(name)
  name = name.to_sym
  callback_names << name
end

.register_default_callbacksObject



142
143
144
145
146
147
# File 'lib/factory_girl.rb', line 142

def self.register_default_callbacks
  register_callback(:after_build)
  register_callback(:after_create)
  register_callback(:after_stub)
  register_callback(:before_create)
end

.register_default_strategiesObject



134
135
136
137
138
139
140
# File 'lib/factory_girl.rb', line 134

def self.register_default_strategies
  register_strategy(:build,          FactoryGirl::Strategy::Build)
  register_strategy(:create,         FactoryGirl::Strategy::Create)
  register_strategy(:attributes_for, FactoryGirl::Strategy::AttributesFor)
  register_strategy(:build_stubbed,  FactoryGirl::Strategy::Stub)
  register_strategy(:null,           FactoryGirl::Strategy::Null)
end

.register_factory(factory) ⇒ Object



92
93
94
95
96
97
# File 'lib/factory_girl.rb', line 92

def self.register_factory(factory)
  factory.names.each do |name|
    factories.register(name, factory)
  end
  factory
end

.register_sequence(sequence) ⇒ Object



103
104
105
106
107
108
# File 'lib/factory_girl.rb', line 103

def self.register_sequence(sequence)
  sequence.names.each do |name|
    sequences.register(name, sequence)
  end
  sequence
end

.register_strategy(strategy_name, strategy_class) ⇒ Object



125
126
127
128
# File 'lib/factory_girl.rb', line 125

def self.register_strategy(strategy_name, strategy_class)
  strategies.register(strategy_name, strategy_class)
  StrategySyntaxMethodRegistrar.new(strategy_name).define_strategy_methods
end

.register_trait(trait) ⇒ Object



114
115
116
117
118
119
# File 'lib/factory_girl.rb', line 114

def self.register_trait(trait)
  trait.names.each do |name|
    traits.register(name, trait)
  end
  trait
end

.reloadObject



2
3
4
5
6
7
# File 'lib/factory_girl/reload.rb', line 2

def self.reload
  reset_configuration
  register_default_strategies
  register_default_callbacks
  find_definitions
end

.reset_configurationObject



54
55
56
# File 'lib/factory_girl.rb', line 54

def self.reset_configuration
  @configuration = nil
end

.sequence_by_name(name) ⇒ Object



110
111
112
# File 'lib/factory_girl.rb', line 110

def self.sequence_by_name(name)
  sequences.find(name)
end

.strategy_by_name(name) ⇒ Object



130
131
132
# File 'lib/factory_girl.rb', line 130

def self.strategy_by_name(name)
  strategies.find(name)
end

.trait_by_name(name) ⇒ Object



121
122
123
# File 'lib/factory_girl.rb', line 121

def self.trait_by_name(name)
  traits.find(name)
end