0.0
Low commit activity in last 3 years
No release in over a year
Building OpenStruct trees from configuration files
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 1.15
>= 13.0
>= 3.8
 Project Readme

Gem Version Build

OpenConfig

Ruby gem, that allows you to build deep OpenStructs from your YAML or JSON configuration files

Installation

Add open_config to the project's Gemfile:

gem 'open_config', '~> 2.0'

or as a dependency in your gem's .gemspec file

Gem::Specification.new do |spec|
  # ...
  spec.add_dependency 'open_config', '~> 2.0'
  # ...
end

Supported Ruby versions

  • MRI >= 2.4
  • JRuby >= 9.2

Usage

Imagine, you have file, called configuration.yml in your project's config folder:

ruby: <%= 2 + 2 * 2 %>
node:
  string: Some String
  integer: 123
  float: 1.23
  boolean: false
  nested_node:
    array:
      - First Element
      - Second Element

You can create OpenConfig instance, and access configuration keys using all advantages of Ruby's OpenStruct

> config = OpenConfig::YAML.new('configuration.yml')
=> #<OpenConfig::Node
  ruby: 6,
  node: #<OpenConfig::Node
    string: "Some String",
    integer: 123,
    float: 1.23,
    boolean: false,
    nested_node: #<OpenConfig::Node array: ["First Element", "Second Element"]>>>

> config.ruby
=> 6

> config.node
=> #<OpenConfig::Node
  string: "Some String",
  integer: 123,
  float: 1.23,
  boolean: false,
  nested_node: #<OpenConfig::Node array: ["First Element", "Second Element"]>>

> config.node['string']
=> "Some String"

> config.node[:boolean]
=> false

> config.dig(:node, :string)
=> "Some String"

> config.fetch(:foobar, 123)
=> 123

> config.fetch(:foobar) # => Exception in `fetch': key not found: :foobar (KeyError)
> config.foobar # => Exception in `method_missing': undefined method `foobar' for #<OpenConfig::Node>

Same thing will work with configuration.json, just use OpenConfig::JSON instead

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/eiskrenkov/open_config

License

OpenConfig is released under MIT License