The yaml-ostruct gem for Ruby
Previously known as yaml-sugar, yaml-ostruct is a ruby gem inspired by hashugar. It reads all the yaml files from a given directory, and build them into an OpenStruct.
The gem works in two modes, omitting the directories in which the yaml files are located, or having it as part of the structure. By default the later is used. If you want to omit the directories, use option omit_path: true in the configure method. In that case, files with the same name but located in different directories will be merged.
How to use?
In your ruby code,
require 'yaml/ostruct'
YamlOstruct.load(dir)then you are ready to go.
For example, if you have the following files inside your config directory:
.
+-- config
+-- fox.yaml
+-- asia
+-- china
| +-----people.yaml
+-- people.yaml
config/fox.yaml
color:
is:
green: trueconfig/asia/china/people.yaml
language: chineseconfig/asia/people.yaml
language: englishBy default, parent directories of the yaml file are used as part of the structure. For example,
YamlOstruct.load('config')
assert YamlOstruct.fox.color.is.brown
assert YamlOstruct.asia.china.people.language == 'chinese'
assert YamlOstruct.asia.people.language == 'english'However if you want to omit the directories,
YamlOstruct.configure do |configure|
configure.omit_path = true
end
YamlOstruct.load('config')
assert YamlOstruct.fox.color.is.brown
assert YamlOstruct.people.language == 'english'In this case if there are multiple files with the same name found in different directories, they will be merged before the conversion. You can specify
YamlOstruct.configure do |configure|
configure.omit_path = true
configure.deep_merge = true
endfor a deep merge.
To skip the errors in parsing the yaml files, you can use
YamlOstruct.configure do |configure|
configure.skip_error = true
endYou can also add a dynamic attribute on the fly like this:
YamlOstruct.attr = :foo
assert YamlOstruct.attr == :fooIf you wanna remove an attribute, you can call
YamlOstruct.delete(:attr)If you wanna clear all the previous settings, just call
YamlOstruct.clearOr
YamlOstruct.delete_allSometimes you might want to have multiple instances of YamlOstruct, which are independent of each other. In that case you can just use the following syntax:
config = YamlOstruct.new # Or YamlOstruct.new(omit_path: true, deep_merge: true, skip_error: true)
config.load('config')
config.attr = :land
assert config.attr == :landHow to install?
From a terminal run
gem install yaml-ostructor add the following code into your Gemfiles:
gem 'yaml-ostruct'How to build/install from source?
gem build yaml-ostruct.gemspec
gem install yaml-ostruct-<VERSION>.gemHow to run the test?
rake testLicense
This code is free to use under the terms of the MIT license.
Contribution
You are more than welcome to raise any issues here, or create a Pull Request.