anyway_config 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efb55eb76ff8fe050d265f9f73af34e1ca813134
4
- data.tar.gz: ea56b66f8d7eb4b6d63a4fb068c1631feb6d49e9
3
+ metadata.gz: f61c3efab21364fb9d6f502183bcf2a15edd5207
4
+ data.tar.gz: '08ca86bbc7687a451f035051d9584a7d8b76c955'
5
5
  SHA512:
6
- metadata.gz: a282acf7111e9dab9fbcc995f97604ded970617b0fdf8c5db7b5db612911253bf6aea75cbc7f1e21c383237aab6e45798a9c159d69c4bccf21a54a05af5d2763
7
- data.tar.gz: 66aa6cefebd02087c6926927d805803a7885399b10b31e729e9d33cf7a8f8722e3dcb0cf1c453f580aaf8394b21d4325503a73afb4282b8f6ed2e6d1156aa378
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.3
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
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in anyway_config.gemspec
6
6
  gem 'sqlite3'
7
+ gem 'pry-byebug'
7
8
  gemspec
8
9
 
9
10
  local_gemfile = "#{File.dirname(__FILE__)}/Gemfile.local"
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  lib = File.expand_path('../lib', __FILE__)
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 = hargs.deep_dup
21
- defaults.stringify_keys!
22
- @config_attributes = args + defaults.keys
23
- attr_accessor(*@config_attributes)
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
@@ -22,7 +22,7 @@ module Anyway
22
22
  end
23
23
 
24
24
  def stringify_keys!
25
- keys.each do |key|
25
+ keys.each do |key| # rubocop: disable Performance/HashEachMethods
26
26
  value = delete(key)
27
27
  value.stringify_keys! if value.is_a?(::Hash)
28
28
  self[key.to_s] = value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anyway # :nodoc:
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
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
- describe "config with name" do
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
- describe "config for name" do
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
@@ -61,7 +61,7 @@ describe Anyway::Config do
61
61
 
62
62
  let(:conf) { empty_config_class.new }
63
63
 
64
- specify { expect(conf.config_name).to be_nil }
64
+ specify { expect { conf.config_name }.to raise_error(ArgumentError) }
65
65
  end
66
66
 
67
67
  context "loading from default path" do
@@ -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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SmallConfig < Anyway::Config # :nodoc:
4
+ config_name :small
5
+ attr_config :meta,
6
+ :data
7
+ 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.0.0
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-20 00:00:00.000000000 Z
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.4
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