Module: Complish::Configuration

Defined in:
lib/complish/configuration.rb

Overview

TODO:

Currently the gem relies on a persisted configuration. Refactor the gem to allow for an in memory configuration.

The Complish Configuration Module

Author:

Since:

  • 0.1.0-alpha

Version:

  • 1.1

Constant Summary collapse

CONFIG_DIR =

The Complish config directory

Examples:

Return the CONFIG_DIR

Complish::Configuration::CONFIG_DIR
 #=> '/Users/<username>/.config'

Returns:

  • (String)

    the file path for the Complish config directory

Since:

  • 0.1.0-alpha

File.join(ENV['HOME'], '.config').freeze
CONFIG_FILE =

The Complish config file

Examples:

Return the CONFIG_FILE

Complish::Configuration::CONFIG_FILE
 #=> '/Users/<username>/.config/.complish'

Returns:

  • (String)

    the file path and file name of the Complish config file

Since:

  • 0.1.0-alpha

File.join(CONFIG_DIR.dup, '.complish').freeze
DEFAULT_CONFIG =

The default Complish configuration

Examples:

Return the DEFAULT_CONFIG

Complish::Configuration::DEFAULT_CONFIG
 #=> { db_location => '/Users/<username>', db_file => '.complish.db' }

Returns:

  • (Hash{Symbol=>String})

    the default configuration options

Since:

  • 0.1.0-alpha

{
  db_location: ENV['HOME'],
  db_file: '.complish.db'
}.freeze
STATIC_CONFIG_OPTS =

The static options merged into every configuration.

Examples:

Return the STATIC_CONFIG_OPTS

Complish::Configuration::STATIC_CONFIG_OPTS
 #=> { version: 0.2.0-alpha }

Returns:

  • (Hash{Symbol=>String})

    the static configuration options.

Since:

  • 0.1.0-alpha

{
  version: Complish::Version.whole
}.freeze

Class Method Summary collapse

Class Method Details

.configure(opts = {}) ⇒ Hash{Symbol=>String}

Create a configuration hash

Examples:

create a configuration hash.

Complish::Configuration.configure
 #=> {:db_location=>"/Users/<username>", :db_file=>".complish.db", :version=>"0.1.0-alpha"}

Parameters:

  • opts (Hash{Symbol=>String}) (defaults to: {})

    the options to create the configuration with

Options Hash (opts):

  • db_location (String)

    the location of the Complish database

  • db_file (String)

    the name of the Complish database file

Returns:

  • (Hash{Symbol=>String})

    the configuration hash

Since:

  • 0.1.0-alpha

Version:

  • 1.1


60
61
62
63
64
# File 'lib/complish/configuration.rb', line 60

def configure(opts = {})
  opts.select { |k, _v| DEFAULT_CONFIG.keys.include?(k) }
      .reverse_merge(DEFAULT_CONFIG)
      .merge(STATIC_CONFIG_OPTS)
end

.configure_persisted(opts = {}) ⇒ Hash{Symbol=>String}

Create a configuration hash and save it to file

Examples:

Create a persisted configuration

Complish::Configuration.configure_persisted
 #=> {:db_location=>"/Users/<username>", :db_file=>".complish.db", :version=>"0.1.0-alpha"}

Parameters:

  • opts (Hash{Symbol=>String}) (defaults to: {})

    the options to create the configuration with

Options Hash (opts):

  • db_location (String)

    the location of the Complish database

  • db_file (String)

    the name of the Complish database file

Returns:

  • (Hash{Symbol=>String})

    the persisted configuration hash

Since:

  • 0.1.0-alpha

Version:

  • 1.0


86
87
88
89
# File 'lib/complish/configuration.rb', line 86

def configure_persisted(opts = {})
  config = configure(opts)
  save!(config)
end

.exist?Boolean

Determine if a configuration exists for current environment

Examples:

Determine if configuration exists

Complish::Configuration.exist?
 #=> true

Returns:

  • (Boolean)

    result of configuration existance

Since:

  • 0.1.0-alpha

Version:

  • 1.1


97
98
99
# File 'lib/complish/configuration.rb', line 97

def exist?
  config_exists? && load_config_file.key?(current_env)
end

.from_fileHash{Symbol=>String}

Note:

This will return an empty hash if no configuration is found for current environment

Get a persisted configuration for current environment

Examples:

Get persisted configuration

Complish::configuration.from_file
 #=> {:db_location=>"/Users/<username>", :db_file=>".complish.db", :version=>"0.1.0-alpha"}

Returns:

  • (Hash{Symbol=>String})

    the configuration hash from file for current environment

Since:

  • 0.1.0-alpha

Version:

  • 1.0


73
74
75
# File 'lib/complish/configuration.rb', line 73

def from_file
  exist? ? load_config_file[current_env] : {}
end

.from_file_or_persist_defaultsHash{Symbol=>String}

Return the persisted configuration or persist a new configuration with defaults

Examples:

Get configuration or persist defaults

Complish::Configuration.from_file_or_persist_defaults
 #=> { db_location: 'foo/bar', db_file: 'foo.db', version: 0.2.0-alpha }

Returns:

  • (Hash{Symbol=>String})

    the persisted configuration hash

Since:

  • 0.2.0-alpha

Version:

  • 1.0


108
109
110
# File 'lib/complish/configuration.rb', line 108

def from_file_or_persist_defaults
  exist? ? from_file : configure_persisted
end