Class: ComplexConfig::Settings
- Inherits:
- BasicObject
- Extended by:
- Tins::ThreadLocal
- Includes:
- Kernel, Tins::AskAndSend
- Defined in:
- lib/complex_config/settings.rb
Instance Attribute Summary collapse
-
#name_prefix ⇒ Object
Returns the value of attribute name_prefix.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #attribute_names ⇒ Object
- #attribute_set?(name) ⇒ Boolean
- #attribute_values ⇒ Object
- #attributes_update(other) ⇒ Object
- #attributes_update_if_nil(other) ⇒ Object
- #deep_freeze ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize(hash = nil) ⇒ Settings
constructor
A new instance of Settings.
- #initialize_copy(orig) ⇒ Object
- #pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {}) ⇒ Object
- #pretty_print(q) ⇒ Object
- #replace_attributes(hash) ⇒ Object
- #size ⇒ Object
-
#to_h ⇒ Object
def write_config(encrypt: false, store_key: false) ::ComplexConfig::Provider.write_config( name_prefix, self, encrypt: encrypt, store_key: store_key ) self end.
- #to_json ⇒ Object
- #to_s(pair_sep: ' = ', path_sep: ?.) ⇒ Object (also: #inspect)
- #to_yaml ⇒ Object
Constructor Details
#initialize(hash = nil) ⇒ Settings
Returns a new instance of Settings.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/complex_config/settings.rb', line 43 def initialize(hash = nil) self.name_prefix = self.class.name_prefix @table = {} if hash hash.each_pair do |k, v| k = k.to_sym @table[k] = v end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *a, &b) ⇒ Object (private)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/complex_config/settings.rb', line 226 def method_missing(id, *a, &b) case when id =~ /\?\z/ begin public_send $`.to_sym, *a, &b rescue ::ComplexConfig::AttributeMissing nil end when id =~ /=\z/ @table[$`.to_sym] = a.first when value = ::ComplexConfig::Provider.apply_plugins(self, id) value else if attribute_set?(id) @table[id] else raise ::ComplexConfig::AttributeMissing, "no attribute named #{id.inspect}" end end end |
Instance Attribute Details
#name_prefix ⇒ Object
Returns the value of attribute name_prefix.
41 42 43 |
# File 'lib/complex_config/settings.rb', line 41 def name_prefix @name_prefix end |
Class Method Details
.[](*a) ⇒ Object
10 11 12 |
# File 'lib/complex_config/settings.rb', line 10 def [](*a) from_hash(*a) end |
.build(name, hash) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/complex_config/settings.rb', line 29 def build(name, hash) name.nil? or self.name_prefix = name.to_sym from_hash(hash) ensure self.name_prefix = nil end |
.from_hash(object) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/complex_config/settings.rb', line 14 def from_hash(object) case when object.respond_to?(:to_hash) result = new object.to_hash.each do |key, value| result[key] = from_hash(value) end result when object.respond_to?(:to_ary) object.to_ary.map { |a| from_hash(a) } else object end end |
Instance Method Details
#==(other) ⇒ Object
113 114 115 |
# File 'lib/complex_config/settings.rb', line 113 def ==(other) to_h == other.to_h end |
#[](name) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/complex_config/settings.rb', line 190 def [](name) if !attribute_set?(name) and value = ::ComplexConfig::Provider.apply_plugins(self, name) then value else @table[name.to_sym] end end |
#[]=(name, value) ⇒ Object
200 201 202 |
# File 'lib/complex_config/settings.rb', line 200 def []=(name, value) @table[name.to_sym] = value end |
#attribute_names ⇒ Object
64 65 66 |
# File 'lib/complex_config/settings.rb', line 64 def attribute_names @table.keys end |
#attribute_set?(name) ⇒ Boolean
60 61 62 |
# File 'lib/complex_config/settings.rb', line 60 def attribute_set?(name) @table.key?(name.to_sym) end |
#attribute_values ⇒ Object
68 69 70 |
# File 'lib/complex_config/settings.rb', line 68 def attribute_values @table.values end |
#attributes_update(other) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/complex_config/settings.rb', line 72 def attributes_update(other) unless other.is_a? self.class other = self.class.from_hash(other) end @table.update(other.table) end |
#attributes_update_if_nil(other) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/complex_config/settings.rb', line 79 def attributes_update_if_nil(other) unless other.is_a? self.class other = self.class.from_hash(other) end @table.update(other.table) do |key, oldval, newval| @table.key?(key) ? oldval : newval end end |
#deep_freeze ⇒ Object
183 184 185 186 187 188 |
# File 'lib/complex_config/settings.rb', line 183 def deep_freeze table_enumerator.each do |_, v| v.ask_and_send(:deep_freeze) || (v.freeze rescue v) end freeze end |
#each(&block) ⇒ Object
204 205 206 |
# File 'lib/complex_config/settings.rb', line 204 def each(&block) table_enumerator.each(&block) end |
#empty? ⇒ Boolean
129 130 131 |
# File 'lib/complex_config/settings.rb', line 129 def empty? size == 0 end |
#freeze ⇒ Object
178 179 180 181 |
# File 'lib/complex_config/settings.rb', line 178 def freeze @table.freeze super end |
#initialize_copy(orig) ⇒ Object
54 55 56 57 58 |
# File 'lib/complex_config/settings.rb', line 54 def initialize_copy(orig) super @table = @table.dup self end |
#pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {}) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/complex_config/settings.rb', line 140 def pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {}) hash.each do |key, value| path = prefix.empty? ? key.to_s : "#{prefix}#{path_sep}#{key}" case value when ::ComplexConfig::Settings pathes( value, path_sep: path_sep, prefix: path, result: result ) when ::Array value.each_with_index do |v, i| sub_path = path + "[#{i}]" if ::ComplexConfig::Settings === v pathes( v, path_sep: path_sep, prefix: sub_path, result: result ) else result[sub_path] = v end end else result[path] = value end end result end |
#pretty_print(q) ⇒ Object
174 175 176 |
# File 'lib/complex_config/settings.rb', line 174 def pretty_print(q) q.text inspect end |
#replace_attributes(hash) ⇒ Object
88 89 90 91 |
# File 'lib/complex_config/settings.rb', line 88 def replace_attributes(hash) @table = self.class.from_hash(hash).table self end |
#size ⇒ Object
125 126 127 |
# File 'lib/complex_config/settings.rb', line 125 def size each.count end |
#to_h ⇒ Object
def write_config(encrypt: false, store_key: false)
::ComplexConfig::Provider.write_config(
name_prefix, self, encrypt: encrypt, store_key: store_key
)
self
end
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/complex_config/settings.rb', line 100 def to_h table_enumerator.each_with_object({}) do |(k, v), h| h[k] = if ::Array === v v.to_ary.map { |x| (x.ask_and_send(:to_h) rescue x) || x } elsif v.respond_to?(:to_h) v.ask_and_send(:to_h) rescue v else v end end end |
#to_json ⇒ Object
121 122 123 |
# File 'lib/complex_config/settings.rb', line 121 def to_json to_h.to_json end |
#to_s(pair_sep: ' = ', path_sep: ?.) ⇒ Object Also known as: inspect
133 134 135 136 137 138 |
# File 'lib/complex_config/settings.rb', line 133 def to_s(pair_sep: ' = ', path_sep: ?.) empty? and return self.class.name pathes(path_sep: path_sep).inject('') do |result, (path, value)| result + "#{path}#{pair_sep}#{value.inspect}\n" end end |
#to_yaml ⇒ Object
117 118 119 |
# File 'lib/complex_config/settings.rb', line 117 def to_yaml to_h.to_yaml end |