Sha256: d052af894d519bcb08a02f085f7afbd7cdd92c4904a108de909c47de41fe2553

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

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
        @spec = 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 spec(slave_group)
        @spec ||= get_spec
        @spec.merge(@spec[slave_group] || {})
      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)
        File.join(Rails.root, "config/slave.yaml")
      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[FreshConnection.env.to_s] || {}
      end

      def get_spec
        ActiveRecord::Base.configurations[FreshConnection.env]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ebisu_connection-0.2.0 lib/ebisu_connection/conf_file.rb
ebisu_connection-0.1.1 lib/ebisu_connection/conf_file.rb