Sha256: 30e8c1c0840417664eeade3a3c43114d9527a65f425e27840b83de5690d6f082

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

module Barometer
  module Utils
    class VersionedRegistration
      def initialize
        @registrations = {}
      end

      def register(key, *args, &block)
        if block_given?
          version = args.shift
          value = block
        elsif args.size > 1
          version = args.shift
          value = args.shift
        else
          version = nil
          value = args.shift
        end

        return if has_version?(key, version)
        add_value(key, version, value)
      end

      def find(key, version=nil)
        return unless has_key?(key)
        registration = find_version(key, version) || find_default(key) || {}
        registration.fetch(:value, nil)
      end

      def size
        @registrations.inject(0){|count,key_value| key_value[1].size + count }
      end

      private

      def has_key?(key)
        @registrations.has_key? key
      end

      def has_version?(key, version)
        registrations_for_key = @registrations[key] || []
        registrations_for_key.detect{|r| r[:version] == (version || :default)}
      end

      def add_value(key, version, value)
        registration = {
          version: (version || :default),
          value: value
        }
        add_registration(key, registration)
      end

      def add_registration(key, registration)
        registrations_for_key = @registrations[key] || []
        registrations_for_key << registration
        @registrations[key] = registrations_for_key
      end

      def find_version(key, version)
        registrations = @registrations[key]
        registrations.detect{|r| r[:version] == version}
      end

      def find_default(key)
        registrations = @registrations[key]
        registrations.detect{|r| r[:version] == :default}
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
barometer-0.9.7 lib/barometer/utils/versioned_registration.rb
barometer-0.9.6 lib/barometer/utils/versioned_registration.rb
barometer-0.9.5 lib/barometer/utils/versioned_registration.rb
barometer-0.9.4 lib/barometer/utils/versioned_registration.rb
barometer-0.9.3 lib/barometer/utils/versioned_registration.rb
barometer-0.9.2 lib/barometer/utils/versioned_registration.rb
barometer-0.9.1 lib/barometer/utils/versioned_registration.rb
barometer-0.9.0 lib/barometer/utils/versioned_registration.rb