Class: ServantConfigDb
- Inherits:
-
Object
- Object
- ServantConfigDb
- Defined in:
- lib/belphanior/servant/servant_config_db.rb
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(json_string) ⇒ ServantConfigDb
constructor
A new instance of ServantConfigDb.
- #is_readonly(key) ⇒ Object
- #set(key, value) ⇒ Object
- #set_readonly(key) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(json_string) ⇒ ServantConfigDb
Returns a new instance of ServantConfigDb.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 11 def initialize(json_string) @config = JSON.parse(json_string) # validation: @config is a hash, keys are strings, vals are strings if (@config.class != {}.class) raise ServantConfigException, "ServantConfig error: JSON string did not evaluate to an object", caller end @config.each{|key, value| if (value.class != "".class) raise ServantConfigException, ("ServantConfig error: JSON initialization string, "+ "value for key '#{key}' is type '#{value.class}'"), caller end } @config.default "" @readonly = [] end |
Instance Method Details
#get(key) ⇒ Object
35 36 37 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 35 def get(key) @config[key] end |
#is_readonly(key) ⇒ Object
50 51 52 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 50 def is_readonly(key) @readonly.include? key end |
#set(key, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 39 def set(key, value) key = key.to_s value = value.to_s if @readonly.include? key raise ServantConfigException, "Attempted to change read-only property '#{key}'", caller end @config[key]=value end |
#set_readonly(key) ⇒ Object
54 55 56 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 54 def set_readonly(key) @readonly << key end |
#to_json ⇒ Object
31 32 33 |
# File 'lib/belphanior/servant/servant_config_db.rb', line 31 def to_json JSON.dump(@config) end |