Class: WDI::Config::ConfigFile
- Inherits:
-
Object
- Object
- WDI::Config::ConfigFile
- Defined in:
- lib/wdi/config.rb
Instance Attribute Summary collapse
-
#pairs ⇒ Object
readonly
Returns the value of attribute pairs.
Instance Method Summary collapse
- #add_key_value(key, value) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(json_configuration) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #keys ⇒ Object
- #keys_with_prefix(prefix = nil) ⇒ Object
- #keys_with_value(value) ⇒ Object
- #set_key_value(key, value) ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #value_at(key) ⇒ Object
Constructor Details
#initialize(json_configuration) ⇒ ConfigFile
Returns a new instance of ConfigFile.
17 18 19 20 21 22 23 |
# File 'lib/wdi/config.rb', line 17 def initialize(json_configuration) config = JSON.parse(json_configuration, symbolize_names: true) @pairs = {} recursive_build_pairs_from config ensure_format_of_pairs end |
Instance Attribute Details
#pairs ⇒ Object (readonly)
Returns the value of attribute pairs.
15 16 17 |
# File 'lib/wdi/config.rb', line 15 def pairs @pairs end |
Instance Method Details
#add_key_value(key, value) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wdi/config.rb', line 64 def add_key_value(key, value) key = translate_incoming(key) disallow_bad_references_in value if has_key?(key) if @pairs[key].is_a? Array @pairs[key] << value else @pairs[key] = [@pairs[key],value] end else @pairs[key] = value end return @pairs[key] end |
#has_key?(key) ⇒ Boolean
25 26 27 28 |
# File 'lib/wdi/config.rb', line 25 def has_key?(key) key = translate_incoming(key) @pairs.key?(key) end |
#keys ⇒ Object
80 81 82 |
# File 'lib/wdi/config.rb', line 80 def keys @pairs.keys.map{|k| translate_outgoing(k)} end |
#keys_with_prefix(prefix = nil) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/wdi/config.rb', line 84 def keys_with_prefix(prefix=nil) return keys if prefix.nil? prefix = translate_outgoing(translate_incoming(prefix)) # force correct string format key_list = keys.select{|k| (k =~ Regexp.new(prefix)) == 0 } key_list.count == 0 ? false : key_list end |
#keys_with_value(value) ⇒ Object
48 49 50 51 |
# File 'lib/wdi/config.rb', line 48 def keys_with_value(value) key_list = @pairs.select {|k,v| v == value}.keys key_list.count == 0 ? false : key_list.map{|k| translate_outgoing(k)} end |
#set_key_value(key, value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/wdi/config.rb', line 53 def set_key_value(key, value) key = translate_incoming(key) raise WDI::ConfigError, "This key is not in the WDI config file." unless @pairs.key?(key) raise WDI::ConfigError, "This value is not formatted correctly for the WDI config file." unless value.is_a?(String) disallow_bad_references_in value @pairs[key] = value end |
#to_h ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/wdi/config.rb', line 91 def to_h result = {} keys.each do |key| add_child_node(result, key, @pairs[translate_incoming(key)]) end return result end |
#to_json ⇒ Object
101 102 103 |
# File 'lib/wdi/config.rb', line 101 def to_json JSON.pretty_generate(self.to_h) end |
#to_s ⇒ Object
105 106 107 |
# File 'lib/wdi/config.rb', line 105 def to_s @pairs.to_s end |
#value_at(key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wdi/config.rb', line 30 def value_at(key) key = translate_incoming(key) if @pairs.key?(key) if @pairs[key].is_a? Array return @pairs[key].map {|value| retrieve_value_with_references(value) } else return retrieve_value_with_references(@pairs[key]) end elsif keys_with_prefix(key) raise WDI::ConfigError, "This key doesn't represent a property in the WDI config file. " + "Try `wdi config keys #{key}`." else return false end end |