class Guacamole::Configuration

Current configuration

You can receive the configuration by calling Guacamole.configuration

@!attribute self.database

The raw Database object

The Database implementation is part of Ashikawa::Core

@see http://rubydoc.info/gems/ashikawa-core/Ashikawa/Core/Database
@return [Ashikawa::Core::Database]

@!attribute self.default_mapper

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

@return [Class] the default mapper class

@!attribute self.logger

The logger

This defaults to the Rails logger

@return [Object] the logger

@!attribute [r] self.current_environment

The current environment Guacamole is running in

If you are running in Rails, this will return the current Rails environment

@return [Object] current environment

Attributes

database[RW]

@!visibility protected

default_mapper[RW]

@!visibility protected

logger[RW]

@!visibility protected

Public Class Methods

build_config(config) click to toggle source

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

# File lib/guacamole/configuration.rb, line 194
def build_config(config)
  ConfigStruct.new config
end
configure_with_uri(connection_uri) click to toggle source

Configures the database connection with a connection URI

@params [String] connection_uri A URI to describe the database connection

# File lib/guacamole/configuration.rb, line 185
def configure_with_uri(connection_uri)
  create_database_connection build_config(connection_uri)
end
create_database_connection(config) click to toggle source

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

# File lib/guacamole/configuration.rb, line 203
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
current_environment() click to toggle source

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

# File lib/guacamole/configuration.rb, line 220
def current_environment
  return Rails.env if defined?(Rails)
  ENV['RACK_ENV'] || ENV['GUACAMOLE_ENV']
end
default_mapper() click to toggle source
# File lib/guacamole/configuration.rb, line 116
def default_mapper
  configuration.default_mapper || (self.default_mapper = Guacamole::DocumentModelMapper)
end
graph() click to toggle source

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

# File lib/guacamole/configuration.rb, line 135
def graph
  database.graph(graph_name)
end
graph_name() click to toggle source

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

# File lib/guacamole/configuration.rb, line 157
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) click to toggle source

Sets a custom name for the internally used graph

@param [String] graph_name The name of the graph to be used

# File lib/guacamole/configuration.rb, line 142
def graph_name=(graph_name)
  @graph_name = graph_name
end
load(file_name) click to toggle source

Load a YAML configuration file to configure Guacamole

@param [String] file_name The file name of the configuration

# File lib/guacamole/configuration.rb, line 173
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
logger() click to toggle source
# File lib/guacamole/configuration.rb, line 120
def logger
  configuration.logger ||= (rails_logger || default_logger)
end
shared_path() click to toggle source
# File lib/guacamole/configuration.rb, line 124
def shared_path
  Pathname.new(File.join(__dir__, '..', '..', 'shared'))
end

Private Class Methods

configuration() click to toggle source
# File lib/guacamole/configuration.rb, line 227
def configuration
  @configuration ||= new
end
default_logger() click to toggle source
# File lib/guacamole/configuration.rb, line 235
def default_logger
  default_logger       = Logger.new(STDOUT)
  default_logger.level = Logger::INFO
  default_logger
end
process_file_with_erb(file_name) click to toggle source
# File lib/guacamole/configuration.rb, line 253
def process_file_with_erb(file_name)
  ERB.new(File.read(file_name)).result
end
rails_logger() click to toggle source
# File lib/guacamole/configuration.rb, line 231
def rails_logger
  return Rails.logger if defined?(Rails)
end
warn_if_database_was_not_yet_created() click to toggle source

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.
# File lib/guacamole/configuration.rb, line 245
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