frecon 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/frecon +9 -36
- data/config/default.yml +24 -27
- data/lib/frecon/base/bson.rb +4 -0
- data/lib/frecon/base/environment.rb +173 -0
- data/lib/frecon/base/variables.rb +5 -16
- data/lib/frecon/base.rb +1 -0
- data/lib/frecon/console.rb +2 -7
- data/lib/frecon/database.rb +5 -18
- data/lib/frecon/server.rb +10 -24
- data/lib/frecon.rb +0 -2
- metadata +3 -4
- data/lib/frecon/configuration.rb +0 -91
- data/lib/frecon/configuration_file.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c15dc8d788fbf1dad75cfa15fd8b55d65001061
|
4
|
+
data.tar.gz: f4990a2a88b29a723cb4ea4a004df2e88ab212ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b38dbdbab7c04883d046ba31318f83b8bf29d3e963e5b91b13bd6bba64570f54cfe3709c93b74e360df89d8075eba44d429d660fcb784b5e36bc314a91466769
|
7
|
+
data.tar.gz: 288a1aee3ac64c651f629d56c363a35f46b09390b5d0fbeaee5a9848f9c45ef94329923b79f87b1c14bbf1521fac9265805ea0f9e45c1ec107822e56e1d2d862
|
data/bin/frecon
CHANGED
@@ -19,11 +19,9 @@ require "optparse"
|
|
19
19
|
|
20
20
|
options = {
|
21
21
|
mode: :server,
|
22
|
-
|
22
|
+
environment: FReCon::ENVIRONMENT
|
23
23
|
}
|
24
24
|
|
25
|
-
defaults = FReCon::ConfigurationFile.default.read
|
26
|
-
|
27
25
|
optparse = OptionParser.new do |opts|
|
28
26
|
opts.banner = <<EOF
|
29
27
|
Usage: #{$0} [OPTIONS] [MODE]
|
@@ -36,44 +34,21 @@ EOF
|
|
36
34
|
opts.separator ""
|
37
35
|
opts.separator "Server OPTIONS:"
|
38
36
|
|
39
|
-
opts.on("-o", "--host HOST", "Bind to HOST (default: #{
|
40
|
-
options[:
|
41
|
-
options[:configuration]["frecon"] ||= {}
|
42
|
-
options[:configuration]["frecon"]["server"] ||= {}
|
43
|
-
options[:configuration]["frecon"]["server"]["host"] = host
|
37
|
+
opts.on("-o", "--host HOST", "Bind to HOST (default: #{options[:environment].server["host"]})") do |host|
|
38
|
+
options[:environment].server["host"] = host
|
44
39
|
end
|
45
40
|
|
46
|
-
opts.on("-p", "--port PORT", "Bind to port PORT (default: #{
|
47
|
-
options[:
|
48
|
-
options[:configuration]["frecon"] ||= {}
|
49
|
-
options[:configuration]["frecon"]["server"] ||= {}
|
50
|
-
options[:configuration]["frecon"]["server"]["port"] = port
|
41
|
+
opts.on("-p", "--port PORT", "Bind to port PORT (default: #{options[:environment].server["port"]})") do |port|
|
42
|
+
options[:environment].server["port"] = port
|
51
43
|
end
|
52
44
|
|
53
45
|
opts.separator ""
|
54
46
|
opts.separator "General OPTIONS:"
|
55
47
|
|
56
|
-
|
57
|
-
|
58
|
-
default_environment_string = server_environment == console_environment ? server_environment : "server: #{server_environment}, console: #{console_environment}"
|
59
|
-
|
60
|
-
opts.on("-E", "--env ENVIRONMENT", "Run in the ENVIRONMENT environment (default: #{default_environment_string})") do |environment|
|
61
|
-
options[:configuration] ||= {}
|
62
|
-
options[:configuration]["frecon"] ||= {}
|
63
|
-
options[:configuration]["frecon"]["server"] ||= {}
|
64
|
-
options[:configuration]["frecon"]["server"]["environment"] = environment
|
65
|
-
options[:configuration]["frecon"]["console"] ||= {}
|
66
|
-
options[:configuration]["frecon"]["console"]["environment"] = environment
|
48
|
+
opts.on("-E", "--env ENVIRONMENT", "Run in the ENVIRONMENT environment (default: #{options[:environment].variable})") do |environment|
|
49
|
+
options[:environment].variable = environment.to_sym
|
67
50
|
end
|
68
51
|
|
69
|
-
# opts.on("-v", "--verbose", "Run more verbosely.") do
|
70
|
-
# options[:output_level] = :verbose
|
71
|
-
# end
|
72
|
-
|
73
|
-
# opts.on("-d", "--debug", "Print debugging messages.") do
|
74
|
-
# options[:output_level] = :debug
|
75
|
-
# end
|
76
|
-
|
77
52
|
opts.on("-h", "--help", "Print this usage message.") do
|
78
53
|
puts opts
|
79
54
|
exit
|
@@ -89,11 +64,9 @@ ARGV.select do |arg|
|
|
89
64
|
end
|
90
65
|
end
|
91
66
|
|
92
|
-
configuration = FReCon::Configuration.construct!(argument_configuration: options[:configuration])
|
93
|
-
|
94
67
|
case options[:mode]
|
95
68
|
when :server
|
96
|
-
FReCon::Server.start
|
69
|
+
FReCon::Server.start
|
97
70
|
when :console
|
98
|
-
FReCon::Console.start
|
71
|
+
FReCon::Console.start
|
99
72
|
end
|
data/config/default.yml
CHANGED
@@ -1,32 +1,29 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
port: 4567
|
5
|
-
environment: "development"
|
1
|
+
server:
|
2
|
+
host: "localhost"
|
3
|
+
port: 5080
|
6
4
|
|
7
|
-
|
8
|
-
environment: "development"
|
5
|
+
console:
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
database:
|
8
|
+
mongoid:
|
9
|
+
development:
|
10
|
+
sessions:
|
11
|
+
default:
|
12
|
+
database: "frecon"
|
13
|
+
hosts:
|
14
|
+
- "localhost:27017"
|
18
15
|
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
options:
|
17
|
+
use_utc: true
|
18
|
+
raise_not_found_error: false
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
production:
|
21
|
+
sessions:
|
22
|
+
default:
|
23
|
+
database: "frecon"
|
24
|
+
hosts:
|
25
|
+
- "localhost:27017"
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
options:
|
28
|
+
use_utc: true
|
29
|
+
raise_not_found_error: false
|
data/lib/frecon/base/bson.rb
CHANGED
@@ -9,9 +9,11 @@
|
|
9
9
|
|
10
10
|
# Public: An extension for the BSON module.
|
11
11
|
module BSON
|
12
|
+
|
12
13
|
# Public: A monkey-patch for the BSON::ObjectId class which introduces an
|
13
14
|
# #as_json method.
|
14
15
|
class ObjectId
|
16
|
+
|
15
17
|
# Public: Get produce a JSON representation of this ObjectId.
|
16
18
|
#
|
17
19
|
# Since we don't want to produce a JSON Object for every ID, this method
|
@@ -21,5 +23,7 @@ module BSON
|
|
21
23
|
def as_json(*args)
|
22
24
|
to_s
|
23
25
|
end
|
26
|
+
|
24
27
|
end
|
28
|
+
|
25
29
|
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module FReCon
|
4
|
+
|
5
|
+
# Public: A class to represent the operational constraints for the FReCon
|
6
|
+
# instance.
|
7
|
+
class Environment
|
8
|
+
|
9
|
+
# Public: The configuration Hash for the server-related configuration.
|
10
|
+
#
|
11
|
+
# Keys will typically include "port", "host", etc.
|
12
|
+
attr_accessor :server
|
13
|
+
|
14
|
+
# Public: The configuration Hash for the console-related configuration.
|
15
|
+
attr_accessor :console
|
16
|
+
|
17
|
+
# Public: The configuration Hash for the database-related configuration.
|
18
|
+
#
|
19
|
+
# Keys will typically include "mongoid", which should be a Hash
|
20
|
+
# representation of a valid mongoid.yml file.
|
21
|
+
attr_accessor :database
|
22
|
+
|
23
|
+
# Public: Get the configuration variable.
|
24
|
+
#
|
25
|
+
# Returns the value of @variable.
|
26
|
+
attr_reader :variable
|
27
|
+
|
28
|
+
# Public: Validate, then set the configuration variable.
|
29
|
+
def variable=(symbol)
|
30
|
+
@variable = symbol if validate_symbol(symbol)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: Initialize an Environment.
|
34
|
+
def initialize(symbol, server: {}, console: {}, database: {})
|
35
|
+
@variable = symbol if validate_symbol(symbol)
|
36
|
+
|
37
|
+
read_configurations
|
38
|
+
|
39
|
+
@server = @server.merge(server)
|
40
|
+
@console = @console.merge(console)
|
41
|
+
@database = @database.merge(database)
|
42
|
+
|
43
|
+
@server = @server.merge(server_defaults)
|
44
|
+
@console = @console.merge(console_defaults)
|
45
|
+
@database = @database.merge(database_defaults)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Public: Read the various configurations on a system.
|
49
|
+
#
|
50
|
+
# Reads, then merges, the configurations present on a system. Then, splices
|
51
|
+
# out the server, console, and database configurations and assigns them.
|
52
|
+
#
|
53
|
+
# If a configuration cannot be found, a value of {} is used for the merging,
|
54
|
+
# and it is considered to be simply noneffectual. Defaults should always be
|
55
|
+
# specified in the default configuration file.
|
56
|
+
#
|
57
|
+
# Returns the merged configuration.
|
58
|
+
def read_configurations
|
59
|
+
# Read the configurations
|
60
|
+
default = default_configuration
|
61
|
+
system = system_configuration
|
62
|
+
user = user_configuration
|
63
|
+
|
64
|
+
# Create a configuration, initialize it to the default configuration.
|
65
|
+
#
|
66
|
+
# Then, merge with the system configuration, then the user configuration.
|
67
|
+
configuration = default || {}
|
68
|
+
configuration.merge(system || {})
|
69
|
+
configuration.merge(user || {})
|
70
|
+
|
71
|
+
# Grab out the "server", "console", and "database" values from the
|
72
|
+
# configuration and store those in the appropriate instance variables.
|
73
|
+
@server = configuration["server"] || {}
|
74
|
+
@console = configuration["console"] || {}
|
75
|
+
@database = configuration["database"] || {}
|
76
|
+
|
77
|
+
configuration
|
78
|
+
end
|
79
|
+
|
80
|
+
# Public: Read a configuration from a given filename.
|
81
|
+
#
|
82
|
+
# Uses YAML to parse the given filename.
|
83
|
+
#
|
84
|
+
# filename - String containing the path to a file.
|
85
|
+
#
|
86
|
+
# Returns a Hash containing the parsed data from the given file.
|
87
|
+
def read_configuration(filename)
|
88
|
+
YAML.load_file(filename) if (filename &&
|
89
|
+
File.exist?(filename) &&
|
90
|
+
File.readable?(filename))
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
# Public: Read and parse the defaults configuration file.
|
95
|
+
def default_configuration
|
96
|
+
read_configuration(default_configuration_filename)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Public: Read and parse the system configuration file.
|
100
|
+
def system_configuration
|
101
|
+
read_configuration(system_configuration_filename)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Public: Read and parse the user configuration file.
|
105
|
+
def user_configuration
|
106
|
+
read_configuration(user_configuration_filename)
|
107
|
+
end
|
108
|
+
|
109
|
+
protected
|
110
|
+
|
111
|
+
# Public: Generate the filename for the defaults configuration file.
|
112
|
+
def default_configuration_filename
|
113
|
+
File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "config", "default.yml"))
|
114
|
+
end
|
115
|
+
|
116
|
+
# Public: Generate the filename for the system configuration file.
|
117
|
+
def system_configuration_filename
|
118
|
+
directories = (ENV['XDG_CONFIG_DIRS'] || '').split(':') ||
|
119
|
+
[File.join('', 'usr', 'share'),
|
120
|
+
File.join('', 'usr', 'local', 'share')]
|
121
|
+
|
122
|
+
file = nil
|
123
|
+
|
124
|
+
directories.each do |directory|
|
125
|
+
check_file = File.join(directory, 'frecon', 'config.yml')
|
126
|
+
file = check_file if File.exist?(check_file)
|
127
|
+
end
|
128
|
+
|
129
|
+
file
|
130
|
+
end
|
131
|
+
|
132
|
+
# Public: Generate the filename for the user configuration file.
|
133
|
+
def user_configuration_filename
|
134
|
+
configuration_home = ENV['XDG_CONFIG_HOME'] || File.join(Dir.home, '.config')
|
135
|
+
|
136
|
+
if File.exist?(file = File.join(configuration_home, 'frecon.yml'))
|
137
|
+
file
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Public: Validate a value for @variable.
|
142
|
+
#
|
143
|
+
# Checks the value for @variable against a list of valid environments.
|
144
|
+
def validate_symbol(symbol)
|
145
|
+
raise ArgumentError, "Environment variable is not one of #{self.valid_environments}" unless
|
146
|
+
valid_environments.include?(symbol)
|
147
|
+
|
148
|
+
true
|
149
|
+
end
|
150
|
+
|
151
|
+
# Public: Produce a list of valid environments.
|
152
|
+
def valid_environments
|
153
|
+
[:development, :production, :test]
|
154
|
+
end
|
155
|
+
|
156
|
+
# Public: Return a Hash representing the default server settings.
|
157
|
+
def server_defaults
|
158
|
+
{"host" => "localhost", "port" => 4567}
|
159
|
+
end
|
160
|
+
|
161
|
+
# Public: Return a Hash representing the default console settings.
|
162
|
+
def console_defaults
|
163
|
+
{}
|
164
|
+
end
|
165
|
+
|
166
|
+
# Public: Return a Hash representing the default database settings.
|
167
|
+
def database_defaults
|
168
|
+
{}
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
@@ -7,24 +7,13 @@
|
|
7
7
|
# license with this program. If not, please see
|
8
8
|
# <http://opensource.org/licenses/MIT>.
|
9
9
|
|
10
|
+
require "frecon/base/environment"
|
11
|
+
|
10
12
|
# Public: The FReCon API module.
|
11
13
|
module FReCon
|
12
14
|
# Public: A String representing the current version of FReCon.
|
13
|
-
VERSION = "1.
|
14
|
-
|
15
|
-
@environment_variable = :development
|
16
|
-
|
17
|
-
# Public: Returns the current environment.
|
18
|
-
def self.environment
|
19
|
-
@environment_variable
|
20
|
-
end
|
15
|
+
VERSION = "1.3.0"
|
21
16
|
|
22
|
-
# Public:
|
23
|
-
|
24
|
-
# arg - The new environment.
|
25
|
-
#
|
26
|
-
# Returns the result from setting the current environment.
|
27
|
-
def self.environment=(arg)
|
28
|
-
@environment_variable = arg
|
29
|
-
end
|
17
|
+
# Public: An Environment representing the system execution environment.
|
18
|
+
ENVIRONMENT = Environment.new(:development)
|
30
19
|
end
|
data/lib/frecon/base.rb
CHANGED
data/lib/frecon/console.rb
CHANGED
@@ -8,7 +8,6 @@
|
|
8
8
|
# <http://opensource.org/licenses/MIT>.
|
9
9
|
|
10
10
|
require "frecon/base/variables"
|
11
|
-
require "frecon/configuration"
|
12
11
|
require "frecon/database"
|
13
12
|
require "frecon/server"
|
14
13
|
|
@@ -17,13 +16,9 @@ module FReCon
|
|
17
16
|
class Console
|
18
17
|
# Public: Starts the FReCon console.
|
19
18
|
#
|
20
|
-
# :configuration - The Configuration to use when starting the console.
|
21
|
-
#
|
22
19
|
# Returns the result of running pry on FReCon.
|
23
|
-
def self.start
|
24
|
-
|
25
|
-
mongoid = configuration["frecon"]["database"]["mongoid"]
|
26
|
-
Database.setup(environment, mongoid)
|
20
|
+
def self.start
|
21
|
+
Database.setup!
|
27
22
|
|
28
23
|
require "pry"
|
29
24
|
|
data/lib/frecon/database.rb
CHANGED
@@ -9,6 +9,8 @@
|
|
9
9
|
|
10
10
|
require "logger"
|
11
11
|
|
12
|
+
require "frecon/base/variables"
|
13
|
+
|
12
14
|
require "mongoid"
|
13
15
|
require "frecon/mongoid/criteria"
|
14
16
|
|
@@ -21,25 +23,10 @@ module FReCon
|
|
21
23
|
# Public: A system to set up the database.
|
22
24
|
class Database
|
23
25
|
# Public: Set up the database.
|
24
|
-
|
25
|
-
|
26
|
-
# mongoid - Hash containing the configuration for Mongoid. If not
|
27
|
-
# present, the lib/frecon/mongoid.yml file is given to
|
28
|
-
# Mongoid.load!. If present, the Hash is dumped to a
|
29
|
-
# tempfile which is given to Mongoid.load!.
|
30
|
-
def self.setup(environment = FReCon.environment, mongoid = nil)
|
31
|
-
if mongoid.is_a?(Hash)
|
32
|
-
mongoid_tempfile = Tempfile.new("FReCon")
|
33
|
-
|
34
|
-
mongoid_tempfile.write(mongoid.to_h.to_yaml)
|
35
|
-
mongoid_tempfile.rewind
|
36
|
-
|
37
|
-
Mongoid.load!(mongoid_tempfile.path, environment)
|
38
|
-
else
|
39
|
-
Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
|
40
|
-
end
|
26
|
+
def self.setup!
|
27
|
+
Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), FReCon::ENVIRONMENT.variable)
|
41
28
|
|
42
|
-
if
|
29
|
+
if FReCon::ENVIRONMENT.console["level"]
|
43
30
|
Mongoid.logger.level = Logger::DEBUG
|
44
31
|
Mongoid.logger = Logger.new($stdout)
|
45
32
|
|
data/lib/frecon/server.rb
CHANGED
@@ -10,7 +10,6 @@
|
|
10
10
|
require "sinatra/base"
|
11
11
|
|
12
12
|
require "frecon/base/variables"
|
13
|
-
require "frecon/configuration"
|
14
13
|
require "frecon/database"
|
15
14
|
require "frecon/routes"
|
16
15
|
require "frecon/controllers"
|
@@ -26,45 +25,32 @@ module FReCon
|
|
26
25
|
|
27
26
|
# Public: Start the Server.
|
28
27
|
#
|
29
|
-
# keyword_arguments - The Hash of arguments to use.
|
30
|
-
# :configuration - The Configuration to use when
|
31
|
-
# setting up the server.
|
32
|
-
#
|
33
28
|
# Returns the result of starting the server.
|
34
|
-
def self.start(
|
35
|
-
run!(
|
29
|
+
def self.start(*arguments)
|
30
|
+
run!(*arguments)
|
36
31
|
end
|
37
32
|
|
38
33
|
protected
|
39
34
|
|
40
35
|
# Internal: Set up the server.
|
41
36
|
#
|
42
|
-
# Sets
|
43
|
-
#
|
44
|
-
# :configuration - The Configuration to use when starting the server.
|
37
|
+
# Sets various Thin and Sinatra options, and sets up the database.
|
45
38
|
#
|
46
39
|
# Returns the result of setting up the database.
|
47
|
-
def self.setup!
|
40
|
+
def self.setup!
|
48
41
|
# Set the Thin and Sinatra options.
|
49
42
|
set :server, %w[thin HTTP webrick]
|
50
|
-
set :bind,
|
51
|
-
set :port,
|
52
|
-
set :environment,
|
53
|
-
|
54
|
-
# Grab out the mongoid configuration.
|
55
|
-
mongoid = configuration["frecon"]["database"]["mongoid"]
|
43
|
+
set :bind, FReCon::ENVIRONMENT.server["host"]
|
44
|
+
set :port, FReCon::ENVIRONMENT.server["port"]
|
45
|
+
set :environment, FReCon::ENVIRONMENT.variable.to_s
|
56
46
|
|
57
47
|
# Set up the database.
|
58
|
-
Database.setup
|
48
|
+
Database.setup!
|
59
49
|
end
|
60
50
|
|
61
51
|
# Internal: Set up the server and start it.
|
62
|
-
|
63
|
-
|
64
|
-
# :configuration - The Configuration to use when
|
65
|
-
# setting up the server.
|
66
|
-
def self.run!(**keyword_arguments)
|
67
|
-
setup!(**keyword_arguments)
|
52
|
+
def self.run!(*arguments)
|
53
|
+
setup!(*arguments)
|
68
54
|
|
69
55
|
super
|
70
56
|
end
|
data/lib/frecon.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: frecon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Craig
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2015-10-
|
16
|
+
date: 2015-10-29 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: sinatra
|
@@ -184,10 +184,9 @@ files:
|
|
184
184
|
- lib/frecon.rb
|
185
185
|
- lib/frecon/base.rb
|
186
186
|
- lib/frecon/base/bson.rb
|
187
|
+
- lib/frecon/base/environment.rb
|
187
188
|
- lib/frecon/base/object.rb
|
188
189
|
- lib/frecon/base/variables.rb
|
189
|
-
- lib/frecon/configuration.rb
|
190
|
-
- lib/frecon/configuration_file.rb
|
191
190
|
- lib/frecon/console.rb
|
192
191
|
- lib/frecon/controller.rb
|
193
192
|
- lib/frecon/controllers.rb
|
data/lib/frecon/configuration.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
# lib/frecon/configuration.rb
|
2
|
-
#
|
3
|
-
# Copyright (C) 2015 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
|
4
|
-
#
|
5
|
-
# This file is part of FReCon, an API for scouting at FRC Competitions, which is
|
6
|
-
# licensed under the MIT license. You should have received a copy of the MIT
|
7
|
-
# license with this program. If not, please see
|
8
|
-
# <http://opensource.org/licenses/MIT>.
|
9
|
-
|
10
|
-
require "frecon/configuration_file"
|
11
|
-
|
12
|
-
module FReCon
|
13
|
-
# Public: A wrapper to allow the manipulation of configurations.
|
14
|
-
class Configuration < Hash
|
15
|
-
# Public: Initialize a Configuration.
|
16
|
-
#
|
17
|
-
# data - a Hash representing the data.
|
18
|
-
def initialize(data)
|
19
|
-
data.each do |key, value|
|
20
|
-
self[key] = value
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
# Public: Convert self to a Hash.
|
25
|
-
#
|
26
|
-
# Recursively converts instances of Configuration within self
|
27
|
-
# to hashes by calling this method.
|
28
|
-
#
|
29
|
-
# Returns a Hash representing self.
|
30
|
-
def to_h
|
31
|
-
hash = {}
|
32
|
-
|
33
|
-
self.each do |key, value|
|
34
|
-
case value
|
35
|
-
when Configuration
|
36
|
-
hash[key] = value.to_h
|
37
|
-
else
|
38
|
-
hash[key] = value
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
hash
|
43
|
-
end
|
44
|
-
|
45
|
-
# Public: Merge with another Configuration.
|
46
|
-
#
|
47
|
-
# Sets all key-value pairs within Configuration to the same within self.
|
48
|
-
#
|
49
|
-
# other - A Configuration or Hash to be merged with.
|
50
|
-
def merge(other)
|
51
|
-
case other
|
52
|
-
when Configuration, Hash
|
53
|
-
other.each do |key, value|
|
54
|
-
case value
|
55
|
-
when Configuration, Hash
|
56
|
-
me = Configuration.new(self[key] || {})
|
57
|
-
me.merge(Configuration.new(value))
|
58
|
-
self[key] = me
|
59
|
-
else
|
60
|
-
self[key] = value
|
61
|
-
end
|
62
|
-
end
|
63
|
-
when nil
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# Public: Constructs a configuration.
|
68
|
-
#
|
69
|
-
# options - A Hash containing various configurations.
|
70
|
-
# :default_configuration - The default configuration's values
|
71
|
-
# :system_configuration - The system's configuration's values
|
72
|
-
# :user_configuration - The user's configuration's values
|
73
|
-
# :argument_configuration - The configuration values from command-line arguments
|
74
|
-
#
|
75
|
-
# Returns a Configuration generated by merging all of the given
|
76
|
-
# configurations together.
|
77
|
-
def self.construct!(default_configuration: ConfigurationFile.default.read,
|
78
|
-
system_configuration: ConfigurationFile.system.read,
|
79
|
-
user_configuration: ConfigurationFile.user.read,
|
80
|
-
argument_configuration: nil)
|
81
|
-
configuration_hierarchy = [default_configuration, system_configuration, user_configuration, argument_configuration]
|
82
|
-
|
83
|
-
configuration = Configuration.new({})
|
84
|
-
configuration_hierarchy.each do |other_configuration|
|
85
|
-
configuration.merge(other_configuration)
|
86
|
-
end
|
87
|
-
|
88
|
-
configuration
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# lib/frecon/configuration_file.rb
|
2
|
-
#
|
3
|
-
# Copyright (C) 2015 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
|
4
|
-
#
|
5
|
-
# This file is part of FReCon, an API for scouting at FRC Competitions, which is
|
6
|
-
# licensed under the MIT license. You should have received a copy of the MIT
|
7
|
-
# license with this program. If not, please see
|
8
|
-
# <http://opensource.org/licenses/MIT>.
|
9
|
-
|
10
|
-
require "yaml"
|
11
|
-
require "frecon/configuration"
|
12
|
-
|
13
|
-
module FReCon
|
14
|
-
# Public: A class to handle configuration files.
|
15
|
-
class ConfigurationFile
|
16
|
-
# Public: The filename for the file.
|
17
|
-
attr_accessor :filename
|
18
|
-
|
19
|
-
# Public: Initialize a ConfigurationFile.
|
20
|
-
#
|
21
|
-
# filename - The name of the file.
|
22
|
-
def initialize(filename)
|
23
|
-
@filename = filename
|
24
|
-
end
|
25
|
-
|
26
|
-
# Public: Read from the file and generate a Configuration
|
27
|
-
# from the YAML data therein.
|
28
|
-
#
|
29
|
-
# Returns a Configuration representing the file's data or nil if it didn't
|
30
|
-
# exist.
|
31
|
-
def read
|
32
|
-
begin
|
33
|
-
data = open(@filename, "rb") do |io|
|
34
|
-
io.read
|
35
|
-
end
|
36
|
-
|
37
|
-
Configuration.new(YAML.load(data))
|
38
|
-
rescue Errno::ENOENT
|
39
|
-
nil
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# Public: Create a new ConfigurationFile corresponding to the default
|
44
|
-
# defaults configuration location.
|
45
|
-
def self.default
|
46
|
-
self.new(File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "default.yml")))
|
47
|
-
end
|
48
|
-
|
49
|
-
# Public: Create a new ConfigurationFile corresponding to the default
|
50
|
-
# system configuration location.
|
51
|
-
def self.system
|
52
|
-
self.new(File.join("", "etc", "frecon", "config.yml"))
|
53
|
-
end
|
54
|
-
|
55
|
-
# Public: Create a new ConfigurationFile corresponding to the default
|
56
|
-
# user configuration location.
|
57
|
-
def self.user
|
58
|
-
self.new(File.join(config_directory, "frecon.yml"))
|
59
|
-
end
|
60
|
-
|
61
|
-
protected
|
62
|
-
|
63
|
-
# Public: Returns the User's config home directory.
|
64
|
-
def self.config_directory
|
65
|
-
ENV['XDG_CONFIG_HOME'] || File.join(Dir.home, '.config')
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|