Class: Journeyman::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/journeyman/configuration.rb

Overview

Public: Provides a DSL for configuration of the factories.

Constant Summary collapse

METHOD_OPTIONS =
[:finder, :builder, :processor]
OPTIONS =
[
  :parent, :defaults, :finder_attribute, # Public
  :static_defaults, :dynamic_defaults, :after_create_callback # Internal
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, config) ⇒ Configuration

Public: Receives the name of the factory, and configuration options.

Yields itself to the block passed to the ‘Journeyman.define`.



19
20
21
22
23
# File 'lib/journeyman/configuration.rb', line 19

def initialize(name, options, config)
  @name, @options = name, options
  extract_defaults config.call(self)
  verify_valid_or_exit
end

Instance Attribute Details

#nameObject (readonly)

Internal: Name of the factory, and configuration options.



14
15
16
# File 'lib/journeyman/configuration.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Internal: Name of the factory, and configuration options.



14
15
16
# File 'lib/journeyman/configuration.rb', line 14

def options
  @options
end

Instance Method Details

#after_create(proc = nil, &block) ⇒ Object

Public: Invoked after creating an object, useful for setting up secondary or optional relations.



69
70
71
# File 'lib/journeyman/configuration.rb', line 69

def after_create(proc=nil, &block)
  options[:after_create_callback] ||= proc || block
end

#build(proc = nil, &block) ⇒ Object

Public: Defines how to build an instance. Highly customizable.

build { |attributes|
  blueprint, patient = attributes.delete(:blueprint), attributes.delete(:patient)
  blueprint.enroll(patient)
}

Yields the arguments passed to the ‘build_##name` method after adding the defaults and invoking the processor.



49
50
51
# File 'lib/journeyman/configuration.rb', line 49

def build(proc=nil, &block)
  options[:builder] ||= proc || block
end

#builderObject

Internal: Returns the finder proc, or the default builder strategy.



88
89
90
# File 'lib/journeyman/configuration.rb', line 88

def builder
  options[:builder] ||= parent_builder || default_builder
end

#find(proc = nil, &block) ⇒ Object

Public: Defines how to find an instance.

find { |id|
  User.find_by(name_or_email(id) => id)
}

Yields the find argument passed to the ‘find_##name` method.



36
37
38
# File 'lib/journeyman/configuration.rb', line 36

def find(proc=nil, &block)
  options[:finder] = proc || block
end

#finderObject

Internal: Returns the finder proc, or the default finder strategy.



83
84
85
# File 'lib/journeyman/configuration.rb', line 83

def finder
  options[:finder] ||= default_finder
end

#ignore(*ignored) ⇒ Object

Public: Allows to ignore certain attributes, that can be accessed in the after_create callback.



61
62
63
64
65
# File 'lib/journeyman/configuration.rb', line 61

def ignore(*ignored)
  options[:ignored] = ->(attrs) do
    attrs = attrs.dup; ignored.each { |key| attrs.delete(key) }; attrs
  end
end

#modelObject

Internal: Class of the model to build, used in the default build strategy.



78
79
80
# File 'lib/journeyman/configuration.rb', line 78

def model
  options[:model] ||= infer_model_class(name)
end

#process(proc = nil, &block) ⇒ Object

Public: Attributes processor, allows to modify the passed attributes before building an instance.



55
56
57
# File 'lib/journeyman/configuration.rb', line 55

def process(proc=nil, &block)
  options[:processor] ||= proc || block
end

#processorObject

Internal: Returns a custom processor, or ignore directive.



93
94
95
# File 'lib/journeyman/configuration.rb', line 93

def processor
  options[:ignored] || options[:processor]
end