Sha256: 6b69a7e029455145ccb9f01595154cab7767f5e0c340795149938a190837b282

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Fabricators
  class Definitions
    include Callbacks
 
    def initialize
      reset!
    end

    def reset!
      @fabricators = {}
      @generators = {}
      @callbacks = {}
    end
 
    def fabricator(name, options={}, &block)
      fabricator = Fabricator.new(name, options, &block)
      iterate_names name, options do |name|
        @fabricators[name] = fabricator
      end
    end
 
    def generator(name, first=0, options={}, &block)
      generator = Generator.new(first, &block)
      iterate_names name, options do |name|
        @generators[name] = generator
      end
    end

    def find(name, type)
      case type
      when :fabricator
        @fabricators[name]
      when :generator
        @generators[name]
      end.tap do |definition|
        raise "Definition #{name} of type #{type} not found" unless definition
      end
    end

    protected

    def iterate_names(name, options)
      names = [name]
      if aliases = options[:aliases]
        case aliases
        when Array
          names |= aliases
        else
          names << aliases
        end
      end
      names.each do |name|
        yield name
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fabricators-0.0.1 lib/fabricators/definitions.rb