Class: Bookie::Config
- Inherits:
-
Object
- Object
- Bookie::Config
- Defined in:
- lib/bookie/config.rb
Overview
Holds database configuration, etc. for Bookie components
Instance Attribute Summary collapse
-
#cores ⇒ Object
The number of cores on the system.
-
#database ⇒ Object
The name of the database to use.
-
#db_type ⇒ Object
The database type.
-
#excluded_users ⇒ Object
A set containing the names of users to be excluded.
-
#hostname ⇒ Object
The system’s hostname.
-
#memory ⇒ Object
The RAM (in KB) in the system.
-
#password ⇒ Object
The password for the database.
-
#port ⇒ Object
The database server’s port.
-
#server ⇒ Object
The database server’s hostname.
-
#system_type ⇒ Object
The system type.
-
#username ⇒ Object
The username for the database.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to the database specified in the configuration file.
-
#initialize(filename) ⇒ Config
constructor
Creates a new Config object using values from the provided JSON file.
-
#verify_type(value, name, type) ⇒ Object
Verifies that a field is of the correct type, raising an error if the type does not match.
Constructor Details
#initialize(filename) ⇒ Config
Creates a new Config object using values from the provided JSON file
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bookie/config.rb', line 39 def initialize(filename) file = File.open(filename) data = JSON::parse(file.read) file.close @db_type = data['Database type'] verify_type(@db_type, 'Database type', String) @server = data['Server'] verify_type(@server, 'Server', String) @port = data['Port'] verify_type(@port, 'Port', Integer) unless @port == nil @database = data['Database'] verify_type(@database, 'Database', String) @username = data['Username'] verify_type(@username, 'Username', String) @password = data['Password'] verify_type(@password, 'Password', String) excluded_users_array = data['Excluded users'] || [] verify_type(excluded_users_array, 'Excluded users', Array) @excluded_users = Set.new(excluded_users_array) @system_type = data['System type'] verify_type(@system_type, 'System type', String) @hostname = data['Hostname'] verify_type(@hostname, 'Hostname', String) @cores = data['Cores'] verify_type(@cores, 'Cores', Integer) @memory = data['Memory'] verify_type(@memory, 'Memory', Integer) end |
Instance Attribute Details
#cores ⇒ Object
The number of cores on the system
33 34 35 |
# File 'lib/bookie/config.rb', line 33 def cores @cores end |
#database ⇒ Object
The name of the database to use
21 22 23 |
# File 'lib/bookie/config.rb', line 21 def database @database end |
#db_type ⇒ Object
The database type
Corresponds to ActiveRecord database adapter name
13 14 15 |
# File 'lib/bookie/config.rb', line 13 def db_type @db_type end |
#excluded_users ⇒ Object
A set containing the names of users to be excluded
27 28 29 |
# File 'lib/bookie/config.rb', line 27 def excluded_users @excluded_users end |
#hostname ⇒ Object
The system’s hostname
31 32 33 |
# File 'lib/bookie/config.rb', line 31 def hostname @hostname end |
#memory ⇒ Object
The RAM (in KB) in the system
35 36 37 |
# File 'lib/bookie/config.rb', line 35 def memory @memory end |
#password ⇒ Object
The password for the database
25 26 27 |
# File 'lib/bookie/config.rb', line 25 def password @password end |
#port ⇒ Object
The database server’s port
If nil, use the default port.
19 20 21 |
# File 'lib/bookie/config.rb', line 19 def port @port end |
#server ⇒ Object
The database server’s hostname
15 16 17 |
# File 'lib/bookie/config.rb', line 15 def server @server end |
#system_type ⇒ Object
The system type
29 30 31 |
# File 'lib/bookie/config.rb', line 29 def system_type @system_type end |
#username ⇒ Object
The username for the database
23 24 25 |
# File 'lib/bookie/config.rb', line 23 def username @username end |
Instance Method Details
#connect ⇒ Object
Connects to the database specified in the configuration file
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bookie/config.rb', line 85 def connect() #To consider: disable colorized logging? #To consider: create config option for this? #ActiveRecord::Base.logger = Logger.new(STDERR) #ActiveRecord::Base.logger.level = Logger::WARN ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.default_timezone = :utc ActiveRecord::Base.establish_connection( :adapter => self.db_type, :database => self.database, :username => self.username, :password => self.password, :host => self.server, :port => self.port) end |
#verify_type(value, name, type) ⇒ Object
Verifies that a field is of the correct type, raising an error if the type does not match
77 78 79 80 81 82 |
# File 'lib/bookie/config.rb', line 77 def verify_type(value, name, type) if value == nil raise "Field \"#{name}\" must have a non-null value." end raise TypeError.new("Invalid data type #{value.class} for JSON field \"#{name}\": #{type} expected") unless value.class <= type end |