Class: Journeyman::Configuration
- Inherits:
-
Object
- Object
- Journeyman::Configuration
- 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
-
#name ⇒ Object
readonly
Internal: Name of the factory, and configuration options.
-
#options ⇒ Object
readonly
Internal: Name of the factory, and configuration options.
Instance Method Summary collapse
-
#after_create(proc = nil, &block) ⇒ Object
Public: Invoked after creating an object, useful for setting up secondary or optional relations.
-
#build(proc = nil, &block) ⇒ Object
Public: Defines how to build an instance.
-
#builder ⇒ Object
Internal: Returns the finder proc, or the default builder strategy.
-
#find(proc = nil, &block) ⇒ Object
Public: Defines how to find an instance.
-
#finder ⇒ Object
Internal: Returns the finder proc, or the default finder strategy.
-
#ignore(*ignored) ⇒ Object
Public: Allows to ignore certain attributes, that can be accessed in the after_create callback.
-
#initialize(name, options, config) ⇒ Configuration
constructor
Public: Receives the name of the factory, and configuration options.
-
#model ⇒ Object
Internal: Class of the model to build, used in the default build strategy.
-
#process(proc = nil, &block) ⇒ Object
Public: Attributes processor, allows to modify the passed attributes before building an instance.
-
#processor ⇒ Object
Internal: Returns a custom processor, or ignore directive.
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, , config) @name, = name, extract_defaults config.call(self) verify_valid_or_exit end |
Instance Attribute Details
#name ⇒ Object (readonly)
Internal: Name of the factory, and configuration options.
14 15 16 |
# File 'lib/journeyman/configuration.rb', line 14 def name @name end |
#options ⇒ Object (readonly)
Internal: Name of the factory, and configuration options.
14 15 16 |
# File 'lib/journeyman/configuration.rb', line 14 def 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) [: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) [:builder] ||= proc || block end |
#builder ⇒ Object
Internal: Returns the finder proc, or the default builder strategy.
88 89 90 |
# File 'lib/journeyman/configuration.rb', line 88 def builder [: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) [:finder] = proc || block end |
#finder ⇒ Object
Internal: Returns the finder proc, or the default finder strategy.
83 84 85 |
# File 'lib/journeyman/configuration.rb', line 83 def finder [: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) [:ignored] = ->(attrs) do attrs = attrs.dup; ignored.each { |key| attrs.delete(key) }; attrs end end |
#model ⇒ Object
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 [: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) [:processor] ||= proc || block end |
#processor ⇒ Object
Internal: Returns a custom processor, or ignore directive.
93 94 95 |
# File 'lib/journeyman/configuration.rb', line 93 def processor [:ignored] || [:processor] end |