complex_config 0.6.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2aef957d9266badac65636ce774d6bc8d222d7e
4
- data.tar.gz: ad3aa4f8783e6aa8128323cdfdd0107bd564694a
3
+ metadata.gz: d74e24dc69a917bf0f1e11a3d8a93238c11de4be
4
+ data.tar.gz: d4c8f847392f993b03097ce58c3ea9d96ac8e5c7
5
5
  SHA512:
6
- metadata.gz: 98d267628da2ba8177601e939821be6cd2c137e677654288138863b2e8025127bad30160d1e1ee6595285fc257fabcc8c7540c19689e4439f84d59ec3d871d84
7
- data.tar.gz: 18460dbc5a63408eeeea56c43b93827eaca6bab6a020617a018908ce769c78c29deb787bfc8051d2c8f1726b2d2e83d50d26c8fe3f38af47a128691a06f2e9f8
6
+ metadata.gz: a9412892436dd53c2fa3d2570825b64acc947bb1c78b583b99e61e84747b4fb88077e1704c1f06c0a02eb112112cff911a90de5e7d456015365004a93a08445d
7
+ data.tar.gz: 0a56013a9ea6a9afcb7aa194f689bf3ae7a05d1d6972e64837506a472f78a8217fab40ee8a6a084b30b0bcc337d30aad2b4262b651816fac59a8e1a68f30543e
data/README.md CHANGED
@@ -143,6 +143,12 @@ Here is the `ComplexConfig::Plugins::MONEY` plugin for example:
143
143
  end
144
144
 
145
145
  ## Changes
146
+
147
+ * 2016-07-15 Release 0.7.0
148
+ * Slim down `ComplexConfig::Settings` interface to avoid clashes with methods
149
+ mixed into `Object` class or `Enumerable#instance_methods`
150
+ * 2016-07-15 Release 0.6.0
151
+ * Depend on mize gem for caching.
146
152
  * 2016-06-23 Release 0.5.2
147
153
  * Resolve index access via the plugin code path, so foo.bar and foo[:bar]
148
154
  have the same result for a plugin key.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: complex_config 0.6.0 ruby lib
2
+ # stub: complex_config 0.7.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "complex_config"
6
- s.version = "0.6.0"
6
+ s.version = "0.7.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Florian Frank"]
11
- s.date = "2016-07-15"
11
+ s.date = "2016-07-18"
12
12
  s.description = "This library allows you to access configuration files via a simple interface"
13
13
  s.email = "flori@ping.de"
14
14
  s.extra_rdoc_files = ["README.md", "lib/complex_config.rb", "lib/complex_config/config.rb", "lib/complex_config/errors.rb", "lib/complex_config/plugins.rb", "lib/complex_config/plugins/enable.rb", "lib/complex_config/plugins/money.rb", "lib/complex_config/plugins/uri.rb", "lib/complex_config/provider.rb", "lib/complex_config/proxy.rb", "lib/complex_config/railtie.rb", "lib/complex_config/rude.rb", "lib/complex_config/settings.rb", "lib/complex_config/shortcuts.rb", "lib/complex_config/version.rb"]
@@ -10,5 +10,3 @@ require 'complex_config/settings'
10
10
  require 'complex_config/config'
11
11
  require 'complex_config/provider'
12
12
  defined? Rails and require 'complex_config/railtie'
13
-
14
-
@@ -2,7 +2,8 @@ require 'uri'
2
2
 
3
3
  module ComplexConfig::Plugins
4
4
  URI = -> id do
5
- if url = id.to_s.sub(/uri\z/, 'url') and url = ask_and_send(url)
5
+ url = id.to_s
6
+ if url.sub!(/uri\z/, 'url') and url = ask_and_send(url)
6
7
  ::URI.parse(url)
7
8
  else
8
9
  skip
@@ -2,12 +2,37 @@ require 'json'
2
2
  require 'tins/xt/ask_and_send'
3
3
  require 'tins/thread_local'
4
4
 
5
- class ComplexConfig::Settings < JSON::GenericObject
6
- def self.[](*a)
7
- from_hash(*a)
8
- end
5
+ class ComplexConfig::Settings < BasicObject
6
+ include ::Kernel
7
+ include ::Tins::AskAndSend
9
8
 
10
9
  class << self
10
+ def [](*a)
11
+ from_hash(*a)
12
+ end
13
+
14
+ def from_hash(object)
15
+ case
16
+ when object.respond_to?(:to_hash)
17
+ result = new
18
+ object.to_hash.each do |key, value|
19
+ result[key] = from_hash(value)
20
+ end
21
+ result
22
+ when object.respond_to?(:to_ary)
23
+ object.to_ary.map { |a| from_hash(a) }
24
+ else
25
+ object
26
+ end
27
+ end
28
+
29
+ def build(name, hash)
30
+ name.nil? or self.name_prefix = name.to_sym
31
+ from_hash(hash)
32
+ ensure
33
+ self.name_prefix = nil
34
+ end
35
+
11
36
  extend Tins::ThreadLocal
12
37
 
13
38
  thread_local :name_prefix
@@ -15,34 +40,39 @@ class ComplexConfig::Settings < JSON::GenericObject
15
40
 
16
41
  attr_accessor :name_prefix
17
42
 
18
- def self.build(name, hash)
19
- name.nil? or self.name_prefix = name.to_sym
20
- self[hash]
21
- ensure
22
- self.name_prefix = nil
43
+ def initialize(hash = nil)
44
+ self.name_prefix = self.class.name_prefix
45
+ @table = {}
46
+ if hash
47
+ hash.each_pair do |k, v|
48
+ k = k.to_sym
49
+ @table[k] = v
50
+ end
51
+ end
23
52
  end
24
53
 
25
- def initialize(*)
26
- self.name_prefix = self.class.name_prefix
54
+ def initialize_copy(orig)
27
55
  super
56
+ @table = @table.dup
57
+ self
28
58
  end
29
59
 
30
60
  def attribute_set?(name)
31
- table.key?(name.to_sym)
61
+ @table.key?(name.to_sym)
32
62
  end
33
63
 
34
64
  def attribute_names
35
- table.keys
65
+ @table.keys
36
66
  end
37
67
 
38
68
  def attribute_values
39
- table.values
69
+ @table.values
40
70
  end
41
71
 
42
72
  def to_h
43
73
  table_enumerator.each_with_object({}) do |(k, v), h|
44
74
  h[k] =
45
- if Array === v
75
+ if ::Array === v
46
76
  v.to_ary.map { |x| (x.ask_and_send(:to_h) rescue x) || x }
47
77
  elsif v.respond_to?(:to_h)
48
78
  v.ask_and_send(:to_h) rescue v
@@ -52,6 +82,10 @@ class ComplexConfig::Settings < JSON::GenericObject
52
82
  end
53
83
  end
54
84
 
85
+ def size
86
+ each.count
87
+ end
88
+
55
89
  def to_s(pair_sep: ' = ', path_sep: ?.)
56
90
  pathes(path_sep: path_sep).inject('') do |result, (path, value)|
57
91
  result << "#{path}#{pair_sep}#{value.inspect}\n"
@@ -62,17 +96,17 @@ class ComplexConfig::Settings < JSON::GenericObject
62
96
  hash.each do |key, value|
63
97
  path = prefix.empty? ? key.to_s : "#{prefix}#{path_sep}#{key}"
64
98
  case value
65
- when ComplexConfig::Settings
99
+ when ::ComplexConfig::Settings
66
100
  pathes(
67
101
  value,
68
102
  path_sep: path_sep,
69
103
  prefix: path,
70
104
  result: result
71
105
  )
72
- when Array
106
+ when ::Array
73
107
  value.each_with_index do |v, i|
74
108
  sub_path = path + "[#{i}]"
75
- if ComplexConfig::Settings === v
109
+ if ::ComplexConfig::Settings === v
76
110
  pathes(
77
111
  v,
78
112
  path_sep: path_sep,
@@ -90,12 +124,16 @@ class ComplexConfig::Settings < JSON::GenericObject
90
124
  result
91
125
  end
92
126
 
127
+ alias inspect to_s
93
128
 
94
- def to_ary(*a, &b)
95
- table_enumerator.__send__(:to_a, *a, &b)
129
+ def pretty_print(q)
130
+ q.text inspect
96
131
  end
97
132
 
98
- alias inspect to_s
133
+ def freeze
134
+ @table.freeze
135
+ super
136
+ end
99
137
 
100
138
  def deep_freeze
101
139
  table_enumerator.each do |_, v|
@@ -104,26 +142,34 @@ class ComplexConfig::Settings < JSON::GenericObject
104
142
  freeze
105
143
  end
106
144
 
107
- alias index []
108
-
109
145
  def [](name)
110
146
  if !attribute_set?(name) and
111
- value = ComplexConfig::Provider.apply_plugins(self, name)
147
+ value = ::ComplexConfig::Provider.apply_plugins(self, name)
112
148
  then
113
149
  value
114
150
  else
115
- index(name)
151
+ @table[name.to_sym]
116
152
  end
117
153
  end
118
154
 
155
+ def []=(name, value)
156
+ @table[name.to_sym] = value
157
+ end
158
+
159
+ def each(&block)
160
+ table_enumerator.each(&block)
161
+ end
162
+
119
163
  private
120
164
 
165
+ attr_reader :table
166
+
121
167
  def table_enumerator
122
- table.enum_for(:each)
168
+ @table.enum_for(:each)
123
169
  end
124
170
 
125
171
  def respond_to_missing?(id, include_private = false)
126
- id =~ /\?\z/ || super
172
+ id =~ /\?\z/ || attribute_set?(id)
127
173
  end
128
174
 
129
175
  def skip
@@ -135,20 +181,18 @@ class ComplexConfig::Settings < JSON::GenericObject
135
181
  when id =~ /\?\z/
136
182
  begin
137
183
  public_send $`.to_sym, *a, &b
138
- rescue ComplexConfig::AttributeMissing
184
+ rescue ::ComplexConfig::AttributeMissing
139
185
  nil
140
186
  end
141
187
  when id =~ /=\z/
142
- super
143
- when value = ComplexConfig::Provider.apply_plugins(self, id)
188
+ @table[$`.to_sym] = a.first
189
+ when value = ::ComplexConfig::Provider.apply_plugins(self, id)
144
190
  value
145
191
  else
146
192
  if attribute_set?(id)
147
- super
148
- elsif table.respond_to?(id)
149
- table.__send__(id, *a, &b)
193
+ @table[id]
150
194
  else
151
- raise ComplexConfig::AttributeMissing, "no attribute named #{id.inspect}"
195
+ raise ::ComplexConfig::AttributeMissing, "no attribute named #{id.inspect}"
152
196
  end
153
197
  end
154
198
  end
@@ -1,6 +1,6 @@
1
1
  module ComplexConfig
2
2
  # ComplexConfig version
3
- VERSION = '0.6.0'
3
+ VERSION = '0.7.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -11,7 +11,13 @@ RSpec.describe ComplexConfig::Provider do
11
11
 
12
12
  context 'plugin' do
13
13
  let :plugin do
14
- -> id { __send__ id }
14
+ -> id {
15
+ if id == :evaluate_plugin
16
+ :evaluated
17
+ else
18
+ skip
19
+ end
20
+ }
15
21
  end
16
22
 
17
23
  let :setting do
@@ -28,8 +34,8 @@ RSpec.describe ComplexConfig::Provider do
28
34
 
29
35
  it 'can apply plugins' do
30
36
  provider.add_plugin plugin
31
- expect(setting).to receive(:foo).and_return :bar
32
- expect(provider.apply_plugins(setting, :foo)).to eq :bar
37
+ allow(setting).to receive(:skip).and_throw :skip
38
+ expect(provider.apply_plugins(setting, :evaluate_plugin)).to eq :evaluated
33
39
  end
34
40
  end
35
41
 
@@ -17,6 +17,40 @@ RSpec.describe ComplexConfig::Settings do
17
17
  ]
18
18
  end
19
19
 
20
+ it 'can be initialized with a hash' do
21
+ s = ComplexConfig::Settings.new(foo: 'bar')
22
+ expect(s.foo).to eq 'bar'
23
+ end
24
+
25
+ it 'can be duped and changed' do
26
+ s = ComplexConfig::Settings.new(foo: 'bar')
27
+ t = s.dup
28
+ expect(s.foo).to eq 'bar'
29
+ t.foo = 'baz'
30
+ expect(s.foo).to eq 'bar'
31
+ expect(t.foo).to eq 'baz'
32
+ end
33
+
34
+ it 'can set its attributes' do
35
+ expect {
36
+ settings.blub = 'blub'
37
+ }.to change {
38
+ settings.blub?
39
+ }.from(nil).to('blub')
40
+ end
41
+
42
+ it 'has a size' do
43
+ expect(settings.size).to eq 1
44
+ end
45
+
46
+ it 'can set its attributes via index method' do
47
+ expect {
48
+ settings['blub'] = 'blub'
49
+ }.to change {
50
+ settings.blub?
51
+ }.from(nil).to('blub')
52
+ end
53
+
20
54
  it "can return an attribute's value" do
21
55
  expect(settings.foo.bar.baz).to eq true
22
56
  end
@@ -64,9 +98,10 @@ ary[2] = 3
64
98
  EOT
65
99
  end
66
100
 
67
- it 'can be array like (first level only), so puts still works' do
68
- expect(settings).to respond_to :to_ary
69
- expect(settings.to_ary).to eq [[:foo, settings.foo]]
101
+ it 'can be pretty printed' do
102
+ q = double
103
+ expect(q).to receive(:text).with("foo.bar.baz = true\nfoo.qux = \"quux\"\n")
104
+ settings.pretty_print(q)
70
105
  end
71
106
 
72
107
  it 'raises exception if expected attribute is missing' do
@@ -89,9 +124,4 @@ EOT
89
124
  settings = ComplexConfig::Settings[zip: 'a string']
90
125
  expect(settings.zip).to eq 'a string'
91
126
  end
92
-
93
- it 'zips the hash if zip was not set' do
94
- settings = ComplexConfig::Settings[not_zip: 'a string']
95
- expect(settings.zip([1])).to eq [ [ [ :not_zip, 'a string' ], 1 ] ]
96
- end
97
127
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: complex_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-15 00:00:00.000000000 Z
11
+ date: 2016-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar