Class: RTM::Rails::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rtm/rails.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Initializer

Create a new Initializer instance that references the given Configuration instance.

See Also:



83
84
85
# File 'lib/rtm/rails.rb', line 83

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



70
71
72
# File 'lib/rtm/rails.rb', line 70

def configuration
  @configuration
end

Class Method Details

.run(configuration = Configuration.new) {|configuration| ... } ⇒ Object

Yields:

See Also:



73
74
75
76
77
78
# File 'lib/rtm/rails.rb', line 73

def self.run(configuration = Configuration.new)
  yield configuration if block_given?
  initializer = new configuration
  initializer.process unless RTM::Rails.initialized?
  initializer
end

Instance Method Details

#check_systems_initializedObject

Some Topic Maps backends like the Ontopia RDBMS backend need some setup, e.g. some database tables. This method checks if these preconditions are met.



180
181
182
183
184
185
186
# File 'lib/rtm/rails.rb', line 180

def check_systems_initialized
  failed_connections = RTM.connections.reject { |identifier,con| con.check }
  unless failed_connections.empty?
    puts "Warning: The following topicmap connection(s) are not properly prepared: #{failed_connections.map{|identifier,con| identifier}.to_sentence}"
    puts "         Maybe you added them recently to your config but forgot 'rake topicmaps:migrate'?"
  end
end

#connect_all_anonymous_connectionsObject



225
226
227
228
229
230
231
# File 'lib/rtm/rails.rb', line 225

def connect_all_anonymous_connections
  topicmaps_with_anonymous_connections = configuration.topicmaps.select {|identifier, tm_config| Hash === tm_config[:connection]}
  topicmaps_with_anonymous_connections.map do |identifier, tm_config|
    connection_config = tm_config[:connection].merge(:identifier => "anon_connection_for_tm_#{identifier}".to_sym)
    connect_anonymous(connection_config)
  end
end

#connect_anonymous(con_spec) ⇒ Object



220
221
222
223
# File 'lib/rtm/rails.rb', line 220

def connect_anonymous(con_spec)
  con_spec.symbolize_keys!
  RTM.connect(con_spec)
end

#initialize_topicmapsObject

Get or create the topic maps and make them available in TM[]



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rtm/rails.rb', line 189

def initialize_topicmaps
  configuration.topicmaps.each do |identifier, tm_config|
    # Get the configured connection (or create it if it was given inline)
    con = case con_spec = tm_config[:connection]
    when nil # nothing specified => default connection
      RTM.connections[:default]
    when Symbol, String # name given => named connection
      RTM.connections[con_spec.to_sym]
    when Hash # inline config given
      c = connect_anonymous(con_spec)
      puts "Warning: The connection for topicmap '#{identifier}' is not yet prepared. Please run the topicmaps:migrate rake task." unless c.check
      c
    else
      raise "Cannot determine connection for #{con_spec.inspect}"
    end

    begin
      tm = con.create(tm_config[:locator])
      TM::Manager.register_map(identifier, tm)
    rescue NativeException => ne
      if ne.cause.is_a?(Java::NetOntopiaUtils::OntopiaRuntimeException) &&
          ne.cause.message =~ /The datatype type is unknown and the property 'net.ontopia.topicmaps.impl.rdbms.Platforms' is not set./ &&
          (dbname = con.property("net.ontopia.topicmaps.impl.rdbms.Database")) =~ /sqlite/
        puts "Warning: An Exception was thrown by the Ontopia RDBMS backend"
        puts "         This was probably because you're trying to use '#{dbname}', which is not supported by Ontopia."
      end
      raise ne
    end
  end
end

#initialize_topicmaps_systemsObject

Connect to the TopicMap engine(s) using the configured settings



169
170
171
172
173
174
175
# File 'lib/rtm/rails.rb', line 169

def initialize_topicmaps_systems
  configuration.connections.each do |identifier, conf|
    # If the connection was declared but not defined, copy over the data from default
    conf ||= configuration.connections[:default].merge(:identifier => identifier)
    RTM.connect(conf.merge(:identifier => identifier))
  end
end

#load_configurationObject

Get the database configuration from ActiveRecord, or if ActiveRecord is not loaded: load it manually.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rtm/rails.rb', line 105

def load_configuration
  # load the default topicmaps configuration file
  complete_config = configuration.topicmaps_configuration || {}

  # load the config from the default section
  default_config = complete_config["default"] || {}
  default_config.symbolize_keys!
  # load the config from the current-environment section
  environment_config = complete_config[::Rails.env] || {}
  environment_config.symbolize_keys!

  # obtain the current configuration by merging default config and the
  # current environment's config. This is a one-step deep-merge as we
  # just want to merge inside the subkeys.
  current_configuration = default_config
  environment_config.keys.each do |key|
    if current_configuration.key?(key) && current_configuration[key].is_a?(Hash)
      current_configuration[key].merge!(environment_config[key])
    else
      current_configuration[key] = environment_config[key]
    end
  end

  # set the topicmaps configuration
  configuration.topicmaps = (current_configuration[:topicmaps] || {}).symbolize_keys

  # expand shortcuts in topicmaps configuration
  configuration.topicmaps.each do |identifier, tm_config|
    # Convert the topicmap configuration to a Hash if the topicmap was
    # specified using the locator-only shortcut.
    configuration.topicmaps[identifier] = tm_config = {:locator => tm_config} unless tm_config.is_a?(Hash)

    tm_config.symbolize_keys!
  end

  # read the connections configuration
  current_connections = current_configuration[:connections] || {}
  current_connections.symbolize_keys!

  # get default connection (if specified)
  default_connection = current_configuration[:connection]

  # if no default connection was specified look for the rails one.
  default_connection ||= ::Rails::Configuration.new.database_configuration[::Rails.env]

  # use the explicitly defined default connection or the above-loaded
  current_connections[:default] ||= default_connection

  # set the connections configuration
  configuration.connections = current_connections

  # expand shortcuts in connection configuration
  configuration.connections.each do |identifier, con_config|
    con_config.symbolize_keys!
  end

  # forget default connection if no topicmap uses it and use_for_topicmaps is false
  keep_default = false
  keep_default = true if configuration.topicmaps.any? {|identifier, tm| ! tm.key?(:connection) || tm[:connection].to_s == "default"}
  keep_default = true if configuration.connections[:default][:use_for_topicmaps]
  configuration.connections.delete(:default) unless keep_default
end

#processObject

Sequentially step through all of the available initialization routines, in order (view execution order in source).



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rtm/rails.rb', line 89

def process
  RTM::Rails.configuration = configuration

  load_configuration

  initialize_topicmaps_systems

  check_systems_initialized

  initialize_topicmaps

  RTM::Rails.initialized = true
end