Class: FReCon::Environment

Inherits:
Object show all
Defined in:
lib/frecon/base/environment.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, server: {}, console: {}, database: {}) ⇒ Environment

Public: Initialize an Environment.



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#consoleObject

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



14
15
16
# File 'lib/frecon/base/environment.rb', line 14

def console
  @console
end

#databaseObject

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.



20
21
22
# File 'lib/frecon/base/environment.rb', line 20

def database
  @database
end

#serverObject

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

Keys will typically include ‘port’, ‘host’, etc.



11
12
13
# File 'lib/frecon/base/environment.rb', line 11

def server
  @server
end

#variableObject

Public: Get the configuration variable.

Returns the value of @variable.



25
26
27
# File 'lib/frecon/base/environment.rb', line 25

def variable
  @variable
end

Instance Method Details

#default_configurationObject

Public: Read and parse the defaults configuration file.



94
95
96
# File 'lib/frecon/base/environment.rb', line 94

def default_configuration
	read_configuration(default_configuration_filename)
end

#read_configuration(filename) ⇒ Object

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.



86
87
88
89
90
# 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_configurationsObject

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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_configurationObject

Public: Read and parse the system configuration file.



99
100
101
# File 'lib/frecon/base/environment.rb', line 99

def system_configuration
	read_configuration(system_configuration_filename)
end

#user_configurationObject

Public: Read and parse the user configuration file.



104
105
106
# File 'lib/frecon/base/environment.rb', line 104

def user_configuration
	read_configuration(user_configuration_filename)
end