sdl-ng 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74e84faac50a5b0f87162c41f4432f17e7c14eb9
4
- data.tar.gz: 859743f5d41746354019bf5a4d651049e2837137
3
+ metadata.gz: fb1fee76f36a1fd102acbf05d58bba9ed837b854
4
+ data.tar.gz: 74b4dc6a128f8919d4c18b844a505a75cf5ba9a2
5
5
  SHA512:
6
- metadata.gz: 9b906a6a61d26020c7f8c3881faadda48600c58dcd94848f46b73d36076e9e1937ca436fe089b617ade562168113a818ad285b910c576a5a2e7fbf286bf2e460
7
- data.tar.gz: 7e74e16dde89de290666cd991f6eace874f6657643acc7aab8ba5469c9bca68db72f77eec15b62dd9e9960fd3eefe699d1b214d92728ef12972b821c2e5ea0c6
6
+ metadata.gz: 5006227d35538634704f03c65ef56acfd06b75343d3a3540fa847b0624defcc7da386ef9290e4ff25c50af13b6ec501f1a814b46c64eb9325f5ca31f63c1fa45
7
+ data.tar.gz: a741df861180d5714844946b3d7bd42d7b99fedb55b17c2cba729652daa5b14b81b2ec4cdc9422ca74e6ef59b2e28a5a95e2e33f78fa3915eb37b745222a812f
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
1
  --format Fuubar
2
- --color
2
+ --color
data/.simplecov CHANGED
@@ -3,5 +3,8 @@ SimpleCov.start do
3
3
  add_group 'Library', 'lib'
4
4
  add_group 'Specs', 'spec'
5
5
 
6
+ add_filter 'bin'
7
+ add_filter 'examples'
8
+
6
9
  refuse_coverage_drop
7
10
  end
@@ -1,16 +1,72 @@
1
1
  ##
2
2
  # A service compendium allows the definition of service facts, types and services.
3
3
  class SDL::Base::ServiceCompendium
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :VocabularyLoadTransaction, 'sdl/base/transactions/vocabulary_load_transaction'
7
+ autoload :ServiceLoadTransaction, 'sdl/base/transactions/service_load_transaction'
8
+
9
+ include VocabularyLoadTransaction
10
+ include ServiceLoadTransaction
11
+
12
+ ##
13
+ # A list of all +Fact+ classes registered in this compendium
14
+ # @!attribute [r] fact_classes
15
+ # @return Array<Class>
4
16
  attr :fact_classes
17
+
18
+ ##
19
+ # A list of all +Type+ classes registered in this compendium
20
+ # @!attribute [r] types
21
+ # @return Array<Class>
5
22
  attr :types
23
+
24
+ ##
25
+ # Registered codes for +SDLSimpleType+s, used for defining property types.
26
+ #
27
+ # @!attribute [r] sdltype_codes
28
+ # @return Hash{String => Class}
6
29
  attr :sdltype_codes
30
+
31
+ ##
32
+ # A map containing predefined type instances, mapped to their Type classes.
33
+ #
34
+ # @!attribute [r] type_instances
35
+ # @return Hash{Class => Hash{Symbol => Type}}
7
36
  attr :type_instances
37
+
38
+ ##
39
+ # A map of service names to service objects. It contains all loaded services in
40
+ # this compendium.
41
+ #
42
+ # @!attribute [r] services
43
+ # @return Hash{String => Service}
8
44
  attr :services
9
45
 
46
+ ##
47
+ # The service compendium specific copy of the ServiceMethods module. See #ServiceMethods
48
+ #
49
+ # @!attribute [r] service_methods
50
+ # @return [Module]
51
+ attr :service_methods
52
+
53
+ ##
54
+ # The current URI when loading services.
55
+ #
56
+ # @!attribute [r] current_uri
57
+ # @return [String]
58
+ attr :current_uri
59
+
60
+ ##
61
+ # Initializes the compendium.
10
62
  def initialize
11
63
  @fact_classes, @types, @services = [], [], {}
12
64
  @type_instances, @sdltype_codes = {}, {}
13
65
 
66
+ @service_methods = Module.new() do |mod|
67
+ include ServiceMethods
68
+ end
69
+
14
70
  register_default_types
15
71
  end
16
72
 
@@ -22,17 +78,30 @@ class SDL::Base::ServiceCompendium
22
78
  self.instance_eval &type_instances_definition
23
79
  end
24
80
 
81
+ def with_uri(current_uri, &block)
82
+ old_uri = @current_uri
83
+ @current_uri = current_uri
84
+
85
+ begin
86
+ block.call
87
+ ensure
88
+ @current_uri = old_uri
89
+ end
90
+ end
91
+
25
92
  # Defines a new class of service facts
26
93
  def fact(sym, &fact_definition)
27
94
  receiver = SDL::Receivers::FactReceiver.new(sym, self)
28
95
  receiver.instance_eval &fact_definition if block_given?
29
96
  receiver.subclasses.each do |fact_class|
97
+ fact_class.loaded_from = @current_uri
98
+
30
99
  @fact_classes << fact_class
31
100
 
32
101
  # Refer to the symbolic name of the current class, which can be a subclass of
33
102
  sym = fact_class.local_name.underscore.to_sym
34
103
 
35
- ServiceMethods.class_eval do
104
+ @service_methods.class_eval do
36
105
  unless SDL::Base::Service.instance_methods.include? sym
37
106
  define_method sym do
38
107
  @facts.find {|fact| fact.is_a? fact_class}
@@ -51,6 +120,7 @@ class SDL::Base::ServiceCompendium
51
120
  # Defines a new type and returns it
52
121
  def type(sym, &type_definition)
53
122
  receiver = SDL::Receivers::TypeReceiver.new(sym, self)
123
+ receiver.klass.loaded_from = @current_uri
54
124
  receiver.instance_eval &type_definition if block_given?
55
125
  register_sdltype_codes(receiver.klass)
56
126
  register_sdltype(receiver.klass)
@@ -63,17 +133,18 @@ class SDL::Base::ServiceCompendium
63
133
  def service(sym, &service_definition)
64
134
  receiver = SDL::Receivers::ServiceReceiver.new(sym, self)
65
135
  receiver.instance_eval &service_definition if block_given?
136
+ receiver.service.loaded_from = current_uri
66
137
  @services[sym] = receiver.service
67
138
  receiver.service.symbolic_name = sym.to_s
68
139
  receiver.service.compendium = self
69
- receiver.service.extend ServiceMethods
140
+ receiver.service.extend @service_methods
70
141
  receiver.service
71
142
  end
72
143
 
73
144
  ##
74
- # Registers the type under its codes
145
+ # Registers an SDLSimpleType under its code
75
146
  def register_sdltype_codes(type)
76
- type.instance_variable_get(:@codes).each do |code|
147
+ type.codes.each do |code|
77
148
  @sdltype_codes[code] = type
78
149
  end
79
150
  end
@@ -88,6 +159,7 @@ class SDL::Base::ServiceCompendium
88
159
 
89
160
  receiver.instance_eval &block if block != nil
90
161
 
162
+ receiver.instance.loaded_from = current_uri
91
163
  receiver.instance.identifier = identifier
92
164
  @type_instances[type][identifier] = receiver.instance
93
165
  end
@@ -102,12 +174,39 @@ class SDL::Base::ServiceCompendium
102
174
  end
103
175
  end
104
176
 
177
+ ##
178
+ # Unloads all items with the specified uri from this service compendium
179
+ def unload(uri)
180
+ @fact_classes.delete_if do |item| item.loaded_from.eql?(uri) end
181
+ @types.delete_if do |item| item.loaded_from.eql?(uri) end
182
+ @services.delete_if do |item| item.loaded_from.eql?(uri) end
183
+
184
+ @type_instances.delete_if do |klass, klass_map|
185
+ klass_map.delete_if do |symbol, type|
186
+ type.loaded_from.eql? uri
187
+ end
188
+
189
+ klass.loaded_from.eql? uri
190
+ end
191
+ end
192
+
105
193
  # This module is included in every service defined by this compendium and contains methods to easily retreive
106
194
  # certain fact instances by their name.
107
195
  module ServiceMethods
108
196
 
109
197
  end
110
198
 
199
+ # This module contains the #loaded_from attribute for all items, which can be loaded by a service compendium
200
+ module ServiceCompendiumMixin
201
+ attr_accessor :loaded_from
202
+
203
+ [SDL::Base::Type, SDL::Base::Type.class, SDL::Base::Service].each do |klass|
204
+ klass.instance_eval do
205
+ include SDL::Base::ServiceCompendium::ServiceCompendiumMixin
206
+ end
207
+ end
208
+ end
209
+
111
210
  private
112
211
  ##
113
212
  # Registers all default types
@@ -0,0 +1,35 @@
1
+ # A transaction for loading vocabulary definition
2
+ module SDL::Base::ServiceCompendium::ServiceLoadTransaction
3
+ ##
4
+ # Loads a service, either from a file or from a path recursively.
5
+ #
6
+ # Service definition files are expected to end with +.service.rb+
7
+ # @param path_or_filename[String] Either a filename or a path
8
+ def load_service_from_path(path_or_filename)
9
+ path_or_filename = [path_or_filename] if File.file? path_or_filename
10
+
11
+ Dir.glob(File.join(path_or_filename, '**', '*.service.rb')) do |filename|
12
+ service_name = filename.match(%r[.+/(.+).service.rb])[1]
13
+
14
+ load_service_from_string File.read(filename), service_name, filename
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Loads a service from a string. The URI is used with ServiceCompendium#with_uri.
20
+ # @param [String] service_definition The service definition
21
+ # @param [String] service_name The service name
22
+ # @param [String] uri The URI
23
+ # @raise [SyntaxError] If there is an error in service_definition
24
+ def load_service_from_string(service_definition, service_name, uri)
25
+ begin
26
+ with_uri uri do
27
+ service service_name do
28
+ eval service_definition, binding
29
+ end
30
+ end
31
+ rescue Exception => e
32
+ raise SyntaxError.new("Error while loading '#{service_name}' from '#{uri}': #{e}")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ # A transaction for loading vocabulary definition
2
+ module SDL::Base::ServiceCompendium::VocabularyLoadTransaction
3
+ ##
4
+ # Loads vocabulary, either from a file or from a path recursively.
5
+ #
6
+ # Vocabulary definition files are expected to end with +.sdl.rb+
7
+ # @param path_or_filename[String] Either a filename or a path
8
+ def load_vocabulary_from_path(path_or_filename)
9
+ path_or_filename = [path_or_filename] if File.file? path_or_filename
10
+
11
+ Dir.glob(File.join(path_or_filename, '**', '*.sdl.rb')) do |filename|
12
+ with_uri filename do
13
+ load_vocabulary_from_string File.read(filename), filename
14
+ end
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Loads a vocabulary from a string. The URI is used with ServiceCompendium#with_uri.
20
+ # @param [String] vocabulary_definition The vocabulary definition
21
+ # @param [String] uri The URI
22
+ def load_vocabulary_from_string(vocabulary_definition, uri)
23
+ begin
24
+ with_uri uri do
25
+ eval vocabulary_definition, binding
26
+ end
27
+ rescue Exception => e
28
+ unload uri
29
+
30
+ raise "Error while loading vocabulary from #{uri}: #{e}"
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  module SDL
2
2
  module NG
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -6,22 +6,34 @@ module SDL::Types::SDLType
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- ## The Ruby type, which is to be wrapped
9
+ ##
10
+ # The Ruby type, which is to be wrapped
11
+ #
12
+ # @return Class
10
13
  attr :wrapped_type
11
14
 
12
- ## The codes, which are to be used to refer to this type
15
+ ##
16
+ # The codes, which are to be used to refer to this type
17
+ #
18
+ # @return [Symbol]
13
19
  attr :codes
14
20
 
15
21
  ##
16
22
  # Sets the wrapped Ruby type
23
+ # @param type Class
17
24
  def wraps(type)
18
25
  @wrapped_type = type
19
26
  end
20
27
 
21
28
  ##
22
- # Registers the codes +symbols+ to be used to refer to this type
29
+ # Adds the +symbols+ to be used to refer to this type to the list
30
+ # of codes and returns them.
31
+ # @param [Array<Symbol>] *symbols the symbols to be used
32
+ # @return [Array<Symbol>] all resulting symbols
23
33
  def codes(*symbols)
24
- @codes = symbols
34
+ @codes ||= []
35
+ symbols.each do |symbol| @codes << symbol end
36
+ @codes
25
37
  end
26
38
  end
27
39
  end
@@ -0,0 +1,28 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require 'rspec'
4
+
5
+ describe 'the bundled examples' do
6
+ let :compendium do
7
+ SDL::Base::ServiceCompendium.new
8
+ end
9
+
10
+ let :loaded_compendium do
11
+ expect {
12
+ compendium.load_vocabulary_from_path File.join(__dir__, '..', 'examples', 'vocabulary')
13
+ compendium.load_service_from_path File.join(__dir__, '..', 'examples', 'services')
14
+ }.not_to raise_exception
15
+
16
+ compendium
17
+ end
18
+
19
+ it 'can be loaded' do
20
+ expect { loaded_compendium }.not_to raise_exception
21
+ end
22
+
23
+ it 'traces load path' do
24
+ expected_path = File.join(__dir__, '..', 'examples', 'services', 'google_drive_for_business.service.rb')
25
+
26
+ expect(loaded_compendium.services['google_drive_for_business'].loaded_from).to eq expected_path
27
+ end
28
+ end
@@ -59,6 +59,87 @@ describe 'The service compendium' do
59
59
  expect(subject).to be CONTEXT_SELF
60
60
  end
61
61
 
62
+ it 'is independent of one another' do
63
+ first_compendium = SDL::Base::ServiceCompendium.new
64
+ second_compendium = SDL::Base::ServiceCompendium.new
65
+
66
+ first_compendium.facts_definition do
67
+ fact :first
68
+ end
69
+
70
+ second_compendium.facts_definition do
71
+ fact :second
72
+ end
73
+
74
+ expect {
75
+ first_compendium.service 'first_service' do
76
+ has_second
77
+ end
78
+ }.to raise_exception
79
+
80
+ service = first_compendium.service 'second_service' do
81
+ has_first
82
+ end
83
+
84
+ expect(service).not_to respond_to :second
85
+ end
86
+
87
+ it 'does not partially load invalid services' do
88
+ compendium = SDL::Base::ServiceCompendium.new
89
+
90
+ compendium.facts_definition do
91
+ fact :first_fact
92
+ fact :second_fact
93
+ end
94
+
95
+ service_definition = <<END
96
+ has_first_fact
97
+ has_second_fact
98
+ has_third_fact
99
+ END
100
+
101
+ expect{
102
+ compendium.load_service_from_string(service_definition, 'my_service', 'empty')
103
+ }.to raise_exception
104
+
105
+ expect(compendium.services.size).to eq 0
106
+ end
107
+
108
+ it 'does not partially load invalid vocabulary' do
109
+ new_compendium = SDL::Base::ServiceCompendium.new
110
+
111
+ new_compendium.with_uri 'rspec' do
112
+ new_compendium.type_instances_definition do
113
+ fact :first_fact
114
+ type :first_type
115
+
116
+ first_type :abc
117
+ end
118
+ end
119
+
120
+ facts_definition = <<END
121
+ fact :second_fact
122
+ type :second_type
123
+
124
+ second_type :def
125
+
126
+ fakt :third_fact
127
+ tüpe :third_type
128
+ END
129
+
130
+ expect {
131
+ new_compendium.load_vocabulary_from_string(facts_definition, 'empty')
132
+ }.to raise_exception
133
+
134
+ expect(new_compendium.fact_classes.count).to eq 1
135
+ expect(new_compendium.types.count).to eq 1
136
+ expect(new_compendium.type_instances.count).to eq 1
137
+ end
138
+
139
+ it 'does not let double definitions of types and facts happen' do
140
+ pending 'Not yet implemented'
141
+ end
142
+
62
143
  context 'with defined example classes' do
63
144
  subject do
64
145
  compendium = SDL::Base::ServiceCompendium.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdl-ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Slawik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
11
+ date: 2014-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -233,6 +233,8 @@ files:
233
233
  - lib/sdl/base/property.rb
234
234
  - lib/sdl/base/service.rb
235
235
  - lib/sdl/base/service_compendium.rb
236
+ - lib/sdl/base/transactions/service_load_transaction.rb
237
+ - lib/sdl/base/transactions/vocabulary_load_transaction.rb
236
238
  - lib/sdl/base/type.rb
237
239
  - lib/sdl/base/uri_mapped_resource.rb
238
240
  - lib/sdl/exporters.rb
@@ -268,6 +270,7 @@ files:
268
270
  - sdl-ng.gemspec
269
271
  - spec/bin_spec.rb
270
272
  - spec/documentation_spec.rb
273
+ - spec/examples_spec.rb
271
274
  - spec/exporter_spec.rb
272
275
  - spec/fact_type_instance_definition_spec.rb
273
276
  - spec/nokogiri_spec.rb
@@ -307,6 +310,7 @@ summary: Framework for building descriptions of business services.
307
310
  test_files:
308
311
  - spec/bin_spec.rb
309
312
  - spec/documentation_spec.rb
313
+ - spec/examples_spec.rb
310
314
  - spec/exporter_spec.rb
311
315
  - spec/fact_type_instance_definition_spec.rb
312
316
  - spec/nokogiri_spec.rb