{"id":19343516,"url":"https://github.com/hex0cter/yaml-sugar","last_synced_at":"2025-04-23T04:35:46.811Z","repository":{"id":56898947,"uuid":"56009350","full_name":"hex0cter/yaml-sugar","owner":"hex0cter","description":"(Deprecated: Use yaml-ostruct instead) Ruby gem to read yaml files recursively from a given directory and return an OpenStruct","archived":false,"fork":false,"pushed_at":"2016-05-21T06:42:50.000Z","size":6,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-30T10:23:40.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/yaml-sugar","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hex0cter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-11T21:34:52.000Z","updated_at":"2018-02-07T14:33:38.000Z","dependencies_parsed_at":"2022-08-20T17:30:12.755Z","dependency_job_id":null,"html_url":"https://github.com/hex0cter/yaml-sugar","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex0cter%2Fyaml-sugar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex0cter%2Fyaml-sugar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex0cter%2Fyaml-sugar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hex0cter%2Fyaml-sugar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hex0cter","download_url":"https://codeload.github.com/hex0cter/yaml-sugar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223910050,"owners_count":17223591,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T03:38:33.898Z","updated_at":"2024-11-10T03:38:34.523Z","avatar_url":"https://github.com/hex0cter.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The yaml-sugar gem for Ruby\n\n[![Gem Version](https://badge.fury.io/rb/yaml-sugar.svg)](https://badge.fury.io/rb/yaml-sugar)\n[![Build Status](https://travis-ci.org/hex0cter/yaml-sugar.svg?branch=master)](https://travis-ci.org/hex0cter/yaml-sugar)\n[![Coverage Status](https://coveralls.io/repos/github/hex0cter/yaml-sugar/badge.svg?branch=master)](https://coveralls.io/github/hex0cter/yaml-sugar?branch=master)\n\nyaml-sugar is a ruby gem inspired by hashugar. It reads all the yaml files from\na given directory, and build them into an OpenStruct. If there are several yaml\nfiles with the same name, the later will be merged into the earlier.\n\n## How to use?\n\nIn your ruby code,\n\n```ruby\n  require 'yaml/sugar'\n  YamlSugar.load(dir)\n```\n\nthen you are ready to go.\n\nFor example, if you have the following files inside your config directory:\n\n```\n.\n+-- config\n       +-- fox.yaml\n       +-- consumer\n       |       +-- consumer.yaml\n       +-- people.yaml\n       +-- country\n               +-- people.yaml\n```\n\nconfig/fox.yaml\n```yaml\n  color:\n    is:\n      green: true\n```\n\nconfig/consumer/consumer.yaml\n\n```yaml\n  language: english\n  hobbies:\n    balls: tennis\n```\n\nconfig/people.yaml\n\n```yaml\n  average_age: 90\n```\n\nconfig/country/people.yaml\n\n```yaml\n  average_age: 75\n  longest_age: 112\n```\n\n\nIn your project,\n\n```ruby\n  YamlSugar.load('config')\n\n  assert YamlSugar.fox.color.is.green\n  assert YamlSugar.people.average_age == 90\n  assert YamlSugar.people.longest_age == 112\n  assert YamlSugar.consumer.language == 'english'\n  assert YamlSugar.consumer.hobbies.balls == 'tennis'\n```\n\nYou can also add a dynamic attribute on the fly like this:\n\n```ruby\n  YamlSugar.set(:attr, :foo)\n  assert YamlSugar.attr == :foo\n```\n\nor simply like this:\n\n```ruby\n  YamlSugar.attr = :foo\n  assert YamlSugar.attr == :foo\n```\n\nThe default order for loading the yaml files with same name is dependant\non the implementation of Find.find(dir). If that doesn't meet your\nexpectation, you can explicitly load them again. When the two yaml files\nwith same name are merged, you can choose either a normal merge or deep merge.\n\nFor instance, assume you have the following two files:\n\n```\n.\n+-- config\n       +-- aminal\n       |     +-- setting.yaml\n       +-- bird\n             +-- setting.yaml\n```\n\nconfig/animal/setting.yaml\n\n```yaml\n  type: animal\n  list:\n    - monkey\n    - sheep\n\n```\n\nconfig/bird/setting.yaml\n\n```yaml\n  type: bird\n  list:\n    - seagull\n    - pigeon\n```\n\nLoad with overwriting fields with same names:\n\n```ruby\n  YamlSugar.load('config/animal')\n  YamlSugar.load('config/bird')\n\n  assert YamlSugar.setting.type == 'bird'\n  assert YamlSugar.setting.list == %w(seagull pigeon)\n```\n\nLoad with merging fields with same names:\n\n```ruby\n  YamlSugar.load('config/animal')\n  YamlSugar.load('config/bird', deep_merge: true)\n\n  assert YamlSugar.setting.type == 'bird'\n  assert YamlSugar.setting.list == %w(seagull pigeon monkey sheep)\n```\n\nIf you wanna clear all the previous settings, just call\n\n```ruby\n  YamlSugar.clear\n```\n\nSometimes you might want to have multiple instances of YamlSugar, which are independent\nof each other. In that case you can just use the following syntax:\n\n```ruby\n  animal_config = YamlSugar.new\n  bird_config = YamlSugar.new\n  animal_config.load('config/animal')\n  bird_config.load('config/bird')\n\n  assert animal_config.setting.type == 'animal'\n  assert bird_config.setting.type == 'bird'\n\n  assert animal_config.setting.list == %w(monkey sheep)\n  assert bird_config.setting.list == %w(seagull pigeon)\n\n  animal_config.set(:from, :land) # or animal_config.from = :land\n  bird_config.set(:from, :sky)  # or bird_config.from = :sky\n\n  assert animal_config.from == :land\n  assert bird_config.from == :sky\n```\n\n## How to install?\n\nFrom a terminal run\n\n```bash\n  gem install yaml-sugar\n```\n\nor add the following code into your Gemfiles:\n\n```ruby\n  gem 'yaml-sugar'\n```\n\n## How to build/install from source?\n\n```bash\n  gem build yaml-sugar.gemspec\n  gem install yaml-sugar-\u003cVERSION\u003e.gem\n```\n\n## How to run the test?\n\n```bash\n  rake test\n```\n\n## License\n\nThis code is free to use under the terms of the MIT license.\n\n## Contribution\n\nYou are more than welcome to raise any issues [here](https://github.com/hex0cter/yaml-sugar/issues), or create a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhex0cter%2Fyaml-sugar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhex0cter%2Fyaml-sugar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhex0cter%2Fyaml-sugar/lists"}