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.1'.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

Methods included from Processor

process, process_array, process_boolean, process_hash, process_string

Class Method Details

.[](key) ⇒ Object

Get configuration option



69
70
71
# File 'lib/app-config/app-config.rb', line 69

def self.[](key)
  @@records[key.to_s]
end

.configurationObject

Returns a configuration options



32
33
34
# File 'lib/app-config/app-config.rb', line 32

def self.configuration
  @@options
end

.configure(opts = {}) ⇒ Object

Configure app config



24
25
26
27
28
29
# File 'lib/app-config/app-config.rb', line 24

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/app-config/app-config.rb', line 64

def self.empty?
  @@records.empty?
end

.exist?(key) ⇒ Boolean

Returns true if configuration key exists

Returns:

  • (Boolean)


79
80
81
# File 'lib/app-config/app-config.rb', line 79

def self.exist?(key)
  @@records.key?(key)
end

.flushObject

Delete all settings



42
43
44
# File 'lib/app-config/app-config.rb', line 42

def self.flush
  @@records.clear
end

.keysObject

Returns all configuration keys



59
60
61
# File 'lib/app-config/app-config.rb', line 59

def self.keys
  @@records.keys
end

.loadObject

Load and process application settings



37
38
39
# File 'lib/app-config/app-config.rb', line 37

def self.load
  @@records = fetch
end

.method_missing(method, *args) ⇒ Object

Get configuration option by attribute



74
75
76
# File 'lib/app-config/app-config.rb', line 74

def self.method_missing(method, *args)
  @@records[method.to_s]
end

.reloadObject

Safe method to reload new settings



47
48
49
50
# File 'lib/app-config/app-config.rb', line 47

def self.reload
  records = fetch rescue nil
  @@records = records || {}
end

.set_key(keyname, value, format = 'string') ⇒ Object

Manually set (or add) a key

Raises:



53
54
55
56
# File 'lib/app-config/app-config.rb', line 53

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_modelObject

Returns class that defined as source



84
85
86
# File 'lib/app-config/app-config.rb', line 84

def self.source_model
  @@options[:model]
end