anyway_config 1.0.0 → 1.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 +4 -4
- data/.rubocop.yml +7 -1
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -0
- data/anyway_config.gemspec +0 -1
- data/lib/anyway/config.rb +19 -4
- data/lib/anyway/ext/deep_freeze.rb +26 -0
- data/lib/anyway/ext/hash.rb +1 -1
- data/lib/anyway/version.rb +1 -1
- data/spec/config_spec.rb +63 -2
- data/spec/config_spec_norails.rb +1 -1
- data/spec/ext/deep_freeze_spec.rb +32 -0
- data/spec/support/small_config.rb +7 -0
- metadata +6 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: f61c3efab21364fb9d6f502183bcf2a15edd5207
         | 
| 4 | 
            +
              data.tar.gz: '08ca86bbc7687a451f035051d9584a7d8b76c955'
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: e665ffd00ff4b055cfc4eaece0f9aec66e69638fd7a18b4151d740c6e434678764c8bbbbaf2852238ad2c2f6551826e9d619cf8d592ec7f2d974763c44507b1a
         | 
| 7 | 
            +
              data.tar.gz: f259801f4db6626cc0356c07439dda350718c08073db6e4a40c891e8c45820dde4eb3f87812478936902eee2cca7def1859e0d13a10740912fb9ca7af2f65c21
         | 
    
        data/.rubocop.yml
    CHANGED
    
    | @@ -13,7 +13,7 @@ AllCops: | |
| 13 13 | 
             
                - 'vendor/**/*'
         | 
| 14 14 | 
             
              DisplayCopNames: true
         | 
| 15 15 | 
             
              StyleGuideCopsOnly: false
         | 
| 16 | 
            -
              TargetRubyVersion: 2. | 
| 16 | 
            +
              TargetRubyVersion: 2.2
         | 
| 17 17 |  | 
| 18 18 | 
             
            Style/Documentation:
         | 
| 19 19 | 
             
              Exclude:
         | 
| @@ -36,3 +36,9 @@ Metrics/BlockLength: | |
| 36 36 |  | 
| 37 37 | 
             
            Metrics/LineLength:
         | 
| 38 38 | 
             
              Max: 100
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            Bundler/OrderedGems:
         | 
| 41 | 
            +
              Enabled: false
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            Style/MutableConstant:
         | 
| 44 | 
            +
              Enabled: false
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,5 +1,13 @@ | |
| 1 1 | 
             
            # Change log
         | 
| 2 2 |  | 
| 3 | 
            +
            ## 1.1.0 (2017-10-06)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            - Add `#to_h` method. ([@palkan][])
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            See [#4](https://github.com/palkan/anyway_config/issues/4).
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            - Make it possible to extend configuration parameters. ([@palkan][])
         | 
| 10 | 
            +
             | 
| 3 11 | 
             
            ## 1.0.0 (2017-06-20)
         | 
| 4 12 |  | 
| 5 13 | 
             
            - Lazy load and parse ENV configurtaion. ([@palkan][])
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/anyway_config.gemspec
    CHANGED
    
    
    
        data/lib/anyway/config.rb
    CHANGED
    
    | @@ -2,11 +2,13 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            require 'anyway/ext/class'
         | 
| 4 4 | 
             
            require 'anyway/ext/deep_dup'
         | 
| 5 | 
            +
            require 'anyway/ext/deep_freeze'
         | 
| 5 6 | 
             
            require 'anyway/ext/hash'
         | 
| 6 7 |  | 
| 7 8 | 
             
            module Anyway # :nodoc:
         | 
| 8 9 | 
             
              using Anyway::Ext::Class
         | 
| 9 10 | 
             
              using Anyway::Ext::DeepDup
         | 
| 11 | 
            +
              using Anyway::Ext::DeepFreeze
         | 
| 10 12 | 
             
              using Anyway::Ext::Hash
         | 
| 11 13 |  | 
| 12 14 | 
             
              # Base config class
         | 
| @@ -17,10 +19,16 @@ module Anyway # :nodoc: | |
| 17 19 | 
             
                  attr_reader :defaults, :config_attributes
         | 
| 18 20 |  | 
| 19 21 | 
             
                  def attr_config(*args, **hargs)
         | 
| 20 | 
            -
                    @defaults  | 
| 21 | 
            -
                     | 
| 22 | 
            -
             | 
| 23 | 
            -
                     | 
| 22 | 
            +
                    @defaults ||= {}
         | 
| 23 | 
            +
                    @config_attributes ||= []
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    new_defaults = hargs.deep_dup
         | 
| 26 | 
            +
                    new_defaults.stringify_keys!
         | 
| 27 | 
            +
                    defaults.merge! new_defaults
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    new_keys = (args + new_defaults.keys) - config_attributes
         | 
| 30 | 
            +
                    @config_attributes += new_keys
         | 
| 31 | 
            +
                    attr_accessor(*new_keys)
         | 
| 24 32 | 
             
                  end
         | 
| 25 33 |  | 
| 26 34 | 
             
                  def config_name(val = nil)
         | 
| @@ -44,6 +52,7 @@ module Anyway # :nodoc: | |
| 44 52 |  | 
| 45 53 | 
             
                def initialize(config_name = nil, do_load = true)
         | 
| 46 54 | 
             
                  @config_name = config_name || self.class.config_name
         | 
| 55 | 
            +
                  raise ArgumentError, "Config name is missing" unless @config_name
         | 
| 47 56 | 
             
                  load if do_load
         | 
| 48 57 | 
             
                end
         | 
| 49 58 |  | 
| @@ -88,6 +97,12 @@ module Anyway # :nodoc: | |
| 88 97 | 
             
                  config
         | 
| 89 98 | 
             
                end
         | 
| 90 99 |  | 
| 100 | 
            +
                def to_h
         | 
| 101 | 
            +
                  self.class.config_attributes.each_with_object({}) do |key, obj|
         | 
| 102 | 
            +
                    obj[key.to_sym] = send(key)
         | 
| 103 | 
            +
                  end.deep_dup.deep_freeze
         | 
| 104 | 
            +
                end
         | 
| 105 | 
            +
             | 
| 91 106 | 
             
                private
         | 
| 92 107 |  | 
| 93 108 | 
             
                def set_value(key, val)
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Anyway
         | 
| 4 | 
            +
              module Ext
         | 
| 5 | 
            +
                # Add #deep_freeze to hashes and arrays
         | 
| 6 | 
            +
                module DeepFreeze
         | 
| 7 | 
            +
                  refine ::Hash do
         | 
| 8 | 
            +
                    def deep_freeze
         | 
| 9 | 
            +
                      freeze
         | 
| 10 | 
            +
                      each_value do |value|
         | 
| 11 | 
            +
                        value.deep_freeze if value.is_a?(::Hash) || value.is_a?(::Array)
         | 
| 12 | 
            +
                      end
         | 
| 13 | 
            +
                    end
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  refine ::Array do
         | 
| 17 | 
            +
                    def deep_freeze
         | 
| 18 | 
            +
                      freeze
         | 
| 19 | 
            +
                      each do |value|
         | 
| 20 | 
            +
                        value.deep_freeze if value.is_a?(::Hash) || value.is_a?(::Array)
         | 
| 21 | 
            +
                      end
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
    
        data/lib/anyway/ext/hash.rb
    CHANGED
    
    
    
        data/lib/anyway/version.rb
    CHANGED
    
    
    
        data/spec/config_spec.rb
    CHANGED
    
    | @@ -6,7 +6,7 @@ describe Anyway::Config do | |
| 6 6 | 
             
              let(:conf) { CoolConfig.new }
         | 
| 7 7 | 
             
              let(:test_conf) { Anyway::TestConfig.new }
         | 
| 8 8 |  | 
| 9 | 
            -
               | 
| 9 | 
            +
              context "config with name" do
         | 
| 10 10 | 
             
                before(:each) do
         | 
| 11 11 | 
             
                  ENV.delete_if { |var| var =~ /^(cool|anyway)_/i }
         | 
| 12 12 | 
             
                end
         | 
| @@ -26,6 +26,25 @@ describe Anyway::Config do | |
| 26 26 | 
             
                  expect(conf).to respond_to(:user)
         | 
| 27 27 | 
             
                end
         | 
| 28 28 |  | 
| 29 | 
            +
                describe "#to_h" do
         | 
| 30 | 
            +
                  subject(:config) { CoolConfig.new }
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  it "returns deeply frozen hash" do
         | 
| 33 | 
            +
                    hashed = config.to_h
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                    expect(hashed).to be_a(Hash)
         | 
| 36 | 
            +
                    expect(hashed).to be_frozen
         | 
| 37 | 
            +
                    expect(hashed[:user]).to be_frozen
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                  it "returns new hash every time" do
         | 
| 41 | 
            +
                    hashed = config.to_h
         | 
| 42 | 
            +
                    hashed2 = config.to_h
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                    expect(hashed).to be_eql(hashed2)
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 29 48 | 
             
                describe "load from files" do
         | 
| 30 49 | 
             
                  it "set defaults" do
         | 
| 31 50 | 
             
                    expect(conf.port).to eq 8080
         | 
| @@ -87,7 +106,7 @@ describe Anyway::Config do | |
| 87 106 | 
             
                end
         | 
| 88 107 | 
             
              end
         | 
| 89 108 |  | 
| 90 | 
            -
               | 
| 109 | 
            +
              context "config for name" do
         | 
| 91 110 | 
             
                before(:each) do
         | 
| 92 111 | 
             
                  ENV.delete_if { |var| var =~ /^myapp_/i }
         | 
| 93 112 | 
             
                end
         | 
| @@ -104,4 +123,46 @@ describe Anyway::Config do | |
| 104 123 | 
             
                  end
         | 
| 105 124 | 
             
                end
         | 
| 106 125 | 
             
              end
         | 
| 126 | 
            +
             | 
| 127 | 
            +
              context "config without defaults" do
         | 
| 128 | 
            +
                let(:conf) { SmallConfig.new }
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                it "works" do
         | 
| 131 | 
            +
                  expect(conf.meta).to be_nil
         | 
| 132 | 
            +
                  expect(conf.data).to be_nil
         | 
| 133 | 
            +
                end
         | 
| 134 | 
            +
              end
         | 
| 135 | 
            +
             | 
| 136 | 
            +
              context "when name is missing" do
         | 
| 137 | 
            +
                let(:config) do
         | 
| 138 | 
            +
                  Class.new(described_class)
         | 
| 139 | 
            +
                end
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                it "raises ArgumentError" do
         | 
| 142 | 
            +
                  expect { config.new }.to raise_error(ArgumentError)
         | 
| 143 | 
            +
                end
         | 
| 144 | 
            +
              end
         | 
| 145 | 
            +
             | 
| 146 | 
            +
              context "extending config" do
         | 
| 147 | 
            +
                let(:config) do
         | 
| 148 | 
            +
                  Class.new(described_class) do
         | 
| 149 | 
            +
                    config_name 'testo'
         | 
| 150 | 
            +
                    attr_config :test, debug: false
         | 
| 151 | 
            +
                  end
         | 
| 152 | 
            +
                end
         | 
| 153 | 
            +
             | 
| 154 | 
            +
                it "adds new params" do
         | 
| 155 | 
            +
                  old_config = config.new
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                  expect(old_config.debug).to eq false
         | 
| 158 | 
            +
                  expect(old_config.test).to be_nil
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                  config.attr_config new_param: 'a'
         | 
| 161 | 
            +
             | 
| 162 | 
            +
                  new_config = config.new
         | 
| 163 | 
            +
                  expect(new_config.debug).to eq false
         | 
| 164 | 
            +
                  expect(new_config.test).to be_nil
         | 
| 165 | 
            +
                  expect(new_config.new_param).to eq 'a'
         | 
| 166 | 
            +
                end
         | 
| 167 | 
            +
              end
         | 
| 107 168 | 
             
            end
         | 
    
        data/spec/config_spec_norails.rb
    CHANGED
    
    
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'spec_helper'
         | 
| 4 | 
            +
            require 'anyway/ext/deep_freeze'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            describe Anyway::Ext::DeepFreeze do
         | 
| 7 | 
            +
              using Anyway::Ext::DeepFreeze
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              it "freezes nested arrays and hashes", :aggregate_failures do
         | 
| 10 | 
            +
                source = {
         | 
| 11 | 
            +
                  a: 1,
         | 
| 12 | 
            +
                  b: 'hello',
         | 
| 13 | 
            +
                  c: {
         | 
| 14 | 
            +
                    id: 1,
         | 
| 15 | 
            +
                    list: [1, 2, { name: 'John' }]
         | 
| 16 | 
            +
                  },
         | 
| 17 | 
            +
                  d: [{ id: 1 }, { id: 2 }]
         | 
| 18 | 
            +
                }
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                dup = source.deep_freeze
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                expect(dup).to be_frozen
         | 
| 23 | 
            +
                expect(dup[:c]).to be_frozen
         | 
| 24 | 
            +
                expect(dup[:d]).to be_frozen
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                expect(dup[:c][:list]).to be_frozen
         | 
| 27 | 
            +
                expect(dup[:c][:list].last).to be_frozen
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                expect(dup[:d].first).to be_frozen
         | 
| 30 | 
            +
                expect(dup[:d].last).to be_frozen
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: anyway_config
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1. | 
| 4 | 
            +
              version: 1.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Vladimir Dementyev
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017-06 | 
| 11 | 
            +
            date: 2017-10-06 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: simplecov
         | 
| @@ -79,6 +79,7 @@ files: | |
| 79 79 | 
             
            - lib/anyway/env.rb
         | 
| 80 80 | 
             
            - lib/anyway/ext/class.rb
         | 
| 81 81 | 
             
            - lib/anyway/ext/deep_dup.rb
         | 
| 82 | 
            +
            - lib/anyway/ext/deep_freeze.rb
         | 
| 82 83 | 
             
            - lib/anyway/ext/hash.rb
         | 
| 83 84 | 
             
            - lib/anyway/rails/config.rb
         | 
| 84 85 | 
             
            - lib/anyway/version.rb
         | 
| @@ -97,9 +98,11 @@ files: | |
| 97 98 | 
             
            - spec/dummy/config/secrets.yml
         | 
| 98 99 | 
             
            - spec/env_spec.rb
         | 
| 99 100 | 
             
            - spec/ext/deep_dup_spec.rb
         | 
| 101 | 
            +
            - spec/ext/deep_freeze_spec.rb
         | 
| 100 102 | 
             
            - spec/spec_helper.rb
         | 
| 101 103 | 
             
            - spec/spec_norails_helper.rb
         | 
| 102 104 | 
             
            - spec/support/cool_config.rb
         | 
| 105 | 
            +
            - spec/support/small_config.rb
         | 
| 103 106 | 
             
            - spec/support/test_config.rb
         | 
| 104 107 | 
             
            homepage: http://github.com/palkan/anyway_config
         | 
| 105 108 | 
             
            licenses:
         | 
| @@ -121,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 121 124 | 
             
                  version: '0'
         | 
| 122 125 | 
             
            requirements: []
         | 
| 123 126 | 
             
            rubyforge_project: 
         | 
| 124 | 
            -
            rubygems_version: 2.6. | 
| 127 | 
            +
            rubygems_version: 2.6.13
         | 
| 125 128 | 
             
            signing_key: 
         | 
| 126 129 | 
             
            specification_version: 4
         | 
| 127 130 | 
             
            summary: Configuration DSL for Ruby libraries and applications
         |