Sha256: f42a4f9593a8025ef3c1da9e61a137708da54c2e4bf4136f66a7819f59678c1e

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module CaptainPlanet
  class Builder
    attr_reader :environments
    
    def self.process(klass,pattern)
      builder = Builder.new(klass)
      Dir[pattern].each do |path|
        name = File.basename(path, '.rb')
        config = File.read(path)
        builder[name] = config
      end
      builder
    end
    
    def initialize(klass=Environment)
      @klass = klass
      @environments = {}
    end
    
    # Add a new environment to the thing or find an existing one.
    def environment name, config=nil, &block
      environments[name] = klass.configure(config, &block)
    end
    alias_method :[]=, :environment
    
    def [] name
      environments[name]
    end
    
    # Converts the incoming klass value into something we can instanciate for realz
    def klass
      if @klass.is_a?(Class)
        @klass
      else
        unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ @klass.to_s
          raise NameError, "#{@klass.inspect} is not a valid constant name!"
        end
        
        Object.module_eval("::#{$1}", __FILE__, __LINE__)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
captain_planet-0.2.0 lib/captain_planet/builder.rb
captain_planet-0.1.0 lib/captain_planet/builder.rb