Sha256: 2342f4654f052739248bcd83436f8fda42320244a6c18268e47a2abc3c80837d

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'active_support/core_ext'

module Falcor
  class Fabricator
    class << self
      attr_accessor :factories

      def define(name, options={}, &block)
        factories[name] = self.new({ :name => name, :block => block }.merge(options))
      end

      def create(name, overrides, block)
        raise "No factory defined: [#{name}]" unless factories.has_key?(name)

        factories[name].create(overrides)
      end
    end

    def create(overrides)
      @options = @definition.merge(overrides)
      @options[:super] = Factory if @options[:super].nil?
      klass = create_class
      klass.create_methods(@options[:block])

      klass.new(overrides)
    end

  private

    def initialize(options)
      @definition = options
    end

    def create_class
      Object.send(:remove_const, class_name) rescue nil
      klass = Object.const_set class_name, Class.new(@options[:super])

      klass
    end

    def class_name
      (@options[:class] || @options[:name]).to_s.camelize
    end

    self.factories = {}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
falcor-0.0.3 lib/falcor/fabricator.rb
falcor-0.0.2 lib/falcor/fabricator.rb