rtm-rails 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/DISCLAIMER ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2008-2010 Topic Maps Lab, University of Leipzig.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and limitations
13
+ under the License.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README ADDED
@@ -0,0 +1,84 @@
1
+ ==== Topic Maps for Rails (rtm-rails)
2
+ RTM-Rails is the Rails-Adapter for Ruby Topic Maps. It allows simple configuration of topicmaps in config/topicmaps.yml.
3
+
4
+ ==== Overview
5
+ From a developer's perspective, RTM is a schema-less database management system.
6
+ The Topic Maps standard (described below) on which RTM is based provides a way of creating a self-describing schema just by using it.
7
+ You can use RTM as a complement data storage to ActiveRecord in your Rails apps.
8
+
9
+ ==== Quickstart - existing Rails project
10
+ jruby script/generate topicmaps
11
+
12
+ Run the command above after installing rtm-rails. This will create
13
+ * a minimal default configuration: config/topicmaps.yml and
14
+ * a file with more examples and explanations config/topicmaps.example.yml
15
+ * a file README.topicmaps.txt which contains more information how to use it and where to find more information
16
+ * an initializer to load the topicmaps at startup
17
+ * a rake task to migrate the topic maps backends
18
+ in your rails application.
19
+
20
+ ==== Quickstart - new Rails project
21
+
22
+ For a new Rails application these are the complete initial steps:
23
+ jruby -S rails my_topicmaps_app
24
+ cd my_topicmaps_app
25
+ jruby -S script/generate jdbc
26
+ jruby -S script/generate topicmaps
27
+ # The following lines are necessary because Rails does not have a template
28
+ # for the H2 database and Ontopia does not support the Rails default SQLite3.
29
+ sed -e "s/sqlite3/h2/" config/database.yml > config/database.yml.h2
30
+ mv config/database.yml.h2 config/database.yml
31
+ # Prepare the database and then check if all is OK
32
+ jruby -S rake topicmaps:migrate_backends
33
+ jruby -S rake topicmaps:check
34
+
35
+ ==== Usage inside the application
36
+
37
+ When everything is fine, let's create our first topic:
38
+ jruby -S script/console
39
+ TM[:example].get!("http://example.org/my/first/topic")
40
+ # and save the topic map
41
+ TM[:example].commit
42
+
43
+ Access the configured topic maps anywhere in your application like this:
44
+ TM[:example]
45
+
46
+ To retrieve all topics, you can do
47
+ TM[:example].topics
48
+
49
+ To retrieve a specific topic by its subject identifier:
50
+ TM[:example].get("http://example.org/my/topic")
51
+
52
+ Commit the changes to the database permanently:
53
+ TM[:example].commit
54
+
55
+ ... or abort the transaction:
56
+ TM[:example].abort
57
+
58
+ More information can be found on http://rtm.topicmapslab.de/
59
+
60
+ ==== Minimal configuration
61
+ default:
62
+ topicmaps:
63
+ example: http://rtm.topicmapslab.de/example1/
64
+
65
+ The minimal configuration creates a single topic map, named :example with the locator given.
66
+ This topic map will be persisted in the same database as your ActiveRecord connection if not specified otherwise.
67
+ The default backend is OntopiaRDBMS (from the rtm-ontopia gem).
68
+ A more complete configuration can be found in config/topicmaps.example.yml after running "jruby script/generate topicmaps".
69
+ It also includes how to specifiy multiple connections to different data stores and so on.
70
+
71
+ ==== Topic Maps
72
+ Topic Maps is an international industry standard (ISO13250) for interchangeably representing information
73
+ about the structure of information resources used to define topics, and the relationships between topics.
74
+ A set of one or more interrelated documents that employs the notation defined by this International Standard is called a topic map.
75
+ A topic map defines a multidimensional topic space - a space in which the locations are topics,
76
+ and in which the distances between topics are measurable in terms of the number of intervening topics
77
+ which must be visited in order to get from one topic to another, and the kinds of relationships that define
78
+ the path from one topic to another, if any, through the intervening topics, if any.
79
+ In addition, information objects can have properties, as well as values for those properties, assigned to them.
80
+ The Topic Maps Data Model which is used in this implementation can be found on http://www.isotopicmaps.org/sam/sam-model/.
81
+
82
+ ==== License
83
+ Copyright 2009 Topic Maps Lab, University of Leipzig.
84
+ Apache License, Version 2.0
data/lib/rtm-rails.rb ADDED
@@ -0,0 +1,4 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ require File.join(File.dirname(__FILE__), 'rtm/rails')
data/lib/rtm/rails.rb ADDED
@@ -0,0 +1,285 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ unless Object.const_defined?("Gem") && rtmgem = Gem.loaded_specs["rtm-rails"]
5
+ javatmapi_path = File.expand_path(File.join(File.dirname(__FILE__), "../../../rtm/lib"))
6
+ $LOAD_PATH.unshift javatmapi_path if File.directory?(javatmapi_path)
7
+ end
8
+ require 'rtm'
9
+
10
+ module RTM
11
+ # The RTM-Rails Gem largely remodels the Rails startup sequence and
12
+ # initializes the application {TopicMap}s TopicMap {TopicMap} from the topicmaps configuration
13
+ # file (Defaults to +config/topicmaps.yml+).
14
+ # The {TopicMap} s defined there can then be accessed using {TM} bla {TM.[identifier]}
15
+ module Rails
16
+ class << self
17
+ # The Configuration instance used to configure the Rails environment
18
+ def configuration
19
+ @@configuration
20
+ end
21
+
22
+ def configuration=(configuration)
23
+ @@configuration = configuration
24
+ end
25
+
26
+ def initialized?
27
+ @initialized || false
28
+ end
29
+
30
+ def initialized=(initialized)
31
+ @initialized ||= initialized
32
+ end
33
+ end
34
+
35
+ class Configuration
36
+ # The path to the topicmaps configuration file to use. (Defaults to
37
+ # <tt>config/topicmaps.yml</tt>.)
38
+ # @see Rails::Configuration#database_configuration_file
39
+ attr_accessor :topicmaps_configuration_file
40
+
41
+ # The connection configuration for the current environment as collected
42
+ # from the configuration files.
43
+ attr_accessor :connections
44
+
45
+ # The topicmaps configuration for the current environment as collected
46
+ # from the configuration files.
47
+ attr_accessor :topicmaps
48
+
49
+ # Create a new Configuration instance, initialized with the default
50
+ # values.
51
+ def initialize
52
+ self.topicmaps_configuration_file = default_topicmaps_configuration_file
53
+ end
54
+
55
+ # Loads and returns the contents of the #database_configuration_file.
56
+ # The contents of the file are processed via ERB before being sent
57
+ # through YAML::load.
58
+ # @see Rails::Configuration#database_configuration
59
+ def topicmaps_configuration
60
+ require 'erb'
61
+ YAML::load(ERB.new(::IO.read(topicmaps_configuration_file)).result)
62
+ end
63
+
64
+ def default_topicmaps_configuration_file
65
+ File.join(::Rails.root, 'config', 'topicmaps.yml')
66
+ end
67
+ end
68
+
69
+ class Initializer
70
+ attr_reader :configuration
71
+
72
+ # @see Rails::Initializer.run
73
+ def self.run(configuration = Configuration.new)
74
+ yield configuration if block_given?
75
+ initializer = new configuration
76
+ initializer.process
77
+ initializer
78
+ end
79
+
80
+ # Create a new Initializer instance that references the given
81
+ # Configuration instance.
82
+ # @see Rails::Initializer#initialize
83
+ def initialize(configuration)
84
+ @configuration = configuration
85
+ end
86
+
87
+ # Sequentially step through all of the available initialization
88
+ # routines, in order (view execution order in source).
89
+ def process
90
+ RTM::Rails.configuration = configuration
91
+
92
+ load_configuration
93
+
94
+ initialize_topicmaps_systems
95
+
96
+ check_systems_initialized
97
+
98
+ initialize_topicmaps
99
+
100
+ RTM::Rails.initialized = true
101
+ end
102
+
103
+ # Get the database configuration from ActiveRecord, or if ActiveRecord
104
+ # is not loaded: load it manually.
105
+ def load_configuration
106
+ # load the default topicmaps configuration file
107
+ complete_config = configuration.topicmaps_configuration || {}
108
+
109
+ # load the config from the default section
110
+ default_config = complete_config["default"] || {}
111
+ default_config.symbolize_keys!
112
+ # load the config from the current-environment section
113
+ environment_config = complete_config[::Rails.env] || {}
114
+ environment_config.symbolize_keys!
115
+
116
+ # obtain the current configuration by merging default config and the
117
+ # current environment's config. This is a one-step deep-merge as we
118
+ # just want to merge inside the subkeys.
119
+ current_configuration = default_config
120
+ environment_config.keys.each do |key|
121
+ if current_configuration.key?(key) && current_configuration[key].is_a?(Hash)
122
+ current_configuration[key].merge!(environment_config[key])
123
+ else
124
+ current_configuration[key] = environment_config[key]
125
+ end
126
+ end
127
+
128
+ # set the topicmaps configuration
129
+ configuration.topicmaps = (current_configuration[:topicmaps] || {}).symbolize_keys
130
+
131
+ # expand shortcuts in topicmaps configuration
132
+ configuration.topicmaps.each do |identifier, tm_config|
133
+ # Convert the topicmap configuration to a Hash if the topicmap was
134
+ # specified using the locator-only shortcut.
135
+ configuration.topicmaps[identifier] = tm_config = {:locator => tm_config} unless tm_config.is_a?(Hash)
136
+
137
+ tm_config.symbolize_keys!
138
+ end
139
+
140
+ # read the connections configuration
141
+ current_connections = current_configuration[:connections] || {}
142
+ current_connections.symbolize_keys!
143
+
144
+ # get default connection (if specified)
145
+ default_connection = current_configuration[:connection]
146
+
147
+ # if no default connection was specified look for the rails one.
148
+ default_connection ||= ::Rails::Configuration.new.database_configuration[::Rails.env]
149
+
150
+ # use the explicitly defined default connection or the above-loaded
151
+ current_connections[:default] ||= default_connection
152
+
153
+ # set the connections configuration
154
+ configuration.connections = current_connections
155
+
156
+ # expand shortcuts in connection configuration
157
+ configuration.connections.each do |identifier, con_config|
158
+ con_config.symbolize_keys!
159
+ end
160
+
161
+ # forget default connection if no topicmap uses it and use_for_topicmaps is false
162
+ keep_default = false
163
+ keep_default = true if configuration.topicmaps.any? {|identifier, tm| ! tm.key?(:connection) || tm[:connection].to_s == "default"}
164
+ keep_default = true if configuration.connections[:default][:use_for_topicmaps]
165
+ configuration.connections.delete(:default) unless keep_default
166
+ end
167
+
168
+ # Connect to the TopicMap engine(s) using the configured settings
169
+ def initialize_topicmaps_systems
170
+ configuration.connections.each do |identifier, conf|
171
+ # If the connection was declared but not defined, copy over the data from default
172
+ conf ||= configuration.connections[:default].merge(:identifier => identifier)
173
+ RTM.connect(conf.merge(:identifier => identifier))
174
+ end
175
+ end
176
+
177
+ # Some Topic Maps backends like the Ontopia RDBMS backend need some
178
+ # setup, e.g. some database tables. This method checks if these
179
+ # preconditions are met.
180
+ def check_systems_initialized
181
+ failed_connections = RTM.connections.reject { |identifier,con| con.check }
182
+ unless failed_connections.empty?
183
+ puts "Warning: The following topicmap connection(s) are not properly prepared: #{failed_connections.map{|identifier,con| identifier}.to_sentence}"
184
+ puts " Maybe you added them recently to your config but forgot 'rake topicmaps:migrate_backends'?"
185
+ end
186
+ end
187
+
188
+ # Get or create the topic maps and make them available in {TM[]}
189
+ def initialize_topicmaps
190
+ configuration.topicmaps.each do |identifier, tm_config|
191
+ # Get the configured connection (or create it if it was given inline)
192
+ con = case con_spec = tm_config[:connection]
193
+ when nil # nothing specified => default connection
194
+ RTM.connections[:default]
195
+ when Symbol, String # name given => named connection
196
+ RTM.connections[con_spec.to_sym]
197
+ when Hash # inline config given
198
+ c = connect_anonymous(con_spec)
199
+ puts "Warning: The connection for topicmap '#{identifier}' is not yet prepared. Please run the topicmaps:migrate_backends rake task." unless c.check
200
+ c
201
+ else
202
+ raise "Cannot determine connection for #{con_spec.inspect}"
203
+ end
204
+
205
+ begin
206
+ tm = con.create(tm_config[:locator])
207
+ TM::Manager.register_map(identifier, tm)
208
+ rescue NativeException => ne
209
+ if ne.cause.is_a?(Java::NetOntopiaUtils::OntopiaRuntimeException) &&
210
+ ne.cause.message =~ /The datatype type is unknown and the property 'net.ontopia.topicmaps.impl.rdbms.Platforms' is not set./ &&
211
+ (dbname = con.property("net.ontopia.topicmaps.impl.rdbms.Database")) =~ /sqlite/
212
+ puts "Warning: An Exception was thrown by the Ontopia RDBMS backend"
213
+ puts " This was probably because you're trying to use '#{dbname}', which is not supported by Ontopia."
214
+ end
215
+ raise ne
216
+ end
217
+ end
218
+ end
219
+
220
+ def connect_anonymous(con_spec)
221
+ con_spec.symbolize_keys!
222
+ RTM.connect(con_spec)
223
+ end
224
+
225
+ def connect_all_anonymous_connections
226
+ topicmaps_with_anonymous_connections = configuration.topicmaps.select {|identifier, tm_config| Hash === tm_config[:connection]}
227
+ topicmaps_with_anonymous_connections.map do |identifier, tm_config|
228
+ connection_config = tm_config[:connection].merge(:identifier => "anon_connection_for_tm_#{identifier}".to_sym)
229
+ connect_anonymous(connection_config)
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ # The TopicMapManager manages TopicMaps by registering a Topic Map using an
236
+ # identifier. The identifier is used to easily access the TopicMap
237
+ # everywhere without passing around the instance manually.
238
+ class TopicMapManager
239
+ attr_reader :topic_maps
240
+
241
+ def initialize
242
+ @topic_maps = {}
243
+ end
244
+
245
+ def register_map(identifier, map)
246
+ raise "A topic map with the identifier #{identifier.inspect} is already registered" if @topic_maps.keys.include?(identifier)
247
+ # TODO maybe we should allow to connect here?
248
+ @topic_maps[identifier] = map
249
+ end
250
+
251
+ def remove_map(identifier)
252
+ # TODO: maybe it should be removed from the system here?
253
+ @topic_maps.delete(identifier)
254
+ end
255
+
256
+ def [](identifier)
257
+ raise "There is no topic map registered with identifier #{identifier.inspect}" unless @topic_maps.keys.include?(identifier)
258
+ @topic_maps[identifier]
259
+ end
260
+ end
261
+ end
262
+
263
+ # The module TM can be used to easily access the TopicMaps used in an
264
+ # application. It uses a TopicMapManager and provides access to a TopicMap by
265
+ # using the Hash access method: {TM[]} or {TM.[]} or {TM.[bla]} or {TM.[](*args)}. TopicMaps can be configured in the
266
+ # +config/topicmaps.yaml+ file.
267
+ module TM
268
+ Manager = RTM::TopicMapManager.new
269
+
270
+ # Returns a TopicMap
271
+ #
272
+ # @param [Types] identifier of the TopicMap to return.
273
+ # @return [RTM::TopicMap] with the given identifier
274
+ def self.[](*args)
275
+ if args.size == 0
276
+ Manager.topic_maps
277
+ else
278
+ Manager[*args]
279
+ end
280
+ end
281
+ end
282
+
283
+ def TM(*args)
284
+ TM[*args]
285
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ namespace :topicmaps do
5
+
6
+ task :load_config => :rails_env do
7
+ initializer = RTM::Rails::Initializer.new(RTM::Rails::Configuration.new)
8
+ initializer.load_configuration
9
+ initializer.initialize_topicmaps_systems
10
+ initializer.connect_all_anonymous_connections
11
+ end
12
+
13
+ desc "Migrate RTM Backends to prepare them for storing topic maps"
14
+ task :migrate_backends => :load_config do
15
+ RTM.connections.each do |identifier, con|
16
+ unless con.check
17
+ print "Migrating #{identifier}..."
18
+ con.migrate
19
+ puts "done"
20
+ end
21
+ end
22
+ end
23
+
24
+ desc "Check if all RTM Backends are properly set up"
25
+ task :check => :load_config do
26
+ fine, failed = RTM.connections.partition { |identifier,con| con.check }
27
+ if failed.empty?
28
+ puts "All #{fine.size} topicmap connections seem to be set up fine: #{fine.map{|identifier,con| identifier}.to_sentence}"
29
+ else
30
+ puts "The following #{fine.size} topicmap connections are OK: #{fine.map{|identifier,con| identifier}.to_sentence}"
31
+ puts "The following #{failed.size} topicmap connections are NOT properly prepared: #{failed.map{|identifier,con| identifier}.to_sentence}"
32
+ puts "Run '#{$0} topicmaps:migrate_backends' to prepare them."
33
+ end
34
+ end
35
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ # This file was generated by the "topicmaps" generator, which is provided
5
+ # by the rtm-rails gem.
6
+
7
+ require 'rtm/rails'
8
+
9
+ RTM::Rails::Initializer.run
@@ -0,0 +1,33 @@
1
+ This file was created by the topicmaps generator which is provided by the
2
+ rtm-rails Gem.
3
+
4
+ 1. Configure the topic maps for your application in
5
+
6
+ config/topicmaps.yml
7
+
8
+
9
+ 2. Migrate your backends to make them ready for your topic maps:
10
+
11
+ jruby -S rake topicmaps:migrate_backends
12
+
13
+
14
+ 3. Use your topic maps in your application code:
15
+
16
+
17
+ TM[:mymap].topics.each do |topic|
18
+ # do something with the topic
19
+ end
20
+
21
+ TM[:another_map].get("xsd:integer") # get an existing topic
22
+
23
+ TM[:some_map].get!("a:b") # get topic (or create it if it does not exist)
24
+
25
+
26
+ See http://rubygems.org/gems/rtm-rails for information about the Gem.
27
+ See http://rtm.topicmapslab.de/ for information about RTM.
28
+ See http://www.topicmapslab.de/ for general information about Topic Maps.
29
+
30
+
31
+ ==== License for RTM and RTM-Rails
32
+ Copyright 2009 Topic Maps Lab, University of Leipzig.
33
+ Apache License, Version 2.0
@@ -0,0 +1,128 @@
1
+ # == config/topicmaps.example.yml - Example Topic Maps configuration file
2
+ # Configure your topic maps in config/topicmaps.yml as described here. It
3
+ # works like configuring databases.
4
+ #
5
+ # === Default Environment
6
+ # Most people want the same topic maps in all environments. The special
7
+ # environment "default" is the basis for all other enviroments. The simplest
8
+ # configuration is
9
+ #
10
+ default:
11
+ topicmaps:
12
+ example1: http://rtm.topicmapslab.de/example1/
13
+ #
14
+ # which provides a topic map called example1 with the given locator for all
15
+ # environments. This topic map is accessible as TM[:example1] in the
16
+ # application.
17
+ #
18
+ # The above example makes the following assumptions:
19
+ # * The database settings are from the Rails config from database.yml
20
+ # * If a database is configured (i.e. an adapter is set)
21
+ # the backend is ontopia_rdbms
22
+ # * else
23
+ # the backend is ontopia (which is in-memory)
24
+ #
25
+ # More details about that in the Defaults section at the end.
26
+ #
27
+ # === Connections
28
+ # A topic map is persisted in a backend. By default the same database
29
+ # connection that ActiveRecord uses will be used. This default is configured
30
+ # in the config/database.yml file. To override this default, specify your own
31
+ # connection as follows.
32
+ #
33
+ development:
34
+ connection:
35
+ adapter: h2
36
+ database: db/development
37
+ topicmaps:
38
+ example2: http://rtm.topicmapslab.de/example2/
39
+ #
40
+ # We choose the development environment and define a connection using the
41
+ # h2 adapter. H2 will create multiple files using the given database name as
42
+ # basis. Everything in the connection section works exactly like in the
43
+ # standard rails configuration. As above, also a topic map is configured.
44
+ # This topic map is accessible as TM[:example2] in the application.
45
+ #
46
+ # === Multiple Connections
47
+ # RTM allows to connect to multiple backends at the same time. To connect to
48
+ # multiple backends use the connections keyword. Each connection specified
49
+ # here must have a name to identify the connection. The following example
50
+ # creates two connections for the production environment: one persistent
51
+ # (called primary) using mysql and one temporary (called tmp) in memory.
52
+ # As with the topic maps, the names are arbitrary.
53
+ #
54
+ production:
55
+ connections:
56
+ primary:
57
+ adapter: mysql
58
+ encoding: utf8
59
+ reconnect: false
60
+ database: my_app_mysql_production
61
+ pool: 5
62
+ username: root
63
+ password:
64
+ socket: /tmp/mysql.sock
65
+ tmp:
66
+ backend: ontopia
67
+ tmp2:
68
+ topicmaps:
69
+ example4:
70
+ locator: http://rtm.topicmapslab.de/example4/
71
+ connection: primary
72
+ example5:
73
+ locator: http://rtm.topicmapslab.de/example5/
74
+ connection: tmp
75
+ example6:
76
+ locator: http://rtm.topicmapslab.de/example6/
77
+ connection: tmp2
78
+ example7:
79
+ locator: http://example.org/7
80
+ connection:
81
+ adapter: h2
82
+ database: db/development
83
+
84
+ # The connection tmp2 above provides no further information so it falls back
85
+ # to the defaults: the ontopia in-memory backend (same as the connection tmp).
86
+ #
87
+ # Instead of an connection name, example 7 defines the connection inline. The
88
+ # inline definition works exactly like the separate ones.
89
+ #
90
+ # === Defaults
91
+ # As mentioned above, there are defaults for most settings. There are two
92
+ # major sections and consequently two sets of defaults.
93
+ #
94
+ # ==== Defaults for Connections
95
+ # Connections can defined stand-alone in the connections-section and for a
96
+ # specific topic map in its connection-property.
97
+ #
98
+ # Stand-alone connections must have a name. If no properties are provided, the
99
+ # default connection is used. If no default connection was specified, the
100
+ # database properties from the Rails database.yml are used. Instead of
101
+ # properties, also the name of another connection can be provided to create an
102
+ # alias to this connection.
103
+ #
104
+ # A connection for a specific topic map can also refer to a named connection
105
+ # (or the default connection if nothing is provided). To create an anonymous
106
+ # connection a Hash of properties can be used.
107
+ #
108
+ # The connection must include the adapter details which are equivalent to
109
+ # the adapter settings used in ActiveRecord::Base.
110
+ # Some backends, like the Ontopia backend support additional settings which
111
+ # can also be provided here.
112
+ #
113
+ # All connections are migrated using the rake task topicmaps:migrate_backend
114
+ # and checked with the rake task topicmaps:check. The default connection is
115
+ # only included there if it is used by at least one topicmap or the property
116
+ # use_for_topicmaps is set:
117
+ # use_for_topicmaps: true
118
+ #
119
+ # ==== Defaults for topic maps.
120
+ # Each topic map has a name, similar to a named connection. There are no
121
+ # anonymous or default topic maps.
122
+ # Next to the name of the topic map there can be either a +locator+ or a
123
+ # +Hash+ of properties. A locator is an IRI which is an internationalized URI,
124
+ # see RFC 3987, http://www.ietf.org/rfc/rfc3987.txt for details.
125
+ # If a Hash of properties is provided it must include a key +locator+.
126
+ #
127
+ # A specific connection can be provided using the +connection+ keyword. If
128
+ # left out, the default connection is used.
@@ -0,0 +1,17 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+ #
4
+ # This file was generated by the "topicmaps" generator, which is provided by
5
+ # the rtm-rails gem.
6
+ #
7
+ # This file allows you to use your topic maps in your rails tasks and provides
8
+ # the Topic Maps backend migration tasks
9
+
10
+ begin
11
+ require "rtm/rails"
12
+ require "rtm/rails/rake_tasks"
13
+ rescue MissingSourceFile => msfe
14
+ puts "ERROR: Failed to load rtm/rails. Please make sure you have the rtm-rails gem installed."
15
+ puts " Maybe you're using standard C-Ruby's rake instead of JRuby's rake? Try jruby -S rake..." unless defined?(JRUBY_VERSION)
16
+ raise msfe
17
+ end
@@ -0,0 +1,11 @@
1
+ # == config/topicmaps.yml - Topic Maps configuration file
2
+ # Configure your topic maps here. It works like configuring databases.
3
+
4
+ # This creates a topic map :example with the base locator defined. The topic
5
+ # maps defined in section default will be available in all Rails environments.
6
+ # Add sections named after the enviroments to define different topic maps in
7
+ # each environment. See the file topicmaps.example for more examples.
8
+ # This topic map is accessible as TM[:example] in the application
9
+ default:
10
+ topicmaps:
11
+ example: http://rtm.topicmapslab.de/example/
@@ -0,0 +1,21 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ class TopicmapsGenerator < Rails::Generator::Base
5
+ def manifest
6
+ record do |m|
7
+ m.directory 'config/initializers'
8
+ m.template '../../rails/init.rb', File.join('config', 'initializers', 'topicmaps.rb')
9
+ m.template 'topicmaps.yml', File.join('config', 'topicmaps.yml')
10
+ m.template 'topicmaps.example.yml', File.join('config', 'topicmaps.example.yml')
11
+ m.directory 'lib/tasks'
12
+ m.template 'topicmaps.rake', File.join('lib', 'tasks', 'topicmaps.rake')
13
+ m.template 'README.txt', 'README.topicmaps.txt'
14
+ end
15
+ end
16
+
17
+ protected
18
+ def banner
19
+ "Usage: #{$0} jdbc\nGenerate Topic Maps bootstrapping files for your Rails application."
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+ #
4
+ # This is the vendor/plugin-version of the rake tasks. The generator version
5
+ # which is used with the rtm-rails Gem is in rails_generators/templates.
6
+ #
7
+ # This file allows you to use your topic maps in your rails tasks and provides
8
+ # the Topic Maps backend migration tasks
9
+
10
+ # The relative loading is necessary for usage of rtm-rails as a plugin
11
+ rtm_rails_dir = File.dirname(__FILE__) + "/../lib/"
12
+ require "#{rtm_rails_dir}rtm/rails"
13
+ require "#{rtm_rails_dir}rtm/rails/rake_tasks"
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtm-rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Benjamin Bock
13
+ - Arnim Bleier
14
+ - Uta Schulze
15
+ - Daniel Exner
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-04-13 00:00:00 +02:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rtm-javatmapi
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ segments:
31
+ - 0
32
+ - 3
33
+ - 0
34
+ version: 0.3.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rtm-ontopia
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ - 3
47
+ - 0
48
+ version: 0.3.0
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: |
52
+ ==== Topic Maps for Rails (rtm-rails)
53
+ RTM-Rails is the Rails-Adapter for Ruby Topic Maps. It allows simple configuration of topicmaps in config/topicmaps.yml.
54
+
55
+ ==== Overview
56
+ From a developer's perspective, RTM is a schema-less database management system.
57
+ The Topic Maps standard (described below) on which RTM is based provides a way of creating a self-describing schema just by using it.
58
+ You can use RTM as a complement data storage to ActiveRecord in your Rails apps.
59
+
60
+ ==== Quickstart - existing Rails project
61
+ jruby script/generate topicmaps
62
+
63
+ Run the command above after installing rtm-rails. This will create
64
+ * a minimal default configuration: config/topicmaps.yml and
65
+ * a file with more examples and explanations config/topicmaps.example.yml
66
+ * a file README.topicmaps.txt which contains more information how to use it and where to find more information
67
+ * an initializer to load the topicmaps at startup
68
+ * a rake task to migrate the topic maps backends
69
+ in your rails application.
70
+
71
+ ==== Quickstart - new Rails project
72
+
73
+ For a new Rails application these are the complete initial steps:
74
+ jruby -S rails my_topicmaps_app
75
+ cd my_topicmaps_app
76
+ jruby -S script/generate jdbc
77
+ jruby -S script/generate topicmaps
78
+ # The following lines are necessary because Rails does not have a template
79
+ # for the H2 database and Ontopia does not support the Rails default SQLite3.
80
+ sed -e "s/sqlite3/h2/" config/database.yml > config/database.yml.h2
81
+ mv config/database.yml.h2 config/database.yml
82
+ # Prepare the database and then check if all is OK
83
+ jruby -S rake topicmaps:migrate_backends
84
+ jruby -S rake topicmaps:check
85
+
86
+ ==== Usage inside the application
87
+
88
+ When everything is fine, let's create our first topic:
89
+ jruby -S script/console
90
+ TM[:example].get!("http://example.org/my/first/topic")
91
+ # and save the topic map
92
+ TM[:example].commit
93
+
94
+ Access the configured topic maps anywhere in your application like this:
95
+ TM[:example]
96
+
97
+ To retrieve all topics, you can do
98
+ TM[:example].topics
99
+
100
+ To retrieve a specific topic by its subject identifier:
101
+ TM[:example].get("http://example.org/my/topic")
102
+
103
+ Commit the changes to the database permanently:
104
+ TM[:example].commit
105
+
106
+ ... or abort the transaction:
107
+ TM[:example].abort
108
+
109
+ More information can be found on http://rtm.topicmapslab.de/
110
+
111
+ ==== Minimal configuration
112
+ default:
113
+ topicmaps:
114
+ example: http://rtm.topicmapslab.de/example1/
115
+
116
+ The minimal configuration creates a single topic map, named :example with the locator given.
117
+ This topic map will be persisted in the same database as your ActiveRecord connection if not specified otherwise.
118
+ The default backend is OntopiaRDBMS (from the rtm-ontopia gem).
119
+ A more complete configuration can be found in config/topicmaps.example.yml after running "jruby script/generate topicmaps".
120
+ It also includes how to specifiy multiple connections to different data stores and so on.
121
+
122
+ ==== Topic Maps
123
+ Topic Maps is an international industry standard (ISO13250) for interchangeably representing information
124
+ about the structure of information resources used to define topics, and the relationships between topics.
125
+ A set of one or more interrelated documents that employs the notation defined by this International Standard is called a topic map.
126
+ A topic map defines a multidimensional topic space - a space in which the locations are topics,
127
+ and in which the distances between topics are measurable in terms of the number of intervening topics
128
+ which must be visited in order to get from one topic to another, and the kinds of relationships that define
129
+ the path from one topic to another, if any, through the intervening topics, if any.
130
+ In addition, information objects can have properties, as well as values for those properties, assigned to them.
131
+ The Topic Maps Data Model which is used in this implementation can be found on http://www.isotopicmaps.org/sam/sam-model/.
132
+
133
+ ==== License
134
+ Copyright 2009 Topic Maps Lab, University of Leipzig.
135
+ Apache License, Version 2.0
136
+
137
+ email: rtm+rtm-rails-gem-20100413@topicmapslab.de
138
+ executables: []
139
+
140
+ extensions: []
141
+
142
+ extra_rdoc_files: []
143
+
144
+ files:
145
+ - lib/rtm/rails/rake_tasks.rb
146
+ - lib/rtm/rails.rb
147
+ - lib/rtm-rails.rb
148
+ - rails/init.rb
149
+ - rails_generators/topicmaps_generator.rb
150
+ - rails_generators/templates/topicmaps.rake
151
+ - tasks/topicmaps_tasks.rake
152
+ - rails_generators/templates/README.txt
153
+ - rails_generators/templates/topicmaps.example.yml
154
+ - rails_generators/templates/topicmaps.yml
155
+ - LICENSE
156
+ - DISCLAIMER
157
+ - README
158
+ has_rdoc: true
159
+ homepage: http://rtm.topicmapslab.de/
160
+ licenses: []
161
+
162
+ post_install_message:
163
+ rdoc_options: []
164
+
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ segments:
179
+ - 0
180
+ version: "0"
181
+ requirements: []
182
+
183
+ rubyforge_project: rtm
184
+ rubygems_version: 1.3.6
185
+ signing_key:
186
+ specification_version: 3
187
+ summary: RTM-Rails is the Rails-Adapter for Ruby Topic Maps. It allows configuring connections and topicmaps in config/topicmaps.yml.
188
+ test_files: []
189
+