configurate 0.1.0 → 0.2.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: b1c77311fcc9be9b8545a349cb8a9352fac266ba
4
- data.tar.gz: 23cc86a4bc3e0edad45c756eb77a27a97628cb3c
3
+ metadata.gz: 996d3e9be532044af0c5ab4597eb547661683c3b
4
+ data.tar.gz: 2e9b90ac7a188be0c8624fcd5e52619b7f267b53
5
5
  SHA512:
6
- metadata.gz: 0f9b04ce701034847ce8d337093a692bae175e75c8b68b30de0260bfe20520350f6d0112a0423df972d564321b23b4a91a35160d1186899fef1c3d2f61f64388
7
- data.tar.gz: f48dbe835f2cb4b2102d8090a1b030612b19f52a7b2b23d517eb6cbc7bc748304a6cac4a1a6826eb90367ad77e73e783735b7bb9be02901766c8a5ea1680bf43
6
+ metadata.gz: 0c51c9bc229cff96e092dee805cb4f4517cdc315831b79f65b46253687b692a2db562278bb7831178433ff2768d403a4eb7c7151c9859b8884dd8e1413b08157
7
+ data.tar.gz: e3af9cf48b009e97fe81cd7fcbee2d5e55586d6aff33a45be88b562d9115c69a4b1157af12d52d525dc6e06d866b49586ba04576b72613e37b1a9b39649c69c5
@@ -1,11 +1,11 @@
1
1
  -----BEGIN PGP SIGNATURE-----
2
2
  Version: GnuPG v2
3
3
 
4
- iQEcBAABAgAGBQJUJcgAAAoJEPNH4OtHrHDWwu0IAIPCkoGVvTACKyK1T3wq/1MY
5
- /Ea17MjRfBzNZ4vagFJd8LZAIf58ZFGYPiFhvZim26o7WVIf6BGUNDQUDC+CKAg6
6
- iFXfbPjXXjuB3v1mylZ81ZWcfkyClda2T95LHdsdJ52s/ciar2L1QvngM/MR1+Vr
7
- 5nmnBS9OFA3/OsQVJKWi1oKClfz4RLFCcI16Y3gyH9WrsLFlCr2McJ/lTr6U/D8x
8
- XLrUEcMMYWK7G7qc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=
10
- =4Tkj
4
+ iQEcBAABAgAGBQJULyg7AAoJEPNH4OtHrHDWgO0H/jLKCQPi2kguHzfJU/k4hII8
5
+ VITtYbNZb4ZBbHecgiam4mI+m5EvDCfQu3KEzSN3w06czUoVFW0uCABJkAqb43He
6
+ EvITE2z36GKFb8GqfA5EXnRPjmsGTvH8EnUG8DetTRNjFkERvNSi6t3RxgfNQ8yw
7
+ UFwtascrsmM5vjzCvgRlJ0cm1HA3LZFqoEyw/MovOLDviLTz1ECKqxNmaEULdhp9
8
+ Fb10e5Y9XalRbbF6wSOKWp06owiEcTVY/lqNOZBJ+ZIG1RQUD73yhAGW2b7bM6rg
9
+ tiBEAal1Tt6c5v5SvucCcMrKxd3R8CXCtwETQVFl4rxB4eYhSgx0PkFiQ3OPicY=
10
+ =hJkd
11
11
  -----END PGP SIGNATURE-----
@@ -1,3 +1,10 @@
1
+ # 0.2.0
2
+
3
+ * Dynamic provider listens to reset_dynamic! message and forgets all settings on it.
4
+ * Calls ending in ! call the providers directly.
5
+ * Added SettingPath#action?, remove is_ prefix from SettingPath methods.
6
+ * Add implicit converters to Proxy that call the explicit converters.
7
+
1
8
  # 0.1.0
2
9
 
3
10
  * Dynamic provider resolves nested assignments
data/README.md CHANGED
@@ -80,7 +80,7 @@ Ruby does not allow to metaprogram `false`, thus something like
80
80
  puts "yep" if Config.enable_stuff
81
81
  ```
82
82
 
83
- always outputs `yep`. The workaround is to append `.get` or `?` to get the
83
+ always outputs `yep`. The workaround is to append `.get`, or `?` to get the
84
84
  real value:
85
85
 
86
86
  ```ruby
@@ -156,14 +156,15 @@ the following optional parameters, as a hash:
156
156
 
157
157
  ### Configurate::Provider::Dynamic
158
158
 
159
- A provider which stores the first additonal parameter if the query string ends with an equal sign and can
160
- return it later. This is mainly useful for testing but can be useful to temporarily overide stuff
161
- too. To clarify a small example:
159
+ A provider which stores the first additional parameter if the query string ends with an equal sign and can
160
+ return it later. This is mainly useful for testing but can be useful to temporarily override stuff too. To clarify a small example:
162
161
 
163
162
  ```ruby
164
163
  Config.foo.bar # => nil
165
164
  Config.foo.bar = "baz"
166
165
  Config.foo.bar # => "baz"
166
+ Config.reset_dynamic!
167
+ Config.foo.bar # => nil
167
168
  ```
168
169
 
169
170
  ## Writing a provider
@@ -5,7 +5,7 @@ module Configurate
5
5
  def initialize
6
6
  @provider = []
7
7
  end
8
-
8
+
9
9
  # Adds a provider to the chain. Providers are tried in the order
10
10
  # they are added, so the order is important.
11
11
  #
@@ -17,11 +17,11 @@ module Configurate
17
17
  unless provider.respond_to?(:instance_methods) && provider.instance_methods.include?(:lookup)
18
18
  raise ArgumentError, "the given provider does not respond to lookup"
19
19
  end
20
-
20
+
21
21
  @provider << provider.new(*args)
22
22
  end
23
-
24
-
23
+
24
+
25
25
  # Tries all providers in the order they were added to provide a response
26
26
  # for setting.
27
27
  #
@@ -37,13 +37,13 @@ module Configurate
37
37
  return special_value_or_string(provider.lookup(setting.clone, *args))
38
38
  rescue SettingNotFoundError; end
39
39
  end
40
-
40
+
41
41
  nil
42
42
  end
43
43
  alias_method :[], :lookup
44
-
45
- private
46
-
44
+
45
+ private
46
+
47
47
  def special_value_or_string(value)
48
48
  if [TrueClass, FalseClass, NilClass, Array, Hash].include?(value.class)
49
49
  return value
@@ -2,14 +2,21 @@ module Configurate; module Provider
2
2
  # This provider knows nothing upon initialization, however if you access
3
3
  # a setting ending with +=+ and give one argument to that call it remembers
4
4
  # that setting, stripping the +=+ and will return it on the next call
5
- # without +=+.
5
+ # without +=+. Sending +reset_dynamic!+ to it will make it forget all
6
+ # settings. Also assigning nil will have the effect of it forgetting
7
+ # a setting.
6
8
  class Dynamic < Base
7
9
  def initialize
8
10
  @settings = {}
9
11
  end
10
12
 
11
13
  def lookup_path(setting_path, *args)
12
- if setting_path.is_setter? && args.length > 0
14
+ if setting_path.to_s == "reset_dynamic!"
15
+ @settings = {}
16
+ return
17
+ end
18
+
19
+ if setting_path.setter? && args.length > 0
13
20
  value = args.first
14
21
  value = value.get if value.respond_to?(:_proxy?) && value._proxy?
15
22
  *root, key = setting_path.to_a
@@ -18,40 +18,53 @@ module Configurate
18
18
  @lookup_chain = lookup_chain
19
19
  @setting_path = SettingPath.new
20
20
  end
21
-
21
+
22
22
  def !
23
23
  !target
24
24
  end
25
-
26
- [:!=, :==, :eql?].each do |method|
25
+
26
+ [:!=, :==, :eql?, :coerce].each do |method|
27
27
  define_method method do |other|
28
28
  target.public_send method, target_or_object(other)
29
29
  end
30
30
  end
31
-
31
+
32
+ {
33
+ :to_int => :to_i,
34
+ :to_hash => :to_h,
35
+ :to_str => :to_s,
36
+ :to_ary => :to_a
37
+ }.each do |method, converter|
38
+ define_method method do
39
+ value = target
40
+ return value.public_send converter if value.respond_to? converter
41
+ value.public_send method
42
+ end
43
+ end
44
+
32
45
  def _proxy?
33
46
  true
34
47
  end
35
-
48
+
36
49
  def respond_to? method, include_private=false
37
50
  method == :_proxy? || target_respond_to?(method, include_private)
38
51
  end
39
-
52
+
40
53
  def send *args, &block
41
54
  __send__(*args, &block)
42
55
  end
43
56
  alias_method :public_send, :send
44
-
57
+
45
58
  def method_missing setting, *args, &block
46
59
  return target.public_send(setting, *args, &block) if target_respond_to? setting
47
60
 
48
61
  @setting_path << setting
49
-
50
- return target(*args) if @setting_path.is_question_or_setter?
51
-
62
+
63
+ return target(*args) if @setting_path.question_action_or_setter?
64
+
52
65
  self
53
66
  end
54
-
67
+
55
68
  # Get the setting at the current path, if found.
56
69
  # (see LookupChain#lookup)
57
70
  def target *args
@@ -60,7 +73,7 @@ module Configurate
60
73
  @lookup_chain.lookup @setting_path, *args
61
74
  end
62
75
  alias_method :get, :target
63
-
76
+
64
77
  private
65
78
  COMMON_KEY_NAMES = [:key, :method]
66
79
 
@@ -19,17 +19,22 @@ module Configurate
19
19
  def_delegators :@path, :empty?, :length, :size, :hsh
20
20
 
21
21
  # Whether the current path looks like a question or setter method
22
- def is_question_or_setter?
23
- is_question? || is_setter?
22
+ def question_action_or_setter?
23
+ question? || action? || setter?
24
24
  end
25
25
 
26
26
  # Whether the current path looks like a question method
27
- def is_question?
27
+ def question?
28
28
  @path.last.to_s.end_with?("?")
29
29
  end
30
30
 
31
+ # Whether the current path looks like an action method
32
+ def action?
33
+ @path.last.to_s.end_with?("!")
34
+ end
35
+
31
36
  # Whether the current path looks like a setter method
32
- def is_setter?
37
+ def setter?
33
38
  @path.last.to_s.end_with?("=")
34
39
  end
35
40
 
@@ -61,7 +66,7 @@ module Configurate
61
66
  end
62
67
 
63
68
  def inspect
64
- "<SettingPath:#{object_id.to_s(16)} path=#{to_s}:#{@path.object_id.to_s(16)} setter=#{is_setter?} question=#{is_question?}>"
69
+ "<SettingPath:#{object_id.to_s(16)} path=#{to_s}:#{@path.object_id.to_s(16)} question=#{question?} action=#{action?} setter=#{setter?}>"
65
70
  end
66
71
 
67
72
  private
@@ -23,5 +23,11 @@ describe Configurate::Provider::Dynamic do
23
23
  subject.lookup_path Configurate::SettingPath.new(["find_me", "later="]), {"a" => "b"}
24
24
  expect(subject.lookup_path Configurate::SettingPath.new(["find_me", "later", "a"])).to eq "b"
25
25
  end
26
+
27
+ it "clears out all overrides on reset_dynamic!" do
28
+ subject.lookup_path Configurate::SettingPath.new(["find_me", "later="]), "there"
29
+ subject.lookup_path Configurate::SettingPath.new(["reset_dynamic!"])
30
+ expect(subject.lookup_path Configurate::SettingPath.new(["find_me", "later"]) ).to_not eq "there"
31
+ end
26
32
  end
27
33
  end
@@ -6,7 +6,7 @@ describe Configurate::Proxy do
6
6
 
7
7
  describe "in case statements" do
8
8
  it "acts like the target" do
9
- pending "If anyone knows a way to overwrite ===, please tell me :P"
9
+ pending "If anyone knows a sane way to overwrite Module#===, please tell me :P"
10
10
  result = case proxy
11
11
  when String
12
12
  "string"
@@ -23,6 +23,11 @@ describe Configurate::Proxy do
23
23
  proxy.method_missing(:enable?)
24
24
  end
25
25
 
26
+ it "calls #target if the method ends with a !" do
27
+ expect(lookup_chain).to receive(:lookup).and_return(false)
28
+ proxy.method_missing(:do_it!)
29
+ end
30
+
26
31
  it "calls #target if the method ends with a =" do
27
32
  expect(lookup_chain).to receive(:lookup).and_return(false)
28
33
  proxy.method_missing(:url=)
@@ -49,7 +54,7 @@ describe Configurate::Proxy do
49
54
  end
50
55
 
51
56
  describe "#target" do
52
- [:to_str, :to_s, :to_xml, :respond_to?, :present?, :!=, :eql?,
57
+ [:to_s, :to_xml, :respond_to?, :present?, :!=, :eql?,
53
58
  :each, :try, :size, :length, :count, :==, :=~, :gsub, :blank?, :chop,
54
59
  :start_with?, :end_with?].each do |method|
55
60
  it "is called for accessing #{method} on the proxy" do
@@ -72,5 +77,25 @@ describe Configurate::Proxy do
72
77
  it "returns nil if no setting is given" do
73
78
  expect(proxy.target).to be_nil
74
79
  end
80
+
81
+ it "converts to a string" do
82
+ allow(lookup_chain).to receive(:lookup).and_return("bar")
83
+ expect("foo"+proxy.something).to eq "foobar"
84
+ end
85
+
86
+ it "converts to a number" do
87
+ allow(lookup_chain).to receive(:lookup).and_return(1)
88
+ expect(2+proxy.something).to eq 3
89
+ end
90
+
91
+ it "converts to an array" do
92
+ allow(lookup_chain).to receive(:lookup).and_return([1, 2])
93
+ expect([:a, :b].zip(proxy.something)).to eq [[:a, 1], [:b, 2]]
94
+ end
95
+
96
+ it "converts to a hash" do
97
+ allow(lookup_chain).to receive(:lookup).and_return({:a => :b})
98
+ expect({c: :d}.merge(proxy.something)).to eq({:a => :b, :c => :d})
99
+ end
75
100
  end
76
101
  end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe Configurate::SettingPath do
4
4
  let(:normal_path) { described_class.new([:foo]) }
5
5
  let(:question_path) { described_class.new([:foo?]) }
6
+ let(:action_path) { described_class.new([:foo!]) }
6
7
  let(:setter_path) { described_class.new([:foo=]) }
7
8
  let(:long_path) { described_class.new(["foo", "bar?"]) }
8
9
 
@@ -14,26 +15,38 @@ describe Configurate::SettingPath do
14
15
  end
15
16
  end
16
17
 
17
- describe "#is_question?" do
18
+ describe "#question?" do
18
19
  context "with a question signature as setting" do
19
- subject { question_path.is_question? }
20
+ subject { question_path.question? }
20
21
  it { should be_truthy }
21
22
  end
22
23
 
23
24
  context "with a normal path as setting" do
24
- subject { normal_path.is_question? }
25
+ subject { normal_path.question? }
25
26
  it { should be_falsey }
26
27
  end
27
28
  end
28
29
 
29
- describe "#is_setter?" do
30
+ describe "#action?" do
31
+ context "with a action signature as setting" do
32
+ subject { action_path.action? }
33
+ it { should be_truthy }
34
+ end
35
+
36
+ context "with a normal path as setting" do
37
+ subject { normal_path.action? }
38
+ it { should be_falsey }
39
+ end
40
+ end
41
+
42
+ describe "#setter?" do
30
43
  context "with a setter signature as setting" do
31
- subject { setter_path.is_setter? }
44
+ subject { setter_path.setter? }
32
45
  it { should be_truthy }
33
46
  end
34
47
 
35
48
  context "with a normal path as setting" do
36
- subject { normal_path.is_setter? }
49
+ subject { normal_path.setter? }
37
50
  it { should be_falsey }
38
51
  end
39
52
  end
@@ -49,19 +62,24 @@ describe Configurate::SettingPath do
49
62
  end
50
63
 
51
64
 
52
- describe "#is_question_or_setter?" do
65
+ describe "#question_action_or_setter?" do
53
66
  context "with a question signature as setting" do
54
- subject { question_path.is_question_or_setter? }
67
+ subject { question_path.question_action_or_setter? }
68
+ it { should be_truthy }
69
+ end
70
+
71
+ context "with an action signature as setting" do
72
+ subject { action_path.question_action_or_setter? }
55
73
  it { should be_truthy }
56
74
  end
57
75
 
58
76
  context "with a setter signature as setting" do
59
- subject { setter_path.is_question_or_setter? }
77
+ subject { setter_path.question_action_or_setter? }
60
78
  it { should be_truthy }
61
79
  end
62
80
 
63
81
  context "with a normal path as setting" do
64
- subject { normal_path.is_question_or_setter? }
82
+ subject { normal_path.question_action_or_setter? }
65
83
  it { should be_falsey }
66
84
  end
67
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configurate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonne Haß
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-26 00:00:00.000000000 Z
11
+ date: 2014-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
metadata.gz.asc CHANGED
@@ -1,11 +1,11 @@
1
1
  -----BEGIN PGP SIGNATURE-----
2
2
  Version: GnuPG v2
3
3
 
4
- iQEcBAABAgAGBQJUJcgAAAoJEPNH4OtHrHDWGi0H/1bZoFxs/WNJhIvM/NciG/lx
5
- VNgn9R/R46sRZqjCU4EXUAicWAOggNhF78d6XAbdQJSE2aiokBxQrE/tci6q4iDU
6
- I1LLbcahSmZle9fR2JsUpGtT7A8hyKYpCDUjUG7TY2Ktfas9qL1GEsDtPKVjmQNS
7
- l0GTg1JQ7XZHSkRAdBWHn9geGnQBQ7AqAqREIK/6+zSAaN7QNtBijIg9h4EyBWXb
8
- SDZILyvkgj6bt+eE+o32jrNleLMfJbGU9zlNl92l4O1bb5TspUKwq9JkpSR4ZRzd
9
- jkt5H+gXfwF0/zltejfLlkp50fwF3XnWgy9cEAh/BNTVPzT0HgfJzq3YBPlM7Vo=
10
- =QI26
4
+ iQEcBAABAgAGBQJULyg7AAoJEPNH4OtHrHDWD0IH/0e90hWnDOQqWhUnKY3JM+gf
5
+ wOdHXiyB4p7cPIoO6QFsZq0Mm4t+bYLADI3adDq9kQAtIjo6s3jUJzK84QNjnsNS
6
+ 8Sc1OWNFYLrMVi/7rkJfyjiV6rMHYyB9O76vxH3w7/CZAl7fscadF4BW1PTqcti8
7
+ z4IoXtBs5KVxk1U3vtpPLaYitYIx/mc2voL0Z5wEuMeBhTYHjx4XraLrNzuSh6yN
8
+ cRnItaD70Z2uIvRF6bSyazDLWEO2cUfH/kTjsjHja4xwa4yYm3/ZkFZ2YZAXJNvU
9
+ P5mtnv5cCcJKZy1uNKV5DRj9zATL+yrzgSaSKV1O9BPW7zL3eHeu156TOxIRW48=
10
+ =sUo3
11
11
  -----END PGP SIGNATURE-----