Sha256: b58941b5e7b4fe2118abb2e648d9b15402c7f33468a4c9270f622f6bd222adc9

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require 'singleton'

module OData4
  # Provides a registry for keeping track of multiple OData4::Service instances
  class ServiceRegistry
    include Singleton

    # Add a service to the Registry
    #
    # @param service [OData4::Service] service to add to the registry
    def add(service)
      initialize_instance_variables
      @services << service if service.is_a?(OData4::Service) && !@services.include?(service)
      @services_by_name[service.name] = @services.find_index(service)
      @services_by_url[service.service_url] = @services.find_index(service)
    end

    # Lookup a service by URL or name
    #
    # @param lookup_key [String] the URL or name to lookup
    # @return [OData4::Service, nil] the OData4::Service or nil
    def [](lookup_key)
      initialize_instance_variables
      index = @services_by_name[lookup_key] || @services_by_url[lookup_key]
      index.nil? ? nil : @services[index]
    end

    # (see #add)
    def self.add(service)
      OData4::ServiceRegistry.instance.add(service)
    end

    # (see #[])
    def self.[](lookup_key)
      OData4::ServiceRegistry.instance[lookup_key]
    end

    private

    def initialize_instance_variables
      @services ||= []
      @services_by_name ||= {}
      @services_by_url ||= {}
    end

    def flush
      @services = []
      @services_by_name = {}
      @services_by_url = {}
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
odata4-0.9.1 lib/odata4/service_registry.rb
odata4-0.9.0 lib/odata4/service_registry.rb
odata4-0.8.2 lib/odata4/service_registry.rb
odata4-0.8.1 lib/odata4/service_registry.rb
odata4-0.8.0 lib/odata4/service_registry.rb
odata4-0.7.0 lib/odata4/service_registry.rb