Sha256: e575c5a14f7a8308861f215eb71b822fc49425e6496ef8ba5bff35f1f780d2df

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'yaml'

module EbisuConnection
  class ConfFile
    class << self
      attr_writer :slaves_file, :check_interval

      def if_modify
        if time_to_check? && modify?
          yield
        end
      end

      def conf_clear!
        @slaves_conf = nil
      end

      def slaves_conf(slave_group)
        @slaves_conf ||= get_slaves_conf
        if @slaves_conf.is_a?(Hash)
          @slaves_conf[slave_group] || @slaves_conf
        else
          @slaves_conf
        end
      end

      def slaves_file
        return @slaves_file if @slaves_file
        raise "nothing slaves_file. You have to set a file path using EbisuConnection.slaves_file= method" unless defined?(Rails)

        @slaves_file = %w(yml yaml).map{|ext| Rails.root.join("config/slave.#{ext}").to_s }.detect {|f| File.exist?(f) }
        raise "nothing slaves_file. You have to put a config/slave.yml file" unless @slaves_file
        @slaves_file
      end

      def check_interval
        @check_interval || 1.minute
      end

      private

      def time_to_check?
        now = Time.now
        @check_time ||= now

        return false if now - @check_time < check_interval

        @check_time = now
        true
      end

      def modify?
        @file_mtime != File.mtime(slaves_file)
      end

      def get_slaves_conf
        @file_mtime = File.mtime(slaves_file)
        conf = YAML.load_file(slaves_file)
        conf[EbisuConnection.env.to_s] || {}
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ebisu_connection-2.2.0 lib/ebisu_connection/conf_file.rb
ebisu_connection-2.1.0 lib/ebisu_connection/conf_file.rb
ebisu_connection-2.0.0 lib/ebisu_connection/conf_file.rb