class SDL::Base::ServiceCompendium

A service compendium allows the definition of service facts, types and services.

Attributes

all_codes[R]

Registered codes for +SDLSimpleType+s and +Type+s.

These are used in the definition of the type of a property.

@!attribute [r] all_codes @return [Hash{String => Class}]

current_uri[R]

The current URI when loading services.

@!attribute [r] current_uri @return [String]

sdl_simple_type_codes[R]

Registered codes for +SDLSimpleType+s.

These are used in the definition of the type of a property.

@!attribute [r] sdl_simple_type_codes @return [Hash{String => Class}]

type_codes[R]

Registered codes for +Type+s.

These are used in the definition of the type of a property.

@!attribute [r] type_codes @return [Hash{String => Class}]

Public Class Methods

new() click to toggle source

Initializes the compendium.

# File lib/sdl/base/service_compendium.rb, line 82
def initialize
  @services = {}
  @type_instances, @sdl_simple_type_codes, @type_codes, @all_codes = {}, {}, {}, {}

  @type_instances.default = {}

  register_default_types

  @current_uri = :default

  type :service
end

Public Instance Methods

clear!() click to toggle source

Clears all loaded types and instances

# File lib/sdl/base/service_compendium.rb, line 104
def clear!
  SDL::Base::Type::Service.clear_instances!

  SDL::Base::Type.subtypes_recursive.drop(2).each do |type|
    type.codes.each do |code|
      @all_codes.delete code
      @type_codes.delete code
    end

    type.unregister
  end

  SDL::Base::Type::Service.clear_properties!
end
create_type_instance(type, identifier, &block) click to toggle source

@param [Class] type The instance type @param [Symbol] identifier The identifier

# File lib/sdl/base/service_compendium.rb, line 190
def create_type_instance(type, identifier, &block)
  receiver = SDL::Receivers::TypeInstanceReceiver.new(type.new)

  receiver.instance_eval &block if block != nil

  receiver.instance.loaded_from = current_uri
  receiver.instance.identifier = identifier

  type.instances[identifier] = receiver.instance
end
empty?() click to toggle source

A compendium is empty, if there are neither types, nor services loaded. @return [Boolean] If this compendium is empty.

# File lib/sdl/base/service_compendium.rb, line 98
def empty?
  SDL::Base::Type.subtypes_recursive.count == 2 &&
  SDL::Base::Type.instances_recursive.to_a.empty?
end
facts_definition(&facts_definitions) click to toggle source
# File lib/sdl/base/service_compendium.rb, line 119
def facts_definition(&facts_definitions)
  type :service, &facts_definitions
end
Also aliased as: service_properties
load_service_from_string(service_definition, service_name, uri) click to toggle source

Loads a service from a string. The URI is used with ServiceCompendium#with_uri. @param [String] service_definition The service definition @param [String] service_name The service name @param [String] uri The URI @raise [SyntaxError] If there is an error in service_definition

# File lib/sdl/base/service_compendium/service_load_transaction.rb, line 31
def load_service_from_string(service_definition, service_name, uri)
  with_uri uri do
    service service_name do
      eval service_definition, binding, uri
    end
  end
end
loaded_items() { |type| ... } click to toggle source

Yields all items, which were loaded by this compendium

# File lib/sdl/base/service_compendium.rb, line 212
def loaded_items
  SDL::Base::Type.subtypes_recursive.drop(1).each do |type|
    yield type

    type.instances.each do |sym, instance|
      yield instance
    end
  end
end
register_classes_globally() click to toggle source

Registers all classes by their local_name to be used in all scopes

# File lib/sdl/base/service_compendium.rb, line 203
def register_classes_globally
  SDL::Base::Type.subtypes_recursive.each do |defined_class|
    Object.send(:remove_const, defined_class.local_name) if Object.const_defined? defined_class.local_name.to_sym
    Object.const_set defined_class.local_name, defined_class
  end
end
register_codes(type) click to toggle source

Registers an SDLSimpleType or SDLType under its code @param [Class] type The type to be registered.

# File lib/sdl/base/service_compendium.rb, line 166
def register_codes(type)
  if type < SDL::Types::SDLSimpleType
    type.codes.each do |c| @sdl_simple_type_codes[c] = type end
  else
    type.codes.each do |c| @type_codes[c] = type end
  end

  type.codes.each do |code|
    @all_codes[code] = type
  end
end
register_sdltype(type) click to toggle source

Allows this compendium to be used for adding new type instances

# File lib/sdl/base/service_compendium.rb, line 180
def register_sdltype(type)
  # Define a method, which adds the type instance defined in the block to this compendium and adds it as a
  # constant the the type class
  self.class.send(:define_method, type.local_name.underscore) do |identifier, &block|
    create_type_instance(type, identifier, &block)
  end
end
service_properties(&facts_definitions)
Alias for: facts_definition
services() click to toggle source

A map of service names to service objects. It contains all loaded services in this compendium.

@!attribute [r] services @return Hash{String => Service}

# File lib/sdl/base/service_compendium.rb, line 69
def services
  SDL::Base::Type::Service.instances
end
type(sym, &type_definition) click to toggle source

Defines a new type and returns it

# File lib/sdl/base/service_compendium.rb, line 146
def type(sym, &type_definition)
  type = SDL::Base::Type.subtype(sym, &type_definition)

  SDL::Base::Type.subtypes_recursive.drop(1).each do |type|
    # All newly loaded types have loaded_from.nil?
    # Cannot iterate over subtypes, as types can define property types on the fly, which are
    # certainly NOT subtypes of themselves
    if(type.loaded_from.nil?)
      type.loaded_from = @current_uri
      register_codes type
      register_sdltype type
    end
  end

  type
end
type_instances() click to toggle source

A map containing predefined type instances, mapped to their Type classes.

@!attribute [r] type_instances @return Hash{Class => Hash{Symbol => Type}}

# File lib/sdl/base/service_compendium.rb, line 53
def type_instances
  type_instances = {}

  types.each do |type|
    type_instances[type] = type.instances
  end

  type_instances
end
type_instances_definition(&type_instances_definition) click to toggle source
# File lib/sdl/base/service_compendium.rb, line 125
def type_instances_definition(&type_instances_definition)
  self.instance_eval &type_instances_definition
end
types() click to toggle source

An enumerator of all registered Type classes @!attribute [r] types @return [Enumerable<Class>]

# File lib/sdl/base/service_compendium.rb, line 17
def types
  SDL::Base::Type.subtypes_recursive.drop(2)
end
unload(uri) click to toggle source

Unloads all items with the specified uri from this service compendium

# File lib/sdl/base/service_compendium.rb, line 224
def unload(uri)
  SDL::Base::Type::Service.instances.delete_if do |symbolic_name, service|
    service.loaded_from.eql?(uri)
  end

  SDL::Base::Type.subtypes_recursive.each do |type|
    if type.loaded_from.eql?(uri) then
      type.codes.each do |code|
        @all_codes.delete code
        @type_codes.delete code
      end

      type.unregister
    end
  end
end
with_uri(current_uri, &block) click to toggle source

Runs the +&block+ with specified current_uri and restores the old current_uri.

@param [String] current_uri The URI, with which the block should be run. @param [Block] block The block, which will be called.

# File lib/sdl/base/service_compendium.rb, line 134
def with_uri(current_uri, &block)
  old_uri = @current_uri
  @current_uri = current_uri

  begin
    block.call
  ensure
    @current_uri = old_uri
  end
end

Private Instance Methods

register_default_types() click to toggle source

Registers all default types

# File lib/sdl/base/service_compendium.rb, line 255
def register_default_types
  SDL::Types::SDLSimpleType.descendants.each do |type|
    register_codes type
  end
end