Sha256: b5dd7b8861bb8a1ef4a5a4c610da3a0d443332cc83b28f2b34a44d0f321e8d10

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8

module AMQP
  class Failover
    class Config < ::Hash
      
      attr_accessor :last_fail
      
      def initialize(hash = {}, last_fail_date = nil)
        self.replace(defaults.merge(symbolize_keys(hash)))
        self.last_fail = last_fail_date if last_fail_date
      end
      
      def defaults
        AMQP.settings
      end
      
      def symbolize_keys(hash = {})
        hash.inject({}) do |result, (key, value)|
          result[key.is_a?(String) ? key.to_sym : key] = value
          result
        end
      end
      
      # order by latest fail, potentially useful if random config selection is used
      def <=>(other)
        if self.respond_to?(:last_fail) && other.respond_to?(:last_fail)
          if self.last_fail.nil? && other.last_fail.nil?
            return 0
          elsif self.last_fail.nil? && !other.last_fail.nil?
            return 1
          elsif !self.last_fail.nil? && other.last_fail.nil?
            return -1
          end
          return other.last_fail <=> self.last_fail
        end
        return 0
      end
      
    end # Config
  end # Failover
end # AMQP

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amqp-failover-0.0.3 lib/amqp/failover/config.rb