Class: EasySuite::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/easysuite/config.rb

Overview

– Config ++ Class to config easily.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir = nil) ⇒ Config

– initialize ++

config_dir

Default directory of instance.



163
164
165
# File 'lib/easysuite/config.rb', line 163

def initialize(config_dir = nil)
  @config_dir = config_dir
end

Instance Attribute Details

#config_dirObject

Returns the value of attribute config_dir.



156
157
158
# File 'lib/easysuite/config.rb', line 156

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

.dumpObject

– 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).



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/easysuite/config.rb', line 91

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).



126
127
128
129
130
131
132
133
134
# File 'lib/easysuite/config.rb', line 126

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).



112
113
114
115
116
117
118
119
120
# File 'lib/easysuite/config.rb', line 112

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

.loadObject

– 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
# 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 = 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)



211
212
213
214
# File 'lib/easysuite/config.rb', line 211

def binread(filename)
  path = expand_path(filename)
  Config.binread(path)
end

#binwrite(filename, str) ⇒ Object

– binwrite ++ Write string to file.(binary mode)



220
221
222
223
# File 'lib/easysuite/config.rb', line 220

def binwrite(filename, str)
  path = expand_path(filename)
  Config.binwrite(path, str)
end

#dump_yaml(filename, obj) ⇒ Object Also known as: dump

– dump_yaml ++ Dump object to YAML file.



182
183
184
185
# File 'lib/easysuite/config.rb', line 182

def dump_yaml(filename, obj)
  path = expand_path(filename)
  Config.dump_yaml(path, obj)
end

#load_yaml(filename) ⇒ Object Also known as: load

– load_yaml ++ Load object from YAML file.



171
172
173
174
# File 'lib/easysuite/config.rb', line 171

def load_yaml(filename)
  path = expand_path(filename)
  Config.load_yaml(path)
end

#read(filename) ⇒ Object

– read ++ Read string from file.



193
194
195
196
# File 'lib/easysuite/config.rb', line 193

def read(filename)
  path = expand_path(filename)
  Config.read(path)
end

#set_values(obj, src, *keys) ⇒ Object

– set_values ++ Set values from Hash or YAML file.



229
230
231
232
# File 'lib/easysuite/config.rb', line 229

def set_values(obj, src, *keys)
  src = expand_path(src) if src.kind_of?(String)
  Config.set_values(obj, src, *keys)
end

#write(filename, str) ⇒ Object

– write ++ Write string to file.



202
203
204
205
# File 'lib/easysuite/config.rb', line 202

def write(filename, str)
  path = expand_path(filename)
  Config.write(path, str)
end