Sha256: 57039fd2f7a7f60f077bbe3b725672ce49e86fdf5cdad029f988adfaf953ef29

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

require 'yaml'

module EbisuConnection
  class Config
    include Enumerable

    class << self
      attr_writer :replica_file

      def config
        return @config if defined?(@config)

        conf = YAML.load_file(replica_file)
        @config = conf[EbisuConnection.env.to_s] || {}
      end

      private

      def replica_file
        return @replica_file if @replica_file

        raise "nothing replica_file. You have to set a file path using EbisuConnection.replica_file= method" unless defined?(Rails)

        file = %w(yml yaml).map{|ext| Rails.root.join("config/replica.#{ext}").to_s }.detect {|f| File.exist?(f) }
        return file if file

        raise "nothing replica_file. You have to put a config/replica.yml file"
      end
    end

    def initialize(spec_name)
      @conf = load_config(spec_name)
    end

    def each(&block)
      @conf.each(&block)
    end

    private

    def load_config(spec_name)
      c = self.class.config

      if c.is_a?(Hash)
        c[spec_name] || c
      else
        c
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ebisu_connection-3.1.0 lib/ebisu_connection/config.rb
ebisu_connection-3.0.0 lib/ebisu_connection/config.rb