Class: EasySuite::Config
- Inherits:
-
Object
- Object
- EasySuite::Config
- Defined in:
- lib/easysuite/config.rb
Overview
– Config ++ Class to config easily.
Instance Attribute Summary collapse
-
#config_dir ⇒ Object
Returns the value of attribute config_dir.
Class Method Summary collapse
-
.binread(path) ⇒ Object
– binread ++ Read string from file.(binary mode).
-
.binwrite(path, str) ⇒ Object
– binwrite ++ Write string to file.(binary mode).
-
.deep_copy(obj) ⇒ Object
– deep_copy ++ Simple deep copy.
-
.dump ⇒ Object
– dump_yaml ++ Dump object to YAML file.
-
.dump_yaml(path, obj) ⇒ Object
– dump_yaml ++ Dump object to YAML file.
-
.encode(obj, &proc) ⇒ Object
– encode ++ Encode strings (in Hash and Array).
-
.keys_to_string(obj) ⇒ Object
– keys_to_string ++ Convert Hash keys into String from Symbol (in Hash and Array).
-
.keys_to_symbol(obj) ⇒ Object
– keys_to_symbol ++ Convert Hash keys into Symbol from String (in Hash and Array).
-
.load ⇒ Object
– load_yaml ++ Load object from YAML file.
-
.load_yaml(path) ⇒ Object
– load_yaml ++ Load object from YAML file.
-
.read(path) ⇒ Object
– read ++ Read string from file.
-
.set_values(obj, src, *keys) ⇒ Object
– set_values ++ Set values from Hash or YAML file.
-
.write(path, str) ⇒ Object
– write ++ Write string to file.
Instance Method Summary collapse
-
#binread(filename) ⇒ Object
– binread ++ Read string from file.(binary mode).
-
#binwrite(filename, str) ⇒ Object
– binwrite ++ Write string to file.(binary mode).
-
#dump_yaml(filename, obj) ⇒ Object
(also: #dump)
– dump_yaml ++ Dump object to YAML file.
-
#initialize(config_dir = nil) ⇒ Config
constructor
– initialize ++ [config_dir] Default directory of instance.
-
#load_yaml(filename) ⇒ Object
(also: #load)
– load_yaml ++ Load object from YAML file.
-
#read(filename) ⇒ Object
– read ++ Read string from file.
-
#set_values(obj, src, *keys) ⇒ Object
– set_values ++ Set values from Hash or YAML file.
-
#write(filename, str) ⇒ Object
– write ++ Write string to file.
Constructor Details
#initialize(config_dir = nil) ⇒ Config
– initialize ++
- config_dir
-
Default directory of instance.
172 173 174 |
# File 'lib/easysuite/config.rb', line 172 def initialize(config_dir = nil) @config_dir = config_dir end |
Instance Attribute Details
#config_dir ⇒ Object
Returns the value of attribute config_dir.
165 166 167 |
# File 'lib/easysuite/config.rb', line 165 def config_dir @config_dir end |
Class Method Details
.binread(path) ⇒ Object
– binread ++ Read string from file.(binary mode)
51 52 53 |
# File 'lib/easysuite/config.rb', line 51 def binread(path) IO.binread(path) rescue nil end |
.binwrite(path, str) ⇒ Object
– binwrite ++ Write string to file.(binary mode)
59 60 61 |
# File 'lib/easysuite/config.rb', line 59 def binwrite(path, str) IO.binwrite(path, str) end |
.deep_copy(obj) ⇒ Object
– deep_copy ++ Simple deep copy.
141 142 143 |
# File 'lib/easysuite/config.rb', line 141 def deep_copy(obj) Marshal.load(Marshal.dump(obj)) rescue nil end |
.dump ⇒ Object
– dump_yaml ++ Dump object to YAML file.
29 30 31 |
# File 'lib/easysuite/config.rb', line 29 def dump_yaml(path, obj) binwrite(path, YAML.dump(obj)) end |
.dump_yaml(path, obj) ⇒ Object
– dump_yaml ++ Dump object to YAML file.
25 26 27 |
# File 'lib/easysuite/config.rb', line 25 def dump_yaml(path, obj) binwrite(path, YAML.dump(obj)) end |
.encode(obj, &proc) ⇒ Object
– encode ++ Encode strings (in Hash and Array).
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/easysuite/config.rb', line 92 def encode(obj, &proc) proc ||= lambda {|s| s.encode } case obj when Hash {}.tap {|x| obj.each {|k, v| x[encode(k, &proc)] = encode(v, &proc) }} when Array [].tap {|x| obj.each {|v| x << encode(v, &proc) }} when String proc.call(obj) when Symbol proc.call(obj.to_s).to_sym else obj end end |
.keys_to_string(obj) ⇒ Object
– keys_to_string ++ Convert Hash keys into String from Symbol (in Hash and Array).
127 128 129 130 131 132 133 134 135 |
# File 'lib/easysuite/config.rb', line 127 def keys_to_string(obj) keys_to_something(obj) do |x, k, v| if k.kind_of?(Symbol) x[k.to_s] ||= keys_to_string(v) else x[k] = keys_to_string(v) end end end |
.keys_to_symbol(obj) ⇒ Object
– keys_to_symbol ++ Convert Hash keys into Symbol from String (in Hash and Array).
113 114 115 116 117 118 119 120 121 |
# File 'lib/easysuite/config.rb', line 113 def keys_to_symbol(obj) keys_to_something(obj) do |x, k, v| if k.kind_of?(String) x[k.to_sym] ||= keys_to_symbol(v) else x[k] = keys_to_symbol(v) end end end |
.load ⇒ Object
– load_yaml ++ Load object from YAML file.
19 20 21 22 |
# File 'lib/easysuite/config.rb', line 19 def load_yaml(path) yaml = binread(path).to_s YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml) end |
.load_yaml(path) ⇒ Object
– load_yaml ++ Load object from YAML file.
14 15 16 17 |
# File 'lib/easysuite/config.rb', line 14 def load_yaml(path) yaml = binread(path).to_s YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml) end |
.read(path) ⇒ Object
– read ++ Read string from file.
35 36 37 |
# File 'lib/easysuite/config.rb', line 35 def read(path) IO.read(path) rescue nil end |
.set_values(obj, src, *keys) ⇒ Object
– set_values ++ Set values from Hash or YAML file.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/easysuite/config.rb', line 67 def set_values(obj, src, *keys) values = case src when Hash src when String load_yaml(src) rescue nil end if values.kind_of?(Hash) keys = keys.flatten keys = values.keys if keys.empty? keys.each do |key| writer = key.to_s + "=" obj.__send__(writer, values[key]) if obj.respond_to?(writer) end end obj end |
.write(path, str) ⇒ Object
– write ++ Write string to file.
43 44 45 |
# File 'lib/easysuite/config.rb', line 43 def write(path, str) IO.write(path, str) end |
Instance Method Details
#binread(filename) ⇒ Object
– binread ++ Read string from file.(binary mode)
220 221 222 223 |
# File 'lib/easysuite/config.rb', line 220 def binread(filename) path = (filename) Config.binread(path) end |
#binwrite(filename, str) ⇒ Object
– binwrite ++ Write string to file.(binary mode)
229 230 231 232 |
# File 'lib/easysuite/config.rb', line 229 def binwrite(filename, str) path = (filename) Config.binwrite(path, str) end |
#dump_yaml(filename, obj) ⇒ Object Also known as: dump
– dump_yaml ++ Dump object to YAML file.
191 192 193 194 |
# File 'lib/easysuite/config.rb', line 191 def dump_yaml(filename, obj) path = (filename) Config.dump_yaml(path, obj) end |
#load_yaml(filename) ⇒ Object Also known as: load
– load_yaml ++ Load object from YAML file.
180 181 182 183 |
# File 'lib/easysuite/config.rb', line 180 def load_yaml(filename) path = (filename) Config.load_yaml(path) end |
#read(filename) ⇒ Object
– read ++ Read string from file.
202 203 204 205 |
# File 'lib/easysuite/config.rb', line 202 def read(filename) path = (filename) Config.read(path) end |
#set_values(obj, src, *keys) ⇒ Object
– set_values ++ Set values from Hash or YAML file.
238 239 240 241 |
# File 'lib/easysuite/config.rb', line 238 def set_values(obj, src, *keys) src = (src) if src.kind_of?(String) Config.set_values(obj, src, *keys) end |