Module: AppConfig
- Extended by:
- Processor
- Defined in:
- lib/app-config/errors.rb,
lib/app-config/version.rb,
lib/app-config/processor.rb,
lib/app-config/app-config.rb
Defined Under Namespace
Modules: Processor Classes: InvalidKeyName, InvalidSource, InvalidType, UndefinedKey
Constant Summary collapse
- VERSION =
'0.1.2'.freeze
- FORMATS =
['string', 'array', 'hash', 'boolean'].freeze
- RESTRICTED_KEYS =
[ 'id', 'to_s', 'configure', 'load', 'flush', 'reload', 'keys', 'empty?', 'method_missing', 'exist?', 'source_model', 'configuration' ].freeze
- @@options =
{}
- @@records =
{}
Class Method Summary collapse
-
.[](key) ⇒ Object
Get configuration option.
-
.configuration ⇒ Object
Returns a configuration options.
-
.configure(opts = {}) ⇒ Object
Configure app config.
-
.empty? ⇒ Boolean
Returns true if there are no settings available.
-
.exist?(key) ⇒ Boolean
Returns true if configuration key exists.
-
.flush ⇒ Object
Delete all settings.
-
.keys ⇒ Object
Returns all configuration keys.
-
.load ⇒ Object
Load and process application settings.
-
.method_missing(method, *args) ⇒ Object
Get configuration option by attribute.
-
.reload ⇒ Object
Safe method to reload new settings.
-
.set_key(keyname, value, format = 'string') ⇒ Object
Manually set (or add) a key.
-
.source_model ⇒ Object
Returns class that defined as source.
Methods included from Processor
process, process_array, process_boolean, process_hash, process_string
Class Method Details
.[](key) ⇒ Object
Get configuration option
71 72 73 |
# File 'lib/app-config/app-config.rb', line 71 def self.[](key) @@records[key.to_s] end |
.configuration ⇒ Object
Returns a configuration options
34 35 36 |
# File 'lib/app-config/app-config.rb', line 34 def self.configuration @@options end |
.configure(opts = {}) ⇒ Object
Configure app config
26 27 28 29 30 31 |
# 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? ⇒ Boolean
Returns true if there are no settings available
66 67 68 |
# File 'lib/app-config/app-config.rb', line 66 def self.empty? @@records.empty? end |
.exist?(key) ⇒ Boolean
Returns true if configuration key exists
81 82 83 |
# File 'lib/app-config/app-config.rb', line 81 def self.exist?(key) @@records.key?(key) end |
.flush ⇒ Object
Delete all settings
44 45 46 |
# File 'lib/app-config/app-config.rb', line 44 def self.flush @@records.clear end |
.keys ⇒ Object
Returns all configuration keys
61 62 63 |
# File 'lib/app-config/app-config.rb', line 61 def self.keys @@records.keys end |
.load ⇒ Object
Load and process application settings
39 40 41 |
# File 'lib/app-config/app-config.rb', line 39 def self.load @@records = fetch end |
.method_missing(method, *args) ⇒ Object
Get configuration option by attribute
76 77 78 |
# File 'lib/app-config/app-config.rb', line 76 def self.method_missing(method, *args) @@records[method.to_s] end |
.reload ⇒ Object
Safe method to reload new settings
49 50 51 52 |
# 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') ⇒ Object
Manually set (or add) a key
55 56 57 58 |
# 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 ⇒ Object
Returns class that defined as source
86 87 88 |
# File 'lib/app-config/app-config.rb', line 86 def self.source_model @@options[:model] end |