Sha256: d4e45e9a66bf897ab97be5625c86a1060e3495f019b6abf4f5c049e9cdb318d0

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'ostruct'

module Pickle
  def self.config(&block)
    returning(@config ||= Config.new) do |config|
      config.configure(&block) if block_given?
    end
  end
  
  class Config
    attr_writer :adapters, :factories, :mappings
    
    def initialize(&block)
      configure(&block) if block_given?
    end
    
    def configure(&block)
      yield(self)
    end
    
    def adapters
      @adapters ||= [:machinist, :factory_girl, :active_record]
    end
    
    def adapter_classes
      adapters.map {|a| a.is_a?(Class) ? a : "pickle/adapter/#{a}".classify.constantize}
    end
    
    def factories
      @factories ||= adapter_classes.reverse.inject({}) do |factories, adapter|
        factories.merge(adapter.factories.inject({}){|h, f| h.merge(f.name => f)})
      end
    end

    def factory_names
      factories.keys
    end
    
    def mappings
      @mappings ||= []
    end
    
    # Usage: map 'me', 'myself', 'I', :to => 'user: "me"'
    def map(*args)
      options = args.extract_options!
      raise ArgumentError, "Usage: map 'search' [, 'search2', ...] :to => 'replace'" unless args.any? && options[:to].is_a?(String)
      search = args.size == 1 ? args.first.to_s : "(?:#{args.join('|')})"
      self.mappings << OpenStruct.new(:search => search, :replace => options[:to])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ianwhite-pickle-0.1.2 lib/pickle/config.rb
ianwhite-pickle-0.1.3 lib/pickle/config.rb