Sha256: cf364b699d15682e1b0aab9017fe14e9372b7e56924bf9f5590381f320239e78

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# encoding: UTF-8

require "watchmaker/version"

module Watchmaker # :nodoc:

  # Attribute accessors to hold profile mappings.
  #
  mattr_accessor :profiles

  # Learn a new profile.
  #
  def self.learn(profile, options = {}, &block)

    # Initialize the profiles unless they exist.
    #
    @@profiles = {} unless @@profiles

    # Add the block to the list of known profiles.
    #
    @@profiles[profile] = {
      :options => options, 
      :block   => block 
    }

  end

  # Contruct a profile based on lambda.
  #
  def self.construct(profile)

    # Store created objects.
    #
    objects = []

    # If a profile exists, call the proc we've stored; if not, raise.
    #
    if selected_profile = @@profiles[profile]

      if options = selected_profile[:options]

        # For any supplied factories, create them.
        #
        if factories = options[:factories] 
          factories.each do |factory|
            objects << Factory.create(factory.to_sym)
          end
        end

        # For any supplied watchmakers, create them.
        #
        if watchmakers = options[:watchmakers] 
          watchmakers.each do |watchmaker|
            objects << Watchmaker.construct(watchmaker.to_sym)
          end
        end

      end

      # Run the supplied block.
      #
      if block = selected_profile.delete(:block)
        objects << block.call(objects)
      end

      # Return objects.
      #
      objects

    else
      raise "#{profile} is not a valid profile"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
watchmaker-0.0.1 lib/watchmaker.rb