class Journeyman::Builder

Internal: Builds and creates objects, using the configuration provided.

Attributes

config[R]

Public Class Methods

new(name, options, config) click to toggle source
# File lib/journeyman/builder.rb, line 12
def initialize(name, options, config)
  @config = Configuration.new(name, options, config)
end

Public Instance Methods

build(attrs={}) click to toggle source

Internal: Builds a new instance, using the configuration specified in the factory.

attrs - The attributes used to build the object

Returns a new instance of the object.

# File lib/journeyman/builder.rb, line 27
def build(attrs={})
  check_build_arguments(attrs)
  attrs = merge_defaults(attrs)
  attrs = Journeyman.execute(processor, attrs) if processor
  config.builder.call(attrs)
end
create(attrs={}) click to toggle source

Internal: Create a new instance, using the configuration specified in the factory.

attrs - The attributes used to build the object

Returns a new instance of the object.

# File lib/journeyman/builder.rb, line 40
def create(attrs={})
  build(attrs).tap { |instance|
    instance.save!
    Journeyman.execute(after_create_callback, instance, attrs)
  }
end
find(id) click to toggle source

Internal: Executes the finder.

# File lib/journeyman/builder.rb, line 17
def find(id)
  config.finder.call(id)
end

Private Instance Methods

check_build_arguments(attrs) click to toggle source

Internal: Checks the attributes to make sure it's a Hash, to provide more insight on wrong usage.

# File lib/journeyman/builder.rb, line 61
def check_build_arguments(attrs)
  unless attrs.is_a?(Hash)
    raise ArgumentError, "Journeyman expected a Hash, but received: #{attrs}"
  end
end
merge_defaults(attributes={}) click to toggle source

Internal: Merges the default attributes to the specified attributes Hash.

Returns the modified Hash.

# File lib/journeyman/builder.rb, line 52
def merge_defaults(attributes={})
  attributes.tap do |attrs|
    attrs.merge!(static_defaults){ |key, user, default| user } # Reverse Merge
    dynamic_defaults.each { |key, value| attrs[key] ||= Journeyman.execute(value, attrs) }
  end
end