Class: Configgy::ConfigMap
- Inherits:
-
Object
- Object
- Configgy::ConfigMap
- Defined in:
- lib/configgy/config_map.rb
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#inherit_from ⇒ Object
Returns the value of attribute inherit_from.
-
#monitored ⇒ Object
Returns the value of attribute monitored.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #copy_to(map) ⇒ Object
- #create_nested(key) ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(root, name) ⇒ ConfigMap
constructor
A new instance of ConfigMap.
- #inspect ⇒ Object
- #interpolate(s) ⇒ Object
- #lookup_cell(key) ⇒ Object
- #merge(map) ⇒ Object
-
#recurse(key, &block) ⇒ Object
find (ConfigMap, key) after following any dot-separated names.
- #to_config_list ⇒ Object
- #to_config_string ⇒ Object
- #to_map ⇒ Object
Constructor Details
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells.
3 4 5 |
# File 'lib/configgy/config_map.rb', line 3 def cells @cells end |
#inherit_from ⇒ Object
Returns the value of attribute inherit_from.
4 5 6 |
# File 'lib/configgy/config_map.rb', line 4 def inherit_from @inherit_from end |
#monitored ⇒ Object
Returns the value of attribute monitored.
4 5 6 |
# File 'lib/configgy/config_map.rb', line 4 def monitored @monitored end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/configgy/config_map.rb', line 3 def name @name end |
#root ⇒ Object
Returns the value of attribute root.
4 5 6 |
# File 'lib/configgy/config_map.rb', line 4 def root @root end |
Instance Method Details
#==(other) ⇒ Object
79 80 81 |
# File 'lib/configgy/config_map.rb', line 79 def ==(other) other.instance_of?(self.class) and @cells == other.cells end |
#[](key) ⇒ Object
62 63 64 |
# File 'lib/configgy/config_map.rb', line 62 def [](key) lookup_cell(key.to_s) end |
#[]=(key, value) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/configgy/config_map.rb', line 51 def []=(key, value) key = key.to_s if @monitored @root.deep_set(@name, key, value) else recurse(key) do |config_map, key| config_map.cells[key] = value end end end |
#copy_to(map) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/configgy/config_map.rb', line 92 def copy_to(map) @inherit_from.copy_to(map) if @inherit_from @cells.each do |k, v| # assume everything that doesn't respond to dup is immutable. map[k] = begin v.dup rescue v end end end |
#create_nested(key) ⇒ Object
14 15 16 17 18 |
# File 'lib/configgy/config_map.rb', line 14 def create_nested(key) config_map = ConfigMap.new(@root, name == "" ? key : name + "." + key) config_map.monitored = @monitored @cells[key] = config_map end |
#delete(key) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/configgy/config_map.rb', line 66 def delete(key) key = key.to_s if @monitored @root.deep_delete(@name, key) else recurse(key) { |config_map, key| config_map.cells.delete(key) } end end |
#dup ⇒ Object
100 101 102 103 104 |
# File 'lib/configgy/config_map.rb', line 100 def dup map = ConfigMap.new(@root, @name) copy_to(map) map end |
#has_key?(key) ⇒ Boolean
75 76 77 |
# File 'lib/configgy/config_map.rb', line 75 def has_key?(key) !lookup_cell(key.to_s).nil? end |
#inspect ⇒ Object
137 138 139 |
# File 'lib/configgy/config_map.rb', line 137 def inspect "{#{@name}" + (@inherit_from ? " (inherit=#{@inherit_from.name})" : "") + ": " + @cells.keys.sort.map { |k| "#{k}=#{@cells[k].inspect}" }.join(" ") + "}" end |
#interpolate(s) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/configgy/config_map.rb', line 127 def interpolate(s) s.gsub(/(?!\\)\$\((\w[\w\d\._-]*)\)|\\\$/) do |m| if m == "\\$" "$" else ([ self, @root, ENV ].find { |config_map| config_map[$1] } || {})[$1] end end end |
#lookup_cell(key) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/configgy/config_map.rb', line 32 def lookup_cell(key) left, right = key.split(".", 2) if @cells.has_key?(left) if right if @cells[left].respond_to?(:lookup_cell) @cells[left].lookup_cell(right) else nil end else @cells[left] end elsif @inherit_from @inherit_from.lookup_cell(key) else nil end end |
#merge(map) ⇒ Object
106 107 108 109 110 |
# File 'lib/configgy/config_map.rb', line 106 def merge(map) rv = dup map.each { |k, v| rv[k] = v } rv end |
#recurse(key, &block) ⇒ Object
find (ConfigMap, key) after following any dot-separated names
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/configgy/config_map.rb', line 21 def recurse(key, &block) left, right = key.split(".", 2) if right create_nested(left) unless @cells.has_key?(left) raise ConfigException("illegal key #{key}") unless @cells[left].respond_to?(:recurse) @cells[left].recurse(right, &block) else yield [ self, key ] end end |
#to_config_list ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/configgy/config_map.rb', line 116 def to_config_list @cells.keys.sort.map do |k| v = @cells[k] if v.instance_of?(ConfigMap) [ "#{k}" + (v.inherit_from ? " (inherit=\"#{v.inherit_from.name}\")" : "") + " {", v.to_config_list.map { |s| " " + s }, "}" ].flatten else "#{k} = #{v.inspect}" end end.flatten end |
#to_config_string ⇒ Object
112 113 114 |
# File 'lib/configgy/config_map.rb', line 112 def to_config_string to_config_list.join("\n") + "\n" end |
#to_map ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/configgy/config_map.rb', line 83 def to_map rv = @cells.dup rv.each do |k, v| if v.instance_of?(ConfigMap) rv[k] = v.to_map end end end |