module AppConfig
Constants
- FORMATS
- RESTRICTED_KEYS
- VERSION
Public Class Methods
[](key)
click to toggle source
Get configuration option
# File lib/app-config/app-config.rb, line 71 def self.[](key) @@records[key.to_s] end
configuration()
click to toggle source
Returns a configuration options
# File lib/app-config/app-config.rb, line 34 def self.configuration @@options end
configure(opts={})
click to toggle source
Configure app config
# File lib/app-config/app-config.rb, line 26 def self.configure(opts={}) @@options[:model] = opts[:model] || Setting @@options[:key] = opts[:key] || 'keyname' @@options[:value] = opts[:value] || 'value' @@options[:format] = opts[:format] || 'value_format' end
empty?()
click to toggle source
Returns true if there are no settings available
# File lib/app-config/app-config.rb, line 66 def self.empty? @@records.empty? end
exist?(key)
click to toggle source
Returns true if configuration key exists
# File lib/app-config/app-config.rb, line 81 def self.exist?(key) @@records.key?(key) end
flush()
click to toggle source
Delete all settings
# File lib/app-config/app-config.rb, line 44 def self.flush @@records.clear end
keys()
click to toggle source
Returns all configuration keys
# File lib/app-config/app-config.rb, line 61 def self.keys @@records.keys end
load()
click to toggle source
Load and process application settings
# File lib/app-config/app-config.rb, line 39 def self.load @@records = fetch end
method_missing(method, *args)
click to toggle source
Get configuration option by attribute
# File lib/app-config/app-config.rb, line 76 def self.method_missing(method, *args) @@records[method.to_s] end
reload()
click to toggle source
Safe method to reload new settings
# File lib/app-config/app-config.rb, line 49 def self.reload records = fetch rescue nil @@records = records || {} end
set_key(keyname, value, format='string')
click to toggle source
Manually set (or add) a key
# File lib/app-config/app-config.rb, line 55 def self.set_key(keyname, value, format='string') raise InvalidKeyName, "Invalid key name: #{keyname}" if RESTRICTED_KEYS.include?(keyname) @@records[keyname] = process(value, format) end
source_model()
click to toggle source
Returns class that defined as source
# File lib/app-config/app-config.rb, line 86 def self.source_model @@options[:model] end
Protected Class Methods
check_structure()
click to toggle source
Checks the column structure of the source model
# File lib/app-config/app-config.rb, line 93 def self.check_structure klass = @@options[:model] keys = [@@options[:key], @@options[:value], @@options[:format]] return (keys - klass.column_names).empty? end
fetch()
click to toggle source
Fetch data from model
# File lib/app-config/app-config.rb, line 100 def self.fetch raise InvalidSource, 'Model is not defined!' if @@options[:model].nil? raise InvalidSource, 'Model was not found!' unless @@options[:model].superclass == ActiveRecord::Base raise InvalidSource, 'Model fields are invalid!' unless check_structure records = {} begin @@options[:model].send(:all).map do |c| records[c.send(@@options[:key].to_sym)] = process( c.send(@@options[:value].to_sym), c.send(@@options[:format].to_sym) ) end records rescue ActiveRecord::StatementInvalid => ex raise InvalidSource, ex.message end end