Module: YAMLCon
- Defined in:
- lib/yamlcon/version.rb,
lib/yamlcon/yamlcon.rb
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
- .hash_to_struct(hash) ⇒ Object
- .load_config(file) ⇒ Object
- .save_config(file, data) ⇒ Object
- .struct_to_hash(dot_notation) ⇒ Object
- .struct_to_yaml(dot_notation) ⇒ Object
Class Method Details
.hash_to_struct(hash) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yamlcon/yamlcon.rb', line 16 def self.hash_to_struct(hash) return hash unless hash.is_a? Hash dotted = OpenStruct.new hash hash.each do |key, value| if value.is_a? Hash dotted[key] = hash_to_struct(value) elsif value.is_a? Array dotted[key] = value.collect {|item| hash_to_struct item } end end dotted end |
.load_config(file) ⇒ Object
5 6 7 8 |
# File 'lib/yamlcon/yamlcon.rb', line 5 def self.load_config(file) hash = YAML.load_file file hash_to_struct hash end |
.save_config(file, data) ⇒ Object
10 11 12 13 14 |
# File 'lib/yamlcon/yamlcon.rb', line 10 def self.save_config(file, data) File.open file, 'w' do |f| f.write struct_to_yaml data end end |
.struct_to_hash(dot_notation) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/yamlcon/yamlcon.rb', line 31 def self.struct_to_hash(dot_notation) hash = {} dot_notation.each_pair do |k, v| hash[k.to_s] = v.is_a?(OpenStruct) ? struct_to_hash(v) : v end hash end |
.struct_to_yaml(dot_notation) ⇒ Object
39 40 41 |
# File 'lib/yamlcon/yamlcon.rb', line 39 def self.struct_to_yaml(dot_notation) struct_to_hash(dot_notation).to_yaml end |