Class: SDL::Base::ServiceCompendium

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
ServiceLoadTransaction, VocabularyLoadTransaction
Defined in:
lib/sdl/base/service_compendium.rb,
lib/sdl/base/service_compendium/service_load_transaction.rb,
lib/sdl/base/service_compendium/vocabulary_load_transaction.rb

Overview

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

Defined Under Namespace

Modules: LoadTransaction, ServiceCompendiumMixin, ServiceLoadTransaction, VocabularyLoadTransaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ServiceLoadTransaction

#load_service_from_path

Methods included from LoadTransaction

#to_files_array

Methods included from VocabularyLoadTransaction

#load_vocabulary_from_path, #load_vocabulary_from_string

Constructor Details

#initializeServiceCompendium

Initializes the compendium.



82
83
84
85
86
87
88
89
90
91
92
93
# 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

Instance Attribute Details

#all_codesHash{String => Class} (readonly)

Registered codes for SDLSimpleTypes and Types.

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

Returns:

  • (Hash{String => Class})


28
29
30
# File 'lib/sdl/base/service_compendium.rb', line 28

def all_codes
  @all_codes
end

#current_uriString (readonly)

The current URI when loading services.

Returns:

  • (String)


78
79
80
# File 'lib/sdl/base/service_compendium.rb', line 78

def current_uri
  @current_uri
end

#sdl_simple_type_codesHash{String => Class} (readonly)

Registered codes for SDLSimpleTypes.

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

Returns:

  • (Hash{String => Class})


37
38
39
# File 'lib/sdl/base/service_compendium.rb', line 37

def sdl_simple_type_codes
  @sdl_simple_type_codes
end

#servicesObject (readonly)

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



69
70
71
# File 'lib/sdl/base/service_compendium.rb', line 69

def services
  SDL::Base::Type::Service.instances
end

#type_codesHash{String => Class} (readonly)

Registered codes for Types.

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

Returns:

  • (Hash{String => Class})


46
47
48
# File 'lib/sdl/base/service_compendium.rb', line 46

def type_codes
  @type_codes
end

#type_instancesObject (readonly)

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



53
54
55
56
57
58
59
60
61
# 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

#typesEnumerable<Class> (readonly)

An enumerator of all registered Type classes

Returns:

  • (Enumerable<Class>)


17
18
19
# File 'lib/sdl/base/service_compendium.rb', line 17

def types
  SDL::Base::Type.subtypes_recursive.drop(2)
end

Instance Method Details

#clear!Object

Clears all loaded types and instances



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object

Parameters:

  • type (Class)

    The instance type

  • identifier (Symbol)

    The identifier



190
191
192
193
194
195
196
197
198
199
# 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?Boolean

A compendium is empty, if there are neither types, nor services loaded.

Returns:

  • (Boolean)

    If this compendium is empty.



98
99
100
101
# 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) ⇒ Object Also known as: service_properties



119
120
121
# File 'lib/sdl/base/service_compendium.rb', line 119

def facts_definition(&facts_definitions)
  type :service, &facts_definitions
end

#load_service_from_string(service_definition, service_name, uri) ⇒ Object

Loads a service from a string. The URI is used with ServiceCompendium#with_uri.

Parameters:

  • service_definition (String)

    The service definition

  • service_name (String)

    The service name

  • uri (String)

    The URI

Raises:

  • (SyntaxError)

    If there is an error in service_definition



31
32
33
34
35
36
37
# 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_itemsObject

Yields all items, which were loaded by this compendium



212
213
214
215
216
217
218
219
220
# 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_globallyObject

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



203
204
205
206
207
208
# 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) ⇒ Object

Registers an SDLSimpleType or SDLType under its code

Parameters:

  • type (Class)

    The type to be registered.



166
167
168
169
170
171
172
173
174
175
176
# 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) ⇒ Object

Allows this compendium to be used for adding new type instances



180
181
182
183
184
185
186
# 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

#type(sym, &type_definition) ⇒ Object

Defines a new type and returns it



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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_definition(&type_instances_definition) ⇒ Object



125
126
127
# File 'lib/sdl/base/service_compendium.rb', line 125

def type_instances_definition(&type_instances_definition)
  self.instance_eval &type_instances_definition
end

#unload(uri) ⇒ Object

Unloads all items with the specified uri from this service compendium



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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) ⇒ Object

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

Parameters:

  • current_uri (String)

    The URI, with which the block should be run.

  • block (Block)

    The block, which will be called.



134
135
136
137
138
139
140
141
142
143
# 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