Class: Guacamole::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/guacamole/configuration.rb

Overview

Current configuration

You can receive the configuration by calling Guacamole.configuration

Defined Under Namespace

Classes: ConfigStruct

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_environmentString (readonly)

The current environment.

If you're in a Rails application this will return the Rails environment. If Rails is not available it will use RACK_ENV and if that is not available it will fall back to GUACAMOLE_ENV. This allows you to use Guacamole not only in Rails.

Returns:

  • (String)

    The current environment



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/guacamole/configuration.rb', line 71

class Configuration
  # A wrapper object to handle both configuration from a connection URI and a hash.
  class ConfigStruct
    attr_reader :url, :username, :password, :database, :graph

    def initialize(config_hash_or_url)
      case config_hash_or_url
      when Hash
        init_from_hash(config_hash_or_url)
      when String
        init_from_uri_string(config_hash_or_url)
      end
    end

    private

    def init_from_uri_string(uri_string)
      uri       = URI.parse(uri_string)
      @username = uri.user
      @password = uri.password
      uri.user  = nil
      uri.path.match(%r{/_db/(?<db_name>\w+)/?}) { |match| @database = match[:db_name] }

      @url      = "#{uri.scheme}://#{uri.hostname}:#{uri.port}"
    end

    def init_from_hash(hash)
      @username = hash['username']
      @password = hash['password']
      @database = hash['database']
      @graph    = hash['graph']
      @url      = "#{hash['protocol']}://#{hash['host']}:#{hash['port']}"
    end
  end

  # @!visibility protected
  attr_accessor :database, :default_mapper, :logger

  class << self
    extend Forwardable

    def_delegators :configuration,
                   :database, :database=,
                   :default_mapper=,
                   :logger=

    def default_mapper
      configuration.default_mapper || (self.default_mapper = Guacamole::DocumentModelMapper)
    end

    def logger
      configuration.logger ||= (rails_logger || default_logger)
    end

    def shared_path
      Pathname.new(File.join(__dir__, '..', '..', 'shared'))
    end

    # Returns the graph associated with this Guacamole application.
    #
    # You can create more graphs by interacting with the database instance directly. This is just the main graph
    # that will be used to realize relations between models. The default name will be generated based on some
    # environment information. To set a custom name use the `graph_name` option when configure your connection.
    #
    # @return [Graph] The graph to be used internally to handle relations
    def graph
      database.graph(graph_name)
    end

    # Sets a custom name for the internally used graph
    #
    # @param [String] graph_name The name of the graph to be used
    def graph_name=(graph_name)
      @graph_name = graph_name
    end

    # The name of the graph to be used internally.
    #
    # Determining the name of the graph will go through the following steps:
    #
    # 1. Use the manually set `graph_name`
    # 2. Use the ENV variable `GUACAMOLE_GRAPH` if present.
    #    This is useful if you configure the application with ENV variables.
    # 3. If a Rails context was found it will use the name of the application with a `_graph` suffix
    # 4. If none of the above matched it will use the database name with a `_graph` suffix
    #
    # @return [String] The name of the graph to be used
    def graph_name
      return @graph_name if @graph_name
      return ENV['GUACAMOLE_GRAPH'] if ENV['GUACAMOLE_GRAPH']

      base_name = if Module.const_defined?('Rails')
                    Rails.application.class.name.deconstantize.underscore
                  else
                    database.name
                  end

      [base_name, 'graph'].join('_')
    end

    # Load a YAML configuration file to configure Guacamole
    #
    # @param [String] file_name The file name of the configuration
    def load(file_name)
      yaml_content    = process_file_with_erb(file_name)
      config          = build_config(YAML.load(yaml_content)[current_environment.to_s])
      self.graph_name = config.graph

      create_database_connection(config)
      warn_if_database_was_not_yet_created
    end

    # Configures the database connection with a connection URI
    #
    # @params [String] connection_uri A URI to describe the database connection
    def configure_with_uri(connection_uri)
      create_database_connection build_config(connection_uri)
    end

    # Creates a config struct from either a hash or a DATABASE_URL
    #
    # @param [Hash, String] config Either a hash containing config params or a complete connection URI
    # @return [ConfigStruct] A simple object with the required connection parameters
    # @api private
    def build_config(config)
      ConfigStruct.new config
    end

    # Creates the actual Ashikawa::Core::Database instance
    #
    # @param [ConfigStruct] config The config object to extract the config parameters from
    # @return [Ashikawa::Core::Database] The configured database instance
    # @api private
    def create_database_connection(config)
      self.database = Ashikawa::Core::Database.new do |arango_config|
        arango_config.url           = config.url
        arango_config.username      = config.username
        arango_config.password      = config.password
        arango_config.database_name = config.database if config.database
        arango_config.logger        = logger
      end
    end

    # The current environment.
    #
    # If you're in a Rails application this will return the Rails environment. If Rails is
    # not available it will use `RACK_ENV` and if that is not available it will fall back to
    # `GUACAMOLE_ENV`. This allows you to use Guacamole not only in Rails.
    #
    # @return [String] The current environment
    def current_environment
      return Rails.env if defined?(Rails)
      ENV['RACK_ENV'] || ENV['GUACAMOLE_ENV']
    end

    private

    def configuration
      @configuration ||= new
    end

    def rails_logger
      return Rails.logger if defined?(Rails)
    end

    def default_logger
      default_logger       = Logger.new(STDOUT)
      default_logger.level = Logger::INFO
      default_logger
    end

    # Prints a warning to STDOUT and the logger if the configured database could not be found
    #
    # @note Ashikawa::Core doesn't know if the database is not present or the collection was not created.
    #       Thus we will just give the user a warning if the database was not found upon initialization.
    def warn_if_database_was_not_yet_created
      database.send_request 'version' # The /version is database specific
    rescue Ashikawa::Core::ResourceNotFound
      warning_msg = "[WARNING] The configured database ('#{database.name}') cannot be found. Please run `rake db:create` to create it."
      logger.warn warning_msg
      warn warning_msg
    end

    def process_file_with_erb(file_name)
      ERB.new(File.read(file_name)).result
    end
  end
end

.databaseAshikawa::Core::Database

The raw Database object

The Database implementation is part of Ashikawa::Core

Returns:

  • (Ashikawa::Core::Database)

See Also:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/guacamole/configuration.rb', line 71

class Configuration
  # A wrapper object to handle both configuration from a connection URI and a hash.
  class ConfigStruct
    attr_reader :url, :username, :password, :database, :graph

    def initialize(config_hash_or_url)
      case config_hash_or_url
      when Hash
        init_from_hash(config_hash_or_url)
      when String
        init_from_uri_string(config_hash_or_url)
      end
    end

    private

    def init_from_uri_string(uri_string)
      uri       = URI.parse(uri_string)
      @username = uri.user
      @password = uri.password
      uri.user  = nil
      uri.path.match(%r{/_db/(?<db_name>\w+)/?}) { |match| @database = match[:db_name] }

      @url      = "#{uri.scheme}://#{uri.hostname}:#{uri.port}"
    end

    def init_from_hash(hash)
      @username = hash['username']
      @password = hash['password']
      @database = hash['database']
      @graph    = hash['graph']
      @url      = "#{hash['protocol']}://#{hash['host']}:#{hash['port']}"
    end
  end

  # @!visibility protected
  attr_accessor :database, :default_mapper, :logger

  class << self
    extend Forwardable

    def_delegators :configuration,
                   :database, :database=,
                   :default_mapper=,
                   :logger=

    def default_mapper
      configuration.default_mapper || (self.default_mapper = Guacamole::DocumentModelMapper)
    end

    def logger
      configuration.logger ||= (rails_logger || default_logger)
    end

    def shared_path
      Pathname.new(File.join(__dir__, '..', '..', 'shared'))
    end

    # Returns the graph associated with this Guacamole application.
    #
    # You can create more graphs by interacting with the database instance directly. This is just the main graph
    # that will be used to realize relations between models. The default name will be generated based on some
    # environment information. To set a custom name use the `graph_name` option when configure your connection.
    #
    # @return [Graph] The graph to be used internally to handle relations
    def graph
      database.graph(graph_name)
    end

    # Sets a custom name for the internally used graph
    #
    # @param [String] graph_name The name of the graph to be used
    def graph_name=(graph_name)
      @graph_name = graph_name
    end

    # The name of the graph to be used internally.
    #
    # Determining the name of the graph will go through the following steps:
    #
    # 1. Use the manually set `graph_name`
    # 2. Use the ENV variable `GUACAMOLE_GRAPH` if present.
    #    This is useful if you configure the application with ENV variables.
    # 3. If a Rails context was found it will use the name of the application with a `_graph` suffix
    # 4. If none of the above matched it will use the database name with a `_graph` suffix
    #
    # @return [String] The name of the graph to be used
    def graph_name
      return @graph_name if @graph_name
      return ENV['GUACAMOLE_GRAPH'] if ENV['GUACAMOLE_GRAPH']

      base_name = if Module.const_defined?('Rails')
                    Rails.application.class.name.deconstantize.underscore
                  else
                    database.name
                  end

      [base_name, 'graph'].join('_')
    end

    # Load a YAML configuration file to configure Guacamole
    #
    # @param [String] file_name The file name of the configuration
    def load(file_name)
      yaml_content    = process_file_with_erb(file_name)
      config          = build_config(YAML.load(yaml_content)[current_environment.to_s])
      self.graph_name = config.graph

      create_database_connection(config)
      warn_if_database_was_not_yet_created
    end

    # Configures the database connection with a connection URI
    #
    # @params [String] connection_uri A URI to describe the database connection
    def configure_with_uri(connection_uri)
      create_database_connection build_config(connection_uri)
    end

    # Creates a config struct from either a hash or a DATABASE_URL
    #
    # @param [Hash, String] config Either a hash containing config params or a complete connection URI
    # @return [ConfigStruct] A simple object with the required connection parameters
    # @api private
    def build_config(config)
      ConfigStruct.new config
    end

    # Creates the actual Ashikawa::Core::Database instance
    #
    # @param [ConfigStruct] config The config object to extract the config parameters from
    # @return [Ashikawa::Core::Database] The configured database instance
    # @api private
    def create_database_connection(config)
      self.database = Ashikawa::Core::Database.new do |arango_config|
        arango_config.url           = config.url
        arango_config.username      = config.username
        arango_config.password      = config.password
        arango_config.database_name = config.database if config.database
        arango_config.logger        = logger
      end
    end

    # The current environment.
    #
    # If you're in a Rails application this will return the Rails environment. If Rails is
    # not available it will use `RACK_ENV` and if that is not available it will fall back to
    # `GUACAMOLE_ENV`. This allows you to use Guacamole not only in Rails.
    #
    # @return [String] The current environment
    def current_environment
      return Rails.env if defined?(Rails)
      ENV['RACK_ENV'] || ENV['GUACAMOLE_ENV']
    end

    private

    def configuration
      @configuration ||= new
    end

    def rails_logger
      return Rails.logger if defined?(Rails)
    end

    def default_logger
      default_logger       = Logger.new(STDOUT)
      default_logger.level = Logger::INFO
      default_logger
    end

    # Prints a warning to STDOUT and the logger if the configured database could not be found
    #
    # @note Ashikawa::Core doesn't know if the database is not present or the collection was not created.
    #       Thus we will just give the user a warning if the database was not found upon initialization.
    def warn_if_database_was_not_yet_created
      database.send_request 'version' # The /version is database specific
    rescue Ashikawa::Core::ResourceNotFound
      warning_msg = "[WARNING] The configured database ('#{database.name}') cannot be found. Please run `rake db:create` to create it."
      logger.warn warning_msg
      warn warning_msg
    end

    def process_file_with_erb(file_name)
      ERB.new(File.read(file_name)).result
    end
  end
end

.default_mapperClass

The Mapper class that is used by default. This defaults to DocumentModelMapper

Returns:

  • (Class)

    the default mapper class



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/guacamole/configuration.rb', line 71

class Configuration
  # A wrapper object to handle both configuration from a connection URI and a hash.
  class ConfigStruct
    attr_reader :url, :username, :password, :database, :graph

    def initialize(config_hash_or_url)
      case config_hash_or_url
      when Hash
        init_from_hash(config_hash_or_url)
      when String
        init_from_uri_string(config_hash_or_url)
      end
    end

    private

    def init_from_uri_string(uri_string)
      uri       = URI.parse(uri_string)
      @username = uri.user
      @password = uri.password
      uri.user  = nil
      uri.path.match(%r{/_db/(?<db_name>\w+)/?}) { |match| @database = match[:db_name] }

      @url      = "#{uri.scheme}://#{uri.hostname}:#{uri.port}"
    end

    def init_from_hash(hash)
      @username = hash['username']
      @password = hash['password']
      @database = hash['database']
      @graph    = hash['graph']
      @url      = "#{hash['protocol']}://#{hash['host']}:#{hash['port']}"
    end
  end

  # @!visibility protected
  attr_accessor :database, :default_mapper, :logger

  class << self
    extend Forwardable

    def_delegators :configuration,
                   :database, :database=,
                   :default_mapper=,
                   :logger=

    def default_mapper
      configuration.default_mapper || (self.default_mapper = Guacamole::DocumentModelMapper)
    end

    def logger
      configuration.logger ||= (rails_logger || default_logger)
    end

    def shared_path
      Pathname.new(File.join(__dir__, '..', '..', 'shared'))
    end

    # Returns the graph associated with this Guacamole application.
    #
    # You can create more graphs by interacting with the database instance directly. This is just the main graph
    # that will be used to realize relations between models. The default name will be generated based on some
    # environment information. To set a custom name use the `graph_name` option when configure your connection.
    #
    # @return [Graph] The graph to be used internally to handle relations
    def graph
      database.graph(graph_name)
    end

    # Sets a custom name for the internally used graph
    #
    # @param [String] graph_name The name of the graph to be used
    def graph_name=(graph_name)
      @graph_name = graph_name
    end

    # The name of the graph to be used internally.
    #
    # Determining the name of the graph will go through the following steps:
    #
    # 1. Use the manually set `graph_name`
    # 2. Use the ENV variable `GUACAMOLE_GRAPH` if present.
    #    This is useful if you configure the application with ENV variables.
    # 3. If a Rails context was found it will use the name of the application with a `_graph` suffix
    # 4. If none of the above matched it will use the database name with a `_graph` suffix
    #
    # @return [String] The name of the graph to be used
    def graph_name
      return @graph_name if @graph_name
      return ENV['GUACAMOLE_GRAPH'] if ENV['GUACAMOLE_GRAPH']

      base_name = if Module.const_defined?('Rails')
                    Rails.application.class.name.deconstantize.underscore
                  else
                    database.name
                  end

      [base_name, 'graph'].join('_')
    end

    # Load a YAML configuration file to configure Guacamole
    #
    # @param [String] file_name The file name of the configuration
    def load(file_name)
      yaml_content    = process_file_with_erb(file_name)
      config          = build_config(YAML.load(yaml_content)[current_environment.to_s])
      self.graph_name = config.graph

      create_database_connection(config)
      warn_if_database_was_not_yet_created
    end

    # Configures the database connection with a connection URI
    #
    # @params [String] connection_uri A URI to describe the database connection
    def configure_with_uri(connection_uri)
      create_database_connection build_config(connection_uri)
    end

    # Creates a config struct from either a hash or a DATABASE_URL
    #
    # @param [Hash, String] config Either a hash containing config params or a complete connection URI
    # @return [ConfigStruct] A simple object with the required connection parameters
    # @api private
    def build_config(config)
      ConfigStruct.new config
    end

    # Creates the actual Ashikawa::Core::Database instance
    #
    # @param [ConfigStruct] config The config object to extract the config parameters from
    # @return [Ashikawa::Core::Database] The configured database instance
    # @api private
    def create_database_connection(config)
      self.database = Ashikawa::Core::Database.new do |arango_config|
        arango_config.url           = config.url
        arango_config.username      = config.username
        arango_config.password      = config.password
        arango_config.database_name = config.database if config.database
        arango_config.logger        = logger
      end
    end

    # The current environment.
    #
    # If you're in a Rails application this will return the Rails environment. If Rails is
    # not available it will use `RACK_ENV` and if that is not available it will fall back to
    # `GUACAMOLE_ENV`. This allows you to use Guacamole not only in Rails.
    #
    # @return [String] The current environment
    def current_environment
      return Rails.env if defined?(Rails)
      ENV['RACK_ENV'] || ENV['GUACAMOLE_ENV']
    end

    private

    def configuration
      @configuration ||= new
    end

    def rails_logger
      return Rails.logger if defined?(Rails)
    end

    def default_logger
      default_logger       = Logger.new(STDOUT)
      default_logger.level = Logger::INFO
      default_logger
    end

    # Prints a warning to STDOUT and the logger if the configured database could not be found
    #
    # @note Ashikawa::Core doesn't know if the database is not present or the collection was not created.
    #       Thus we will just give the user a warning if the database was not found upon initialization.
    def warn_if_database_was_not_yet_created
      database.send_request 'version' # The /version is database specific
    rescue Ashikawa::Core::ResourceNotFound
      warning_msg = "[WARNING] The configured database ('#{database.name}') cannot be found. Please run `rake db:create` to create it."
      logger.warn warning_msg
      warn warning_msg
    end

    def process_file_with_erb(file_name)
      ERB.new(File.read(file_name)).result
    end
  end
end

.loggerObject

The logger

This defaults to the Rails logger

Returns:

  • (Object)

    the logger



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/guacamole/configuration.rb', line 71

class Configuration
  # A wrapper object to handle both configuration from a connection URI and a hash.
  class ConfigStruct
    attr_reader :url, :username, :password, :database, :graph

    def initialize(config_hash_or_url)
      case config_hash_or_url
      when Hash
        init_from_hash(config_hash_or_url)
      when String
        init_from_uri_string(config_hash_or_url)
      end
    end

    private

    def init_from_uri_string(uri_string)
      uri       = URI.parse(uri_string)
      @username = uri.user
      @password = uri.password
      uri.user  = nil
      uri.path.match(%r{/_db/(?<db_name>\w+)/?}) { |match| @database = match[:db_name] }

      @url      = "#{uri.scheme}://#{uri.hostname}:#{uri.port}"
    end

    def init_from_hash(hash)
      @username = hash['username']
      @password = hash['password']
      @database = hash['database']
      @graph    = hash['graph']
      @url      = "#{hash['protocol']}://#{hash['host']}:#{hash['port']}"
    end
  end

  # @!visibility protected
  attr_accessor :database, :default_mapper, :logger

  class << self
    extend Forwardable

    def_delegators :configuration,
                   :database, :database=,
                   :default_mapper=,
                   :logger=

    def default_mapper
      configuration.default_mapper || (self.default_mapper = Guacamole::DocumentModelMapper)
    end

    def logger
      configuration.logger ||= (rails_logger || default_logger)
    end

    def shared_path
      Pathname.new(File.join(__dir__, '..', '..', 'shared'))
    end

    # Returns the graph associated with this Guacamole application.
    #
    # You can create more graphs by interacting with the database instance directly. This is just the main graph
    # that will be used to realize relations between models. The default name will be generated based on some
    # environment information. To set a custom name use the `graph_name` option when configure your connection.
    #
    # @return [Graph] The graph to be used internally to handle relations
    def graph
      database.graph(graph_name)
    end

    # Sets a custom name for the internally used graph
    #
    # @param [String] graph_name The name of the graph to be used
    def graph_name=(graph_name)
      @graph_name = graph_name
    end

    # The name of the graph to be used internally.
    #
    # Determining the name of the graph will go through the following steps:
    #
    # 1. Use the manually set `graph_name`
    # 2. Use the ENV variable `GUACAMOLE_GRAPH` if present.
    #    This is useful if you configure the application with ENV variables.
    # 3. If a Rails context was found it will use the name of the application with a `_graph` suffix
    # 4. If none of the above matched it will use the database name with a `_graph` suffix
    #
    # @return [String] The name of the graph to be used
    def graph_name
      return @graph_name if @graph_name
      return ENV['GUACAMOLE_GRAPH'] if ENV['GUACAMOLE_GRAPH']

      base_name = if Module.const_defined?('Rails')
                    Rails.application.class.name.deconstantize.underscore
                  else
                    database.name
                  end

      [base_name, 'graph'].join('_')
    end

    # Load a YAML configuration file to configure Guacamole
    #
    # @param [String] file_name The file name of the configuration
    def load(file_name)
      yaml_content    = process_file_with_erb(file_name)
      config          = build_config(YAML.load(yaml_content)[current_environment.to_s])
      self.graph_name = config.graph

      create_database_connection(config)
      warn_if_database_was_not_yet_created
    end

    # Configures the database connection with a connection URI
    #
    # @params [String] connection_uri A URI to describe the database connection
    def configure_with_uri(connection_uri)
      create_database_connection build_config(connection_uri)
    end

    # Creates a config struct from either a hash or a DATABASE_URL
    #
    # @param [Hash, String] config Either a hash containing config params or a complete connection URI
    # @return [ConfigStruct] A simple object with the required connection parameters
    # @api private
    def build_config(config)
      ConfigStruct.new config
    end

    # Creates the actual Ashikawa::Core::Database instance
    #
    # @param [ConfigStruct] config The config object to extract the config parameters from
    # @return [Ashikawa::Core::Database] The configured database instance
    # @api private
    def create_database_connection(config)
      self.database = Ashikawa::Core::Database.new do |arango_config|
        arango_config.url           = config.url
        arango_config.username      = config.username
        arango_config.password      = config.password
        arango_config.database_name = config.database if config.database
        arango_config.logger        = logger
      end
    end

    # The current environment.
    #
    # If you're in a Rails application this will return the Rails environment. If Rails is
    # not available it will use `RACK_ENV` and if that is not available it will fall back to
    # `GUACAMOLE_ENV`. This allows you to use Guacamole not only in Rails.
    #
    # @return [String] The current environment
    def current_environment
      return Rails.env if defined?(Rails)
      ENV['RACK_ENV'] || ENV['GUACAMOLE_ENV']
    end

    private

    def configuration
      @configuration ||= new
    end

    def rails_logger
      return Rails.logger if defined?(Rails)
    end

    def default_logger
      default_logger       = Logger.new(STDOUT)
      default_logger.level = Logger::INFO
      default_logger
    end

    # Prints a warning to STDOUT and the logger if the configured database could not be found
    #
    # @note Ashikawa::Core doesn't know if the database is not present or the collection was not created.
    #       Thus we will just give the user a warning if the database was not found upon initialization.
    def warn_if_database_was_not_yet_created
      database.send_request 'version' # The /version is database specific
    rescue Ashikawa::Core::ResourceNotFound
      warning_msg = "[WARNING] The configured database ('#{database.name}') cannot be found. Please run `rake db:create` to create it."
      logger.warn warning_msg
      warn warning_msg
    end

    def process_file_with_erb(file_name)
      ERB.new(File.read(file_name)).result
    end
  end
end

Class Method Details

.build_config(config) ⇒ ConfigStruct

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a config struct from either a hash or a DATABASE_URL

Parameters:

  • config (Hash, String)

    Either a hash containing config params or a complete connection URI

Returns:

  • (ConfigStruct)

    A simple object with the required connection parameters



195
196
197
# File 'lib/guacamole/configuration.rb', line 195

def build_config(config)
  ConfigStruct.new config
end

.configure_with_uri(connection_uri) ⇒ Object

Configures the database connection with a connection URI



186
187
188
# File 'lib/guacamole/configuration.rb', line 186

def configure_with_uri(connection_uri)
  create_database_connection build_config(connection_uri)
end

.create_database_connection(config) ⇒ Ashikawa::Core::Database

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates the actual Ashikawa::Core::Database instance

Parameters:

  • config (ConfigStruct)

    The config object to extract the config parameters from

Returns:

  • (Ashikawa::Core::Database)

    The configured database instance



204
205
206
207
208
209
210
211
212
# File 'lib/guacamole/configuration.rb', line 204

def create_database_connection(config)
  self.database = Ashikawa::Core::Database.new do |arango_config|
    arango_config.url           = config.url
    arango_config.username      = config.username
    arango_config.password      = config.password
    arango_config.database_name = config.database if config.database
    arango_config.logger        = logger
  end
end

.graphGraph

Returns the graph associated with this Guacamole application.

You can create more graphs by interacting with the database instance directly. This is just the main graph that will be used to realize relations between models. The default name will be generated based on some environment information. To set a custom name use the graph_name option when configure your connection.

Returns:

  • (Graph)

    The graph to be used internally to handle relations



136
137
138
# File 'lib/guacamole/configuration.rb', line 136

def graph
  database.graph(graph_name)
end

.graph_nameString

The name of the graph to be used internally.

Determining the name of the graph will go through the following steps:

  1. Use the manually set graph_name
  2. Use the ENV variable GUACAMOLE_GRAPH if present. This is useful if you configure the application with ENV variables.
  3. If a Rails context was found it will use the name of the application with a _graph suffix
  4. If none of the above matched it will use the database name with a _graph suffix

Returns:

  • (String)

    The name of the graph to be used



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/guacamole/configuration.rb', line 158

def graph_name
  return @graph_name if @graph_name
  return ENV['GUACAMOLE_GRAPH'] if ENV['GUACAMOLE_GRAPH']

  base_name = if Module.const_defined?('Rails')
                Rails.application.class.name.deconstantize.underscore
              else
                database.name
              end

  [base_name, 'graph'].join('_')
end

.graph_name=(graph_name) ⇒ Object

Sets a custom name for the internally used graph

Parameters:

  • graph_name (String)

    The name of the graph to be used



143
144
145
# File 'lib/guacamole/configuration.rb', line 143

def graph_name=(graph_name)
  @graph_name = graph_name
end

.load(file_name) ⇒ Object

Load a YAML configuration file to configure Guacamole

Parameters:

  • file_name (String)

    The file name of the configuration



174
175
176
177
178
179
180
181
# File 'lib/guacamole/configuration.rb', line 174

def load(file_name)
  yaml_content    = process_file_with_erb(file_name)
  config          = build_config(YAML.load(yaml_content)[current_environment.to_s])
  self.graph_name = config.graph

  create_database_connection(config)
  warn_if_database_was_not_yet_created
end

.shared_pathObject



125
126
127
# File 'lib/guacamole/configuration.rb', line 125

def shared_path
  Pathname.new(File.join(__dir__, '..', '..', 'shared'))
end