class Journeyman::Configuration
Public: Provides a DSL for configuration of the factories.
Constants
- METHOD_OPTIONS
- OPTIONS
Attributes
Internal: Name of the factory, and configuration options.
Internal: Name of the factory, and configuration options.
Public Class Methods
Public: Receives the name of the factory, and configuration options.
Yields itself to the block passed to the `Journeyman.define`.
# 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
Private Class Methods
Internal: Performance optimization, the option method is defined the first time it's accessed.
# File lib/journeyman/configuration.rb, line 155 def self.define_option_method(name) class_eval <<-OPTION def #{name}(value=nil) options[:#{name}] = value if value options[:#{name}] end OPTION end
Public Instance Methods
Public: Invoked after creating an object, useful for setting up secondary or optional relations.
# File lib/journeyman/configuration.rb, line 69 def after_create(proc=nil, &block) options[:after_create_callback] ||= proc || block end
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.
# File lib/journeyman/configuration.rb, line 49 def build(proc=nil, &block) options[:builder] ||= proc || block end
Internal: Returns the finder proc, or the default builder strategy.
# File lib/journeyman/configuration.rb, line 88 def builder options[:builder] ||= parent_builder || default_builder end
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.
# File lib/journeyman/configuration.rb, line 36 def find(proc=nil, &block) options[:finder] = proc || block end
Internal: Returns the finder proc, or the default finder strategy.
# File lib/journeyman/configuration.rb, line 83 def finder options[:finder] ||= default_finder end
Public: Allows to ignore certain attributes, that can be accessed in the after_create
callback.
# 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
Internal: Class of the model to build, used in the default build strategy.
# File lib/journeyman/configuration.rb, line 78 def model options[:model] ||= infer_model_class(name) end
Public: Attributes processor, allows to modify the passed attributes before building an instance.
# File lib/journeyman/configuration.rb, line 55 def process(proc=nil, &block) options[:processor] ||= proc || block end
Internal: Returns a custom processor, or ignore directive.
# File lib/journeyman/configuration.rb, line 93 def processor options[:ignored] || options[:processor] end
Private Instance Methods
Internal: Creates the object by simply using the initializer.
# File lib/journeyman/configuration.rb, line 106 def default_builder ->(attrs={}) { model.new(attrs) } end
Internal: Default finder strategy used.
# File lib/journeyman/configuration.rb, line 100 def default_finder options[:finder_attribute] ||= :name ->(name) { model.find_by(finder_attribute => name) } end
Internal: Prepares the default arguments for the builder. The return value of the configuration block may provide the defaults.
result - The return value of the configuration block.
Returns nothing.
# File lib/journeyman/configuration.rb, line 123 def extract_defaults(result) defaults = options[:defaults] || (result if result.is_a?(Hash)) || {} options[:dynamic_defaults], options[:static_defaults] = partition_defaults(defaults) end
Internal: Infers a model class.
# File lib/journeyman/configuration.rb, line 135 def infer_model_class(name) if defined? ActiveSupport name.to_s.classify.constantize else Object.const_get(name.to_s.split('_').collect!{ |w| w.capitalize }.join) end end
Internal: Uses the builder of the parent to construct the object.
Returns the builder if a :parent was set, or nil.
# File lib/journeyman/configuration.rb, line 113 def parent_builder ->(attrs={}) { Journeyman.build(parent, attrs) } if parent end
Internal: Splits static from dynamic arguments for runtime performance.
# File lib/journeyman/configuration.rb, line 130 def partition_defaults(defaults) defaults.partition { |key, value| value.is_a?(Proc) }.map(&:to_h) end
Internal: Checks the configuration's consistency
# File lib/journeyman/configuration.rb, line 144 def verify_valid_or_exit if options[:parent] && options[:builder] raise InvalidArgumentError, 'custom builder can not be used in combination with `parent: true`' end if options[:ignored] && options[:processor] raise InvalidArgumentError, 'custom processor can not be used in combination with `ignore`' end end