class FReCon::Environment

Public: A class to represent the operational constraints for the FReCon instance.

Attributes

console[RW]

Public: The configuration Hash for the console-related configuration.

database[RW]

Public: The configuration Hash for the database-related configuration.

Keys will typically include 'mongoid', which should be a Hash representation of a valid mongoid.yml file.

server[RW]

Public: The configuration Hash for the server-related configuration.

Keys will typically include 'port', 'host', etc.

variable[R]

Public: Get the configuration variable.

Returns the value of @variable.

Public Class Methods

new(symbol, server: {}, console: {}, database: {}) click to toggle source

Public: Initialize an Environment.

# File lib/frecon/base/environment.rb, line 33
def initialize(symbol, server: {}, console: {}, database: {})
        @variable = symbol if validate_symbol(symbol)

        read_configurations

        @server = @server.merge(server)
        @console = @console.merge(console)
        @database = @database.merge(database)

        @server = @server.merge(server_defaults)
        @console = @console.merge(console_defaults)
        @database = @database.merge(database_defaults)
end

Public Instance Methods

default_configuration() click to toggle source

Public: Read and parse the defaults configuration file.

# File lib/frecon/base/environment.rb, line 94
def default_configuration
        read_configuration(default_configuration_filename)
end
read_configuration(filename) click to toggle source

Public: Read a configuration from a given filename.

Uses YAML to parse the given filename.

filename - String containing the path to a file.

Returns a Hash containing the parsed data from the given file.

# File lib/frecon/base/environment.rb, line 86
def read_configuration(filename)
        YAML.load_file(filename) if (filename &&
                                     File.exist?(filename) &&
                                     File.readable?(filename))
end
read_configurations() click to toggle source

Public: Read the various configurations on a system.

Reads, then merges, the configurations present on a system. Then, splices out the server, console, and database configurations and assigns them.

If a configuration cannot be found, a value of {} is used for the merging, and it is considered to be simply noneffectual. Defaults should always be specified in the default configuration file.

Returns the merged configuration.

# File lib/frecon/base/environment.rb, line 57
def read_configurations
        # Read the configurations
        default = default_configuration
        system = system_configuration
        user = user_configuration

        # Create a configuration, initialize it to the default configuration.
        #
        # Then, merge with the system configuration, then the user configuration.
        configuration = default || {}
        configuration.merge(system || {})
        configuration.merge(user || {})

        # Grab out the 'server', 'console', and 'database' values from the
        # configuration and store those in the appropriate instance variables.
        @server = configuration['server'] || {}
        @console = configuration['console'] || {}
        @database = configuration['database'] || {}

        configuration
end
system_configuration() click to toggle source

Public: Read and parse the system configuration file.

# File lib/frecon/base/environment.rb, line 99
def system_configuration
        read_configuration(system_configuration_filename)
end
user_configuration() click to toggle source

Public: Read and parse the user configuration file.

# File lib/frecon/base/environment.rb, line 104
def user_configuration
        read_configuration(user_configuration_filename)
end
variable=(symbol) click to toggle source

Public: Validate, then set the configuration variable.

# File lib/frecon/base/environment.rb, line 28
def variable=(symbol)
        @variable = symbol if validate_symbol(symbol)
end

Protected Instance Methods

console_defaults() click to toggle source

Public: Return a Hash representing the default console settings.

# File lib/frecon/base/environment.rb, line 161
def console_defaults
        {}
end
database_defaults() click to toggle source

Public: Return a Hash representing the default database settings.

# File lib/frecon/base/environment.rb, line 166
def database_defaults
        {}
end
default_configuration_filename() click to toggle source

Public: Generate the filename for the defaults configuration file.

# File lib/frecon/base/environment.rb, line 111
def default_configuration_filename
        File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'default.yml'))
end
server_defaults() click to toggle source

Public: Return a Hash representing the default server settings.

# File lib/frecon/base/environment.rb, line 156
def server_defaults
        {'host' => 'localhost', 'port' => 4567}
end
system_configuration_filename() click to toggle source

Public: Generate the filename for the system configuration file.

# File lib/frecon/base/environment.rb, line 116
def system_configuration_filename
        directories = (ENV['XDG_CONFIG_DIRS'] || '').split(':') ||
                [File.join('', 'usr', 'share'),
                 File.join('', 'usr', 'local', 'share')]

        file = nil

        directories.each do |directory|
                check_file = File.join(directory, 'frecon', 'config.yml')
                file = check_file if File.exist?(check_file)
        end

        file
end
user_configuration_filename() click to toggle source

Public: Generate the filename for the user configuration file.

# File lib/frecon/base/environment.rb, line 132
def user_configuration_filename
        configuration_home = ENV['XDG_CONFIG_HOME'] || File.join(Dir.home, '.config')

        if File.exist?(file = File.join(configuration_home, 'frecon.yml'))
                file
        end
end
valid_environments() click to toggle source

Public: Produce a list of valid environments.

# File lib/frecon/base/environment.rb, line 151
def valid_environments
        [:development, :production, :test]
end
validate_symbol(symbol) click to toggle source

Public: Validate a value for @variable.

Checks the value for @variable against a list of valid environments.

# File lib/frecon/base/environment.rb, line 143
def validate_symbol(symbol)
        raise ArgumentError, "Environment variable is not one of #{self.valid_environments}" unless
                valid_environments.include?(symbol)

        true
end