rb-konfig 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +52 -0
- data/.rspec +1 -0
- data/.rubocop.yml +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +76 -0
- data/README.md +101 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/konfig.gemspec +37 -0
- data/lib/konfig.rb +55 -0
- data/lib/konfig/config_provider.rb +28 -0
- data/lib/konfig/directory_provider.rb +76 -0
- data/lib/konfig/errors.rb +5 -0
- data/lib/konfig/version.rb +3 -0
- data/lib/konfig/yaml_provider.rb +30 -0
- metadata +203 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 7bffadd86f96ef3163efc25608dde8cc6b13e238687d9e5b1366afcf5ee67b56
         | 
| 4 | 
            +
              data.tar.gz: 0fa8992ef19d0985253337af611418d20f8a1cbf74642879571d8c400e479cf4
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 4e354c93f08eba4c21fd691fc012c01769afec1dbf931da322fee7b3e46e58fa07ab0d13004e5b1c211ebeb55647656ee02816043aee9f441b3fb3c27e80a148
         | 
| 7 | 
            +
              data.tar.gz: cbbbf864513bf6dfac4958616fd3e536962204cab81d7c1039e1832919b777f925e66dc485f3830413daabf0f24f389b2b4cb1afaef9d946110103d394219da0
         | 
    
        data/.gitignore
    ADDED
    
    | @@ -0,0 +1,52 @@ | |
| 1 | 
            +
            *.gem
         | 
| 2 | 
            +
            *.rbc
         | 
| 3 | 
            +
            /.config
         | 
| 4 | 
            +
            /coverage/
         | 
| 5 | 
            +
            /InstalledFiles
         | 
| 6 | 
            +
            /pkg/
         | 
| 7 | 
            +
            /spec/reports/
         | 
| 8 | 
            +
            /spec/examples.txt
         | 
| 9 | 
            +
            /test/tmp/
         | 
| 10 | 
            +
            /test/version_tmp/
         | 
| 11 | 
            +
            /tmp/
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            # Used by dotenv library to load environment variables.
         | 
| 14 | 
            +
            # .env
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            ## Specific to RubyMotion:
         | 
| 17 | 
            +
            .dat*
         | 
| 18 | 
            +
            .repl_history
         | 
| 19 | 
            +
            build/
         | 
| 20 | 
            +
            *.bridgesupport
         | 
| 21 | 
            +
            build-iPhoneOS/
         | 
| 22 | 
            +
            build-iPhoneSimulator/
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            ## Specific to RubyMotion (use of CocoaPods):
         | 
| 25 | 
            +
            #
         | 
| 26 | 
            +
            # We recommend against adding the Pods directory to your .gitignore. However
         | 
| 27 | 
            +
            # you should judge for yourself, the pros and cons are mentioned at:
         | 
| 28 | 
            +
            # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
         | 
| 29 | 
            +
            #
         | 
| 30 | 
            +
            # vendor/Pods/
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            ## Documentation cache and generated files:
         | 
| 33 | 
            +
            /.yardoc/
         | 
| 34 | 
            +
            /_yardoc/
         | 
| 35 | 
            +
            /doc/
         | 
| 36 | 
            +
            /rdoc/
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            ## Environment normalization:
         | 
| 39 | 
            +
            /.bundle/
         | 
| 40 | 
            +
            /vendor/bundle
         | 
| 41 | 
            +
            /lib/bundler/man/
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            # for a library or gem, you might want to ignore these files since the code is
         | 
| 44 | 
            +
            # intended to run in multiple environments; otherwise, check them in:
         | 
| 45 | 
            +
            # Gemfile.lock
         | 
| 46 | 
            +
            # .ruby-version
         | 
| 47 | 
            +
            # .ruby-gemset
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
         | 
| 50 | 
            +
            .rvmrc
         | 
| 51 | 
            +
            /.byebug_history
         | 
| 52 | 
            +
            /.idea
         | 
    
        data/.rspec
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            --require spec_helper
         | 
    
        data/.rubocop.yml
    ADDED
    
    
    
        data/.ruby-gemset
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            konfig
         | 
    
        data/.ruby-version
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/Gemfile.lock
    ADDED
    
    | @@ -0,0 +1,76 @@ | |
| 1 | 
            +
            PATH
         | 
| 2 | 
            +
              remote: .
         | 
| 3 | 
            +
              specs:
         | 
| 4 | 
            +
                rb-konfig (0.1.0)
         | 
| 5 | 
            +
                  activesupport
         | 
| 6 | 
            +
                  hash_dot (~> 2.4)
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            GEM
         | 
| 9 | 
            +
              remote: https://rubygems.org/
         | 
| 10 | 
            +
              specs:
         | 
| 11 | 
            +
                activesupport (5.2.3)
         | 
| 12 | 
            +
                  concurrent-ruby (~> 1.0, >= 1.0.2)
         | 
| 13 | 
            +
                  i18n (>= 0.7, < 2)
         | 
| 14 | 
            +
                  minitest (~> 5.1)
         | 
| 15 | 
            +
                  tzinfo (~> 1.1)
         | 
| 16 | 
            +
                ast (2.4.0)
         | 
| 17 | 
            +
                byebug (11.0.1)
         | 
| 18 | 
            +
                concurrent-ruby (1.1.5)
         | 
| 19 | 
            +
                diff-lcs (1.3)
         | 
| 20 | 
            +
                factory_bot (5.0.2)
         | 
| 21 | 
            +
                  activesupport (>= 4.2.0)
         | 
| 22 | 
            +
                hash_dot (2.4.1)
         | 
| 23 | 
            +
                i18n (1.6.0)
         | 
| 24 | 
            +
                  concurrent-ruby (~> 1.0)
         | 
| 25 | 
            +
                jaro_winkler (1.5.3)
         | 
| 26 | 
            +
                minitest (5.11.3)
         | 
| 27 | 
            +
                parallel (1.17.0)
         | 
| 28 | 
            +
                parser (2.6.3.0)
         | 
| 29 | 
            +
                  ast (~> 2.4.0)
         | 
| 30 | 
            +
                rainbow (3.0.0)
         | 
| 31 | 
            +
                rake (10.5.0)
         | 
| 32 | 
            +
                rspec (3.8.0)
         | 
| 33 | 
            +
                  rspec-core (~> 3.8.0)
         | 
| 34 | 
            +
                  rspec-expectations (~> 3.8.0)
         | 
| 35 | 
            +
                  rspec-mocks (~> 3.8.0)
         | 
| 36 | 
            +
                rspec-core (3.8.1)
         | 
| 37 | 
            +
                  rspec-support (~> 3.8.0)
         | 
| 38 | 
            +
                rspec-expectations (3.8.4)
         | 
| 39 | 
            +
                  diff-lcs (>= 1.2.0, < 2.0)
         | 
| 40 | 
            +
                  rspec-support (~> 3.8.0)
         | 
| 41 | 
            +
                rspec-mocks (3.8.1)
         | 
| 42 | 
            +
                  diff-lcs (>= 1.2.0, < 2.0)
         | 
| 43 | 
            +
                  rspec-support (~> 3.8.0)
         | 
| 44 | 
            +
                rspec-support (3.8.2)
         | 
| 45 | 
            +
                rubocop (0.72.0)
         | 
| 46 | 
            +
                  jaro_winkler (~> 1.5.1)
         | 
| 47 | 
            +
                  parallel (~> 1.10)
         | 
| 48 | 
            +
                  parser (>= 2.6)
         | 
| 49 | 
            +
                  rainbow (>= 2.2.2, < 4.0)
         | 
| 50 | 
            +
                  ruby-progressbar (~> 1.7)
         | 
| 51 | 
            +
                  unicode-display_width (>= 1.4.0, < 1.7)
         | 
| 52 | 
            +
                rubocop-performance (1.4.0)
         | 
| 53 | 
            +
                  rubocop (>= 0.71.0)
         | 
| 54 | 
            +
                ruby-progressbar (1.10.1)
         | 
| 55 | 
            +
                rufo (0.7.0)
         | 
| 56 | 
            +
                thread_safe (0.3.6)
         | 
| 57 | 
            +
                tzinfo (1.2.5)
         | 
| 58 | 
            +
                  thread_safe (~> 0.1)
         | 
| 59 | 
            +
                unicode-display_width (1.6.0)
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            PLATFORMS
         | 
| 62 | 
            +
              ruby
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            DEPENDENCIES
         | 
| 65 | 
            +
              bundler (~> 2.0)
         | 
| 66 | 
            +
              byebug
         | 
| 67 | 
            +
              factory_bot (~> 5.0)
         | 
| 68 | 
            +
              rake (~> 10.0)
         | 
| 69 | 
            +
              rb-konfig!
         | 
| 70 | 
            +
              rspec (~> 3.8)
         | 
| 71 | 
            +
              rubocop (~> 0.69)
         | 
| 72 | 
            +
              rubocop-performance (~> 1.3)
         | 
| 73 | 
            +
              rufo (~> 0.7)
         | 
| 74 | 
            +
             | 
| 75 | 
            +
            BUNDLED WITH
         | 
| 76 | 
            +
               2.0.2
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,101 @@ | |
| 1 | 
            +
            # Konfig
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Konfig is a Kubernetes friendly Rails configuration file. While Rails applications can easily read YAML files to load configurations, Kubernetes is good at serving individual configuration values as files via Kubernetes Secrets. This means your Rails application needs to read the same configuration file from a YAML file in development or an individual file while running in Kubernetes. Konfig can load configuration and secrets from both YAML or folders with individual files and present them to your application the same way.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            ## Installation
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            Add this line to your application's Gemfile:
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ```ruby
         | 
| 10 | 
            +
            gem 'rb-konfig'
         | 
| 11 | 
            +
            ```
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            And then execute:
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                $ bundle
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            Or install it yourself as:
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                $ gem install rb-konfig
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            ## Usage
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            ```ruby
         | 
| 24 | 
            +
            Konfig.configuration.mode = :yaml
         | 
| 25 | 
            +
            Konfig.configuration.workdir = "settings/folder"
         | 
| 26 | 
            +
            Konfig.load
         | 
| 27 | 
            +
            ```
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            or if you'd like to use it in Kubernetes:
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            ```ruby
         | 
| 32 | 
            +
            Konfig.configuration.mode = :directory
         | 
| 33 | 
            +
            Konfig.configuration.workdir = "settings/folder"
         | 
| 34 | 
            +
            Konfig.load
         | 
| 35 | 
            +
            ```
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            Now you can use Konfig anywhere in the code:
         | 
| 38 | 
            +
             | 
| 39 | 
            +
            ```ruby
         | 
| 40 | 
            +
            puts Settings.some.configuration.value
         | 
| 41 | 
            +
            ```
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            In `development` mode, Konfig, looks for `development.yml` in `work_dir`. In `kubernetes` mode, it looks for a file for each one of the given configuration keys. For example:
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            ```yml
         | 
| 46 | 
            +
            # development.yml
         | 
| 47 | 
            +
            some:
         | 
| 48 | 
            +
                configuration:
         | 
| 49 | 
            +
                    value: true
         | 
| 50 | 
            +
            ```
         | 
| 51 | 
            +
             | 
| 52 | 
            +
            ```bash
         | 
| 53 | 
            +
            # directory mode
         | 
| 54 | 
            +
            $ ls config/settings
         | 
| 55 | 
            +
            -rw-r--r--    1 khash  staff    20 10 May 07:20 some.configuration.value
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            $ cat config/settings/some.configuration.value
         | 
| 58 | 
            +
            true
         | 
| 59 | 
            +
            ```
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            The value in `some.configuration.value` file can be `true`. Konfig tries to clean the file and coerce the value into the right type before returning. If the file or the key in yaml is missing, it will return a `Konfig::MissingConfiguration` is thrown.
         | 
| 62 | 
            +
             | 
| 63 | 
            +
            ### NULL / nil values
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            By default YAML returns `nil` for a `null` value in a YAML file. This is also replicated in directory mode.
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            ### Configuration
         | 
| 68 | 
            +
            You can change or reach the following from `Konfig.configuration`
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            `namespace`: Default is `Settings`
         | 
| 71 | 
            +
            `delimiter`: Default is `.`
         | 
| 72 | 
            +
            `default_config_file`: Default is `development.yml`
         | 
| 73 | 
            +
            `allow_nil`: Default is `true`
         | 
| 74 | 
            +
            `nil_word`: Default is `null`
         | 
| 75 | 
            +
            `mode`: No default value
         | 
| 76 | 
            +
            `workdir`: No default value
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            ### Data types
         | 
| 79 | 
            +
             | 
| 80 | 
            +
            The directory mode, supports the following data types in files and tries to return the right type:
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            - Integer
         | 
| 83 | 
            +
            - Float
         | 
| 84 | 
            +
            - String
         | 
| 85 | 
            +
            - Date time
         | 
| 86 | 
            +
            - Boolean
         | 
| 87 | 
            +
            - Null (see above)
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            ### ERB
         | 
| 90 | 
            +
             | 
| 91 | 
            +
            YAML mode supports ERB in your YAML file, just like default Rails behavior
         | 
| 92 | 
            +
             | 
| 93 | 
            +
            ## Development
         | 
| 94 | 
            +
             | 
| 95 | 
            +
            After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
         | 
| 96 | 
            +
             | 
| 97 | 
            +
            To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            ## Contributing
         | 
| 100 | 
            +
             | 
| 101 | 
            +
            Bug reports and pull requests are welcome on GitHub at https://github.com/khash/konfig.
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/bin/console
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "bundler/setup"
         | 
| 4 | 
            +
            require "konfig"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # You can add fixtures and/or initialization code here to make experimenting
         | 
| 7 | 
            +
            # with your gem easier. You can also use a different console, if you like.
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # (If you use this, don't forget to add pry to your Gemfile!)
         | 
| 10 | 
            +
            # require "pry"
         | 
| 11 | 
            +
            # Pry.start
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            require "irb"
         | 
| 14 | 
            +
            IRB.start(__FILE__)
         | 
    
        data/bin/setup
    ADDED
    
    
    
        data/konfig.gemspec
    ADDED
    
    | @@ -0,0 +1,37 @@ | |
| 1 | 
            +
            lib = File.expand_path("lib", __dir__)
         | 
| 2 | 
            +
            $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
         | 
| 3 | 
            +
            require "konfig/version"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Gem::Specification.new do |spec|
         | 
| 6 | 
            +
              spec.name = "rb-konfig"
         | 
| 7 | 
            +
              spec.version = Konfig::VERSION
         | 
| 8 | 
            +
              spec.authors = ["Khash Sajadi"]
         | 
| 9 | 
            +
              spec.email = ["khash@sajadi.co.uk"]
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              spec.summary = %q{Konfig is a Kubernetes friendly Rails configuration gem}
         | 
| 12 | 
            +
              spec.homepage = "https://github.com/khash/konfig"
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              spec.metadata["homepage_uri"] = spec.homepage
         | 
| 15 | 
            +
              spec.metadata["source_code_uri"] = "https://github.com/khash/konfig"
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              # Specify which files should be added to the gem when it is released.
         | 
| 18 | 
            +
              # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
         | 
| 19 | 
            +
              spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
         | 
| 20 | 
            +
                `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
              spec.bindir = "exe"
         | 
| 23 | 
            +
              spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         | 
| 24 | 
            +
              spec.require_paths = ["lib"]
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              spec.add_dependency "hash_dot", "~> 2.4"
         | 
| 27 | 
            +
              spec.add_dependency "activesupport"
         | 
| 28 | 
            +
             | 
| 29 | 
            +
              spec.add_development_dependency "bundler", "~> 2.0"
         | 
| 30 | 
            +
              spec.add_development_dependency "rake", "~> 10.0"
         | 
| 31 | 
            +
              spec.add_development_dependency "factory_bot", "~> 5.0"
         | 
| 32 | 
            +
              spec.add_development_dependency "rspec", "~> 3.8"
         | 
| 33 | 
            +
              spec.add_development_dependency "rubocop", "~> 0.69"
         | 
| 34 | 
            +
              spec.add_development_dependency "rubocop-performance", "~> 1.3"
         | 
| 35 | 
            +
              spec.add_development_dependency "rufo", "~> 0.7"
         | 
| 36 | 
            +
              spec.add_development_dependency "byebug"
         | 
| 37 | 
            +
            end
         | 
    
        data/lib/konfig.rb
    ADDED
    
    | @@ -0,0 +1,55 @@ | |
| 1 | 
            +
            require "active_support/core_ext/hash"
         | 
| 2 | 
            +
            Dir["#{File.dirname(__FILE__)}/konfig/**/*.rb"].each { |f| load(f) }
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module Konfig
         | 
| 5 | 
            +
              def self.configuration
         | 
| 6 | 
            +
                @config ||= Konfig::Config.new
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def self.load
         | 
| 10 | 
            +
                provider = ConfigProvider.provider(mode: self.configuration.mode, workdir: self.configuration.workdir)
         | 
| 11 | 
            +
                provider.load
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              class Config
         | 
| 15 | 
            +
                attr_writer :namespace
         | 
| 16 | 
            +
                attr_writer :delimiter
         | 
| 17 | 
            +
                attr_writer :default_config_file
         | 
| 18 | 
            +
                attr_writer :allow_nil
         | 
| 19 | 
            +
                attr_writer :nil_word
         | 
| 20 | 
            +
                attr_writer :mode
         | 
| 21 | 
            +
                attr_writer :workdir
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def namespace
         | 
| 24 | 
            +
                  @namespace || "Settings"
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def workdir
         | 
| 28 | 
            +
                  raise NotConfiguredError, "have you set workdir?" unless @workdir
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                  @workdir
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def mode
         | 
| 34 | 
            +
                  raise NotConfiguredError, "have you set mode?" unless @mode
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  @mode
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                def delimiter
         | 
| 40 | 
            +
                  @delimiter || "."
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                def default_config_file
         | 
| 44 | 
            +
                  @default_config_file || "development.yml"
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                def allow_nil
         | 
| 48 | 
            +
                  @allow_nil.nil? ? true : @allow_nil
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def nil_word
         | 
| 52 | 
            +
                  @nil_word || "null"
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
              end
         | 
| 55 | 
            +
            end
         | 
| @@ -0,0 +1,28 @@ | |
| 1 | 
            +
            module Konfig
         | 
| 2 | 
            +
              class ConfigProvider
         | 
| 3 | 
            +
                attr_reader :mode
         | 
| 4 | 
            +
                attr_reader :workdir
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                def initialize(mode:, workdir:)
         | 
| 7 | 
            +
                  raise ArgumentError unless workdir
         | 
| 8 | 
            +
                  raise ArgumentError unless [:yaml, :directory].include? mode
         | 
| 9 | 
            +
                  raise FileNotFound, "directory #{workdir} not found" unless Dir.exist? workdir
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                  @mode = mode
         | 
| 12 | 
            +
                  @workdir = workdir
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def self.provider(mode:, workdir:)
         | 
| 16 | 
            +
                  raise ArgumentError unless workdir
         | 
| 17 | 
            +
                  raise ArgumentError unless [:yaml, :directory].include? mode
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  if mode == :yaml
         | 
| 20 | 
            +
                    return YamlProvider.new(workdir: workdir)
         | 
| 21 | 
            +
                  elsif mode == :directory
         | 
| 22 | 
            +
                    return DirectoryProvider.new(workdir: workdir)
         | 
| 23 | 
            +
                  else
         | 
| 24 | 
            +
                    raise ArgumentError
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
            end
         | 
| @@ -0,0 +1,76 @@ | |
| 1 | 
            +
            require_relative "config_provider"
         | 
| 2 | 
            +
            require "date"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module Konfig
         | 
| 5 | 
            +
              class NilClass; end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              NIL_VALUE = NilClass.new
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              class DirectoryProvider < ConfigProvider
         | 
| 10 | 
            +
                def initialize(workdir:)
         | 
| 11 | 
            +
                  super(mode: :yaml, workdir: workdir)
         | 
| 12 | 
            +
                  @files = Dir.children(@workdir)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def load
         | 
| 16 | 
            +
                  build_object_from_list(@files)
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                private
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                # deserializers. order matters here
         | 
| 22 | 
            +
                DESERIALIZERS = [
         | 
| 23 | 
            +
                  lambda { |value| Integer(value) rescue NIL_VALUE }, # integer
         | 
| 24 | 
            +
                  lambda { |value| Float(value) rescue NIL_VALUE }, # float
         | 
| 25 | 
            +
                  lambda { |value| (["true", "false"].include?(value)) ? (value == "true") : NIL_VALUE }, # boolean
         | 
| 26 | 
            +
                  lambda { |value| DateTime.parse(value) rescue NIL_VALUE }, # date time
         | 
| 27 | 
            +
                  lambda { |value| (value == Konfig.configuration.nil_word && Konfig.configuration.allow_nil) ? nil : NIL_VALUE }, # nil value
         | 
| 28 | 
            +
                  lambda { |value| value }, # string. should always be the last one
         | 
| 29 | 
            +
                ]
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                def get_value(filepath)
         | 
| 32 | 
            +
                  fullpath = File.join(@workdir, filepath)
         | 
| 33 | 
            +
                  raise FileNotFound, "key #{filepath} not found. Expecting to find it in #{fullpath}" unless File.exist? fullpath
         | 
| 34 | 
            +
                  content = File.read(fullpath).chomp
         | 
| 35 | 
            +
                  return coerce(content)
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                def coerce(value)
         | 
| 39 | 
            +
                  # assume a float first
         | 
| 40 | 
            +
                  DESERIALIZERS.each do |deseralizer|
         | 
| 41 | 
            +
                    result = deseralizer.call(value)
         | 
| 42 | 
            +
                    return result unless result == NIL_VALUE
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  raise UnsupportedValueType, "'#{value}' is unsupported type"
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                def build_hash_from_list(list)
         | 
| 49 | 
            +
                  result = []
         | 
| 50 | 
            +
                  list.each do |item|
         | 
| 51 | 
            +
                    value = get_value(item)
         | 
| 52 | 
            +
                    result << build_hash(item, value)
         | 
| 53 | 
            +
                  end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                  return result
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                def build_object_from_list(list)
         | 
| 59 | 
            +
                  Object.send(:remove_const, Konfig.configuration.namespace) if Object.const_defined?(Konfig.configuration.namespace)
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  full_hash = {}
         | 
| 62 | 
            +
                  hash_list = build_hash_from_list(list)
         | 
| 63 | 
            +
                  hash_list.each do |item|
         | 
| 64 | 
            +
                    full_hash.deep_merge!(item)
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  Object.const_set(Konfig.configuration.namespace, full_hash.to_dot)
         | 
| 68 | 
            +
                end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                def build_hash(key, value)
         | 
| 71 | 
            +
                  parts = key.split(Konfig.configuration.delimiter)
         | 
| 72 | 
            +
                  parts << value
         | 
| 73 | 
            +
                  parts.reverse.inject { |a, n| { n => a } }
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
            end
         | 
| @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            require_relative "config_provider"
         | 
| 2 | 
            +
            require "yaml"
         | 
| 3 | 
            +
            require "hash_dot"
         | 
| 4 | 
            +
            require "erb"
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module Konfig
         | 
| 7 | 
            +
              class YamlProvider < ConfigProvider
         | 
| 8 | 
            +
                attr_reader :file
         | 
| 9 | 
            +
                attr_reader :raw_settings
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def initialize(workdir:, filename: Konfig.configuration.default_config_file)
         | 
| 12 | 
            +
                  super(mode: :yaml, workdir: workdir)
         | 
| 13 | 
            +
                  @file = File.join(workdir, filename)
         | 
| 14 | 
            +
                  raise FileNotFound, "#{@file} not found" unless File.exists? @file
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def load
         | 
| 18 | 
            +
                  content = YAML.load(ERB.new(IO.read(@file)).result) if @file and File.exist?(@file)
         | 
| 19 | 
            +
                  content ||= {}
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                  @raw_settings = content.to_dot
         | 
| 22 | 
            +
                  Object.send(:remove_const, Konfig.configuration.namespace) if Object.const_defined?(Konfig.configuration.namespace)
         | 
| 23 | 
            +
                  Object.const_set(Konfig.configuration.namespace, @raw_settings)
         | 
| 24 | 
            +
                rescue Psych::SyntaxError => exc
         | 
| 25 | 
            +
                  raise "YAML syntax error occurred while parsing #{@file}. " \
         | 
| 26 | 
            +
                        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
         | 
| 27 | 
            +
                        "Error: #{exc.message}"
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,203 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: rb-konfig
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Khash Sajadi
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: exe
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2019-06-27 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: hash_dot
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '2.4'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '2.4'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: activesupport
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: bundler
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - "~>"
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '2.0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - "~>"
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '2.0'
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: rake
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - "~>"
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: '10.0'
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - "~>"
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: '10.0'
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: factory_bot
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - "~>"
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '5.0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - "~>"
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '5.0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: rspec
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - "~>"
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '3.8'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - "~>"
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '3.8'
         | 
| 97 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 98 | 
            +
              name: rubocop
         | 
| 99 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 100 | 
            +
                requirements:
         | 
| 101 | 
            +
                - - "~>"
         | 
| 102 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 103 | 
            +
                    version: '0.69'
         | 
| 104 | 
            +
              type: :development
         | 
| 105 | 
            +
              prerelease: false
         | 
| 106 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 107 | 
            +
                requirements:
         | 
| 108 | 
            +
                - - "~>"
         | 
| 109 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 110 | 
            +
                    version: '0.69'
         | 
| 111 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 112 | 
            +
              name: rubocop-performance
         | 
| 113 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 114 | 
            +
                requirements:
         | 
| 115 | 
            +
                - - "~>"
         | 
| 116 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 117 | 
            +
                    version: '1.3'
         | 
| 118 | 
            +
              type: :development
         | 
| 119 | 
            +
              prerelease: false
         | 
| 120 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
                requirements:
         | 
| 122 | 
            +
                - - "~>"
         | 
| 123 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 124 | 
            +
                    version: '1.3'
         | 
| 125 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 126 | 
            +
              name: rufo
         | 
| 127 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 128 | 
            +
                requirements:
         | 
| 129 | 
            +
                - - "~>"
         | 
| 130 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 131 | 
            +
                    version: '0.7'
         | 
| 132 | 
            +
              type: :development
         | 
| 133 | 
            +
              prerelease: false
         | 
| 134 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 135 | 
            +
                requirements:
         | 
| 136 | 
            +
                - - "~>"
         | 
| 137 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 138 | 
            +
                    version: '0.7'
         | 
| 139 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 140 | 
            +
              name: byebug
         | 
| 141 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 142 | 
            +
                requirements:
         | 
| 143 | 
            +
                - - ">="
         | 
| 144 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 145 | 
            +
                    version: '0'
         | 
| 146 | 
            +
              type: :development
         | 
| 147 | 
            +
              prerelease: false
         | 
| 148 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 149 | 
            +
                requirements:
         | 
| 150 | 
            +
                - - ">="
         | 
| 151 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 152 | 
            +
                    version: '0'
         | 
| 153 | 
            +
            description: 
         | 
| 154 | 
            +
            email:
         | 
| 155 | 
            +
            - khash@sajadi.co.uk
         | 
| 156 | 
            +
            executables: []
         | 
| 157 | 
            +
            extensions: []
         | 
| 158 | 
            +
            extra_rdoc_files: []
         | 
| 159 | 
            +
            files:
         | 
| 160 | 
            +
            - ".gitignore"
         | 
| 161 | 
            +
            - ".rspec"
         | 
| 162 | 
            +
            - ".rubocop.yml"
         | 
| 163 | 
            +
            - ".ruby-gemset"
         | 
| 164 | 
            +
            - ".ruby-version"
         | 
| 165 | 
            +
            - Gemfile
         | 
| 166 | 
            +
            - Gemfile.lock
         | 
| 167 | 
            +
            - README.md
         | 
| 168 | 
            +
            - Rakefile
         | 
| 169 | 
            +
            - bin/console
         | 
| 170 | 
            +
            - bin/setup
         | 
| 171 | 
            +
            - konfig.gemspec
         | 
| 172 | 
            +
            - lib/konfig.rb
         | 
| 173 | 
            +
            - lib/konfig/config_provider.rb
         | 
| 174 | 
            +
            - lib/konfig/directory_provider.rb
         | 
| 175 | 
            +
            - lib/konfig/errors.rb
         | 
| 176 | 
            +
            - lib/konfig/version.rb
         | 
| 177 | 
            +
            - lib/konfig/yaml_provider.rb
         | 
| 178 | 
            +
            homepage: https://github.com/khash/konfig
         | 
| 179 | 
            +
            licenses: []
         | 
| 180 | 
            +
            metadata:
         | 
| 181 | 
            +
              homepage_uri: https://github.com/khash/konfig
         | 
| 182 | 
            +
              source_code_uri: https://github.com/khash/konfig
         | 
| 183 | 
            +
            post_install_message: 
         | 
| 184 | 
            +
            rdoc_options: []
         | 
| 185 | 
            +
            require_paths:
         | 
| 186 | 
            +
            - lib
         | 
| 187 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 188 | 
            +
              requirements:
         | 
| 189 | 
            +
              - - ">="
         | 
| 190 | 
            +
                - !ruby/object:Gem::Version
         | 
| 191 | 
            +
                  version: '0'
         | 
| 192 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 193 | 
            +
              requirements:
         | 
| 194 | 
            +
              - - ">="
         | 
| 195 | 
            +
                - !ruby/object:Gem::Version
         | 
| 196 | 
            +
                  version: '0'
         | 
| 197 | 
            +
            requirements: []
         | 
| 198 | 
            +
            rubyforge_project: 
         | 
| 199 | 
            +
            rubygems_version: 2.7.6.2
         | 
| 200 | 
            +
            signing_key: 
         | 
| 201 | 
            +
            specification_version: 4
         | 
| 202 | 
            +
            summary: Konfig is a Kubernetes friendly Rails configuration gem
         | 
| 203 | 
            +
            test_files: []
         |