Project

yaml-sugar

0.0
No commit activity in last 3 years
No release in over 3 years
Read yaml files recursively from a given directory and return an OpenStruct
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 1.0
 Project Readme

The yaml-sugar gem for Ruby

Gem Version Build Status Coverage Status

yaml-sugar is a ruby gem inspired by hashugar. It reads all the yaml files from a given directory, and build them into an OpenStruct. If there are several yaml files with the same name, the later will be merged into the earlier.

How to use?

In your ruby code,

  require 'yaml/sugar'
  YamlSugar.load(dir)

then you are ready to go.

For example, if you have the following files inside your config directory:

.
+-- config
       +-- fox.yaml
       +-- consumer
       |       +-- consumer.yaml
       +-- people.yaml
       +-- country
               +-- people.yaml

config/fox.yaml

  color:
    is:
      green: true

config/consumer/consumer.yaml

  language: english
  hobbies:
    balls: tennis

config/people.yaml

  average_age: 90

config/country/people.yaml

  average_age: 75
  longest_age: 112

In your project,

  YamlSugar.load('config')

  assert YamlSugar.fox.color.is.green
  assert YamlSugar.people.average_age == 90
  assert YamlSugar.people.longest_age == 112
  assert YamlSugar.consumer.language == 'english'
  assert YamlSugar.consumer.hobbies.balls == 'tennis'

You can also add a dynamic attribute on the fly like this:

  YamlSugar.set(:attr, :foo)
  assert YamlSugar.attr == :foo

or simply like this:

  YamlSugar.attr = :foo
  assert YamlSugar.attr == :foo

The default order for loading the yaml files with same name is dependant on the implementation of Find.find(dir). If that doesn't meet your expectation, you can explicitly load them again. When the two yaml files with same name are merged, you can choose either a normal merge or deep merge.

For instance, assume you have the following two files:

.
+-- config
       +-- aminal
       |     +-- setting.yaml
       +-- bird
             +-- setting.yaml

config/animal/setting.yaml

  type: animal
  list:
    - monkey
    - sheep

config/bird/setting.yaml

  type: bird
  list:
    - seagull
    - pigeon

Load with overwriting fields with same names:

  YamlSugar.load('config/animal')
  YamlSugar.load('config/bird')

  assert YamlSugar.setting.type == 'bird'
  assert YamlSugar.setting.list == %w(seagull pigeon)

Load with merging fields with same names:

  YamlSugar.load('config/animal')
  YamlSugar.load('config/bird', deep_merge: true)

  assert YamlSugar.setting.type == 'bird'
  assert YamlSugar.setting.list == %w(seagull pigeon monkey sheep)

If you wanna clear all the previous settings, just call

  YamlSugar.clear

Sometimes you might want to have multiple instances of YamlSugar, which are independent of each other. In that case you can just use the following syntax:

  animal_config = YamlSugar.new
  bird_config = YamlSugar.new
  animal_config.load('config/animal')
  bird_config.load('config/bird')

  assert animal_config.setting.type == 'animal'
  assert bird_config.setting.type == 'bird'

  assert animal_config.setting.list == %w(monkey sheep)
  assert bird_config.setting.list == %w(seagull pigeon)

  animal_config.set(:from, :land) # or animal_config.from = :land
  bird_config.set(:from, :sky)  # or bird_config.from = :sky

  assert animal_config.from == :land
  assert bird_config.from == :sky

How to install?

From a terminal run

  gem install yaml-sugar

or add the following code into your Gemfiles:

  gem 'yaml-sugar'

How to build/install from source?

  gem build yaml-sugar.gemspec
  gem install yaml-sugar-<VERSION>.gem

How to run the test?

  rake test

License

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.