Class: SDL::Base::ServiceCompendium
- Inherits:
-
Object
- Object
- SDL::Base::ServiceCompendium
- Defined in:
- lib/sdl/base/service_compendium.rb
Overview
A service compendium allows the definition of service facts, types and services.
Defined Under Namespace
Modules: ServiceMethods
Instance Attribute Summary collapse
-
#fact_classes ⇒ Object
readonly
Returns the value of attribute fact_classes.
-
#sdltype_codes ⇒ Object
readonly
Returns the value of attribute sdltype_codes.
-
#services ⇒ Object
readonly
Returns the value of attribute services.
-
#type_instances ⇒ Object
readonly
Returns the value of attribute type_instances.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#fact(sym, &fact_definition) ⇒ Object
Defines a new class of service facts.
- #facts_definition(&facts_definitions) ⇒ Object
-
#initialize ⇒ ServiceCompendium
constructor
A new instance of ServiceCompendium.
-
#register_classes_globally ⇒ Object
Registers all classes by their #local_name to be used in all scopes.
-
#register_sdltype(type) ⇒ Object
Allows this compendium to be used for adding new type instances.
-
#register_sdltype_codes(type) ⇒ Object
Registers the type under its codes.
-
#service(sym, &service_definition) ⇒ Object
Defines a new service, adds all service methods, and returns it.
-
#type(sym, &type_definition) ⇒ Object
Defines a new type and returns it.
- #type_instances_definition(&type_instances_definition) ⇒ Object
Constructor Details
#initialize ⇒ ServiceCompendium
Returns a new instance of ServiceCompendium.
10 11 12 13 14 15 |
# File 'lib/sdl/base/service_compendium.rb', line 10 def initialize @fact_classes, @types, @services = [], [], {} @type_instances, @sdltype_codes = {}, {} register_default_types end |
Instance Attribute Details
#fact_classes ⇒ Object (readonly)
Returns the value of attribute fact_classes.
4 5 6 |
# File 'lib/sdl/base/service_compendium.rb', line 4 def fact_classes @fact_classes end |
#sdltype_codes ⇒ Object (readonly)
Returns the value of attribute sdltype_codes.
6 7 8 |
# File 'lib/sdl/base/service_compendium.rb', line 6 def sdltype_codes @sdltype_codes end |
#services ⇒ Object (readonly)
Returns the value of attribute services.
8 9 10 |
# File 'lib/sdl/base/service_compendium.rb', line 8 def services @services end |
#type_instances ⇒ Object (readonly)
Returns the value of attribute type_instances.
7 8 9 |
# File 'lib/sdl/base/service_compendium.rb', line 7 def type_instances @type_instances end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
5 6 7 |
# File 'lib/sdl/base/service_compendium.rb', line 5 def types @types end |
Instance Method Details
#fact(sym, &fact_definition) ⇒ Object
Defines a new class of service facts
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sdl/base/service_compendium.rb', line 26 def fact(sym, &fact_definition) receiver = SDL::Receivers::FactReceiver.new(sym, self) receiver.instance_eval &fact_definition if block_given? receiver.subclasses.each do |fact_class| @fact_classes << fact_class # Refer to the symbolic name of the current class, which can be a subclass of sym = fact_class.local_name.underscore.to_sym ServiceMethods.class_eval do unless SDL::Base::Service.instance_methods.include? sym define_method sym do @facts.find {|fact| fact.is_a? fact_class} end end unless SDL::Base::Service.instance_methods.include? sym.to_s.pluralize.to_sym define_method sym.to_s.pluralize do @facts.find_all {|fact| fact.is_a? fact_class} end end end end end |
#facts_definition(&facts_definitions) ⇒ Object
17 18 19 |
# File 'lib/sdl/base/service_compendium.rb', line 17 def facts_definition(&facts_definitions) self.instance_eval &facts_definitions end |
#register_classes_globally ⇒ Object
Registers all classes by their #local_name to be used in all scopes
98 99 100 101 102 103 |
# File 'lib/sdl/base/service_compendium.rb', line 98 def register_classes_globally (@fact_classes + @types).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_sdltype(type) ⇒ Object
Allows this compendium to be used for adding new type instances
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sdl/base/service_compendium.rb', line 83 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| receiver = SDL::Receivers::TypeInstanceReceiver.new(type.new, self) receiver.instance_eval &block if block != nil receiver.instance.identifier = identifier @type_instances[type][identifier] = receiver.instance end end |
#register_sdltype_codes(type) ⇒ Object
Registers the type under its codes
75 76 77 78 79 |
# File 'lib/sdl/base/service_compendium.rb', line 75 def register_sdltype_codes(type) type.instance_variable_get(:@codes).each do |code| @sdltype_codes[code] = type end end |
#service(sym, &service_definition) ⇒ Object
Defines a new service, adds all service methods, and returns it
63 64 65 66 67 68 69 70 71 |
# File 'lib/sdl/base/service_compendium.rb', line 63 def service(sym, &service_definition) receiver = SDL::Receivers::ServiceReceiver.new(sym, self) receiver.instance_eval &service_definition if block_given? @services[sym] = receiver.service receiver.service.symbolic_name = sym.to_s receiver.service.compendium = self receiver.service.extend ServiceMethods receiver.service end |
#type(sym, &type_definition) ⇒ Object
Defines a new type and returns it
52 53 54 55 56 57 58 59 60 |
# File 'lib/sdl/base/service_compendium.rb', line 52 def type(sym, &type_definition) receiver = SDL::Receivers::TypeReceiver.new(sym, self) receiver.instance_eval &type_definition if block_given? register_sdltype_codes(receiver.klass) register_sdltype(receiver.klass) @types << receiver.klass @type_instances[receiver.klass] = {} receiver.klass end |
#type_instances_definition(&type_instances_definition) ⇒ Object
21 22 23 |
# File 'lib/sdl/base/service_compendium.rb', line 21 def type_instances_definition(&type_instances_definition) self.instance_eval &type_instances_definition end |