rsettings 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rsettings/adapters/basic_disk_settings.rb +1 -1
- data/lib/rsettings/adapters/environment_settings.rb +2 -1
- data/lib/rsettings/core/internal/defaults.rb +20 -0
- data/lib/rsettings/core/internal/settings_chain.rb +18 -0
- data/lib/rsettings/core/internal/settings_configuration.rb +41 -0
- data/lib/rsettings/core/missing.rb +15 -0
- data/lib/rsettings/core/names.rb +21 -0
- data/lib/rsettings/core/rsettings.rb +29 -0
- data/lib/rsettings/core/setting.rb +12 -0
- data/lib/rsettings/core/setting_name.rb +15 -0
- data/lib/rsettings.rb +30 -3
- data/rsettings.gemspec +11 -7
- data/spec/integration.tests/adapters/basic_disk_settings_spec.rb +11 -8
- data/spec/system.tests/can_supply_defaults_for_missing_settings_spec.rb +39 -0
- metadata +12 -8
- data/lib/rsettings/internal/names.rb +0 -13
- data/lib/rsettings/internal/rsettings.rb +0 -29
- data/lib/rsettings/internal/setting.rb +0 -10
- data/lib/rsettings/internal/settings_chain.rb +0 -22
- data/lib/rsettings/internal/settings_configuration.rb +0 -36
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Defaults
|
2
|
+
def initialize(names)
|
3
|
+
@names = names
|
4
|
+
end
|
5
|
+
|
6
|
+
def get(setting)
|
7
|
+
real_name = @names.reverse_for setting
|
8
|
+
Setting.new defaults[real_name]
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(name, value)
|
12
|
+
defaults[name] = value
|
13
|
+
end
|
14
|
+
|
15
|
+
def any?; false == defaults.empty? end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def defaults; @defaults ||= {}; end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class SettingsChain
|
2
|
+
def initialize(*links)
|
3
|
+
@settings = *links.flatten
|
4
|
+
end
|
5
|
+
|
6
|
+
def link(link)
|
7
|
+
@settings << link
|
8
|
+
end
|
9
|
+
|
10
|
+
def get(name)
|
11
|
+
@settings.each do |setting|
|
12
|
+
result = setting.get(name)
|
13
|
+
return result unless result.missing?
|
14
|
+
end
|
15
|
+
|
16
|
+
Setting.new nil
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class SettingsConfiguration
|
2
|
+
attr_reader :missing, :settings, :names
|
3
|
+
|
4
|
+
def initialize(&block)
|
5
|
+
@names = Names.new
|
6
|
+
@defaults = Defaults.new @names
|
7
|
+
@settings = EnvironmentSettings.new
|
8
|
+
|
9
|
+
instance_exec &block if block_given?
|
10
|
+
|
11
|
+
@settings = SettingsChain.new(@settings).tap do |it|
|
12
|
+
it.link @defaults if defaults?
|
13
|
+
it.link tail
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def let(opts = {})
|
18
|
+
@names.add opts
|
19
|
+
end
|
20
|
+
|
21
|
+
def default(name, opts = {})
|
22
|
+
fail "Expected the :to option so I can tell what the default value is" unless opts[:to]
|
23
|
+
@defaults.add name, opts[:to]
|
24
|
+
end
|
25
|
+
|
26
|
+
def when_missing(type)
|
27
|
+
@tail = IgnoreMissing if type === :ignore
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_settings(opts={})
|
31
|
+
@settings = opts.is_a?(Hash) ? SettingsChain.new(opts[:chain].map{|it| it.new}) : opts.new
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def tail
|
37
|
+
@tail || FailOnMissing
|
38
|
+
end
|
39
|
+
|
40
|
+
def defaults?; @defaults.any? end
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Names
|
2
|
+
require "audible"; include Audible
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@opts = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(opts={})
|
9
|
+
notify :adding, opts
|
10
|
+
@opts.merge!(opts.invert)
|
11
|
+
end
|
12
|
+
|
13
|
+
def for(setting)
|
14
|
+
notify :finding, setting
|
15
|
+
@opts[setting.to_s] || @opts[setting.to_sym] || setting
|
16
|
+
end
|
17
|
+
|
18
|
+
def reverse_for(name)
|
19
|
+
@opts.invert[name.value] || name.value
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class RSettings
|
2
|
+
require "audible"; include Audible
|
3
|
+
|
4
|
+
def initialize(opts={})
|
5
|
+
@settings,@names = opts[:settings],opts[:names]
|
6
|
+
end
|
7
|
+
|
8
|
+
def find(m)
|
9
|
+
query = m.to_s.end_with? "?"
|
10
|
+
|
11
|
+
m = m.to_s.delete "?" if query
|
12
|
+
|
13
|
+
name = ConvertibleSettingName.new(@names, m)
|
14
|
+
|
15
|
+
if query
|
16
|
+
value = @settings.get(name)
|
17
|
+
|
18
|
+
fail "Unable to convert setting <#{name.value}> to flag" unless value.truthy?
|
19
|
+
|
20
|
+
return value.to_s.downcase === "yes"
|
21
|
+
end
|
22
|
+
|
23
|
+
notify :finding, m, name
|
24
|
+
|
25
|
+
result = @settings.get(name)
|
26
|
+
|
27
|
+
result.missing? ? nil : result.to_s
|
28
|
+
end
|
29
|
+
end
|
data/lib/rsettings.rb
CHANGED
@@ -10,10 +10,37 @@ class Settings
|
|
10
10
|
def method_missing(m, *args, &block)
|
11
11
|
fail "Only support queries, cannot do command <#{m}>" unless args.empty?
|
12
12
|
|
13
|
-
RSettings.new(
|
13
|
+
rsettings = RSettings.new(
|
14
14
|
:settings => @conf.settings,
|
15
|
-
:missing => @conf.missing,
|
16
15
|
:names => @conf.names
|
17
|
-
)
|
16
|
+
)
|
17
|
+
|
18
|
+
Probe.new rsettings
|
19
|
+
|
20
|
+
rsettings.find m
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Probe
|
25
|
+
def initialize(names)
|
26
|
+
names.on :finding do |e,args|
|
27
|
+
Log.info "[RSettings] Finding setting for <#{args.first}> by name <#{args[1].value}>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class ConsoleLog
|
33
|
+
class << self
|
34
|
+
def info(what)
|
35
|
+
puts "[INFO] #{what}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class DevNullLog
|
41
|
+
class << self
|
42
|
+
def info(what); end
|
18
43
|
end
|
19
44
|
end
|
45
|
+
|
46
|
+
Log = ENV["LOUD"] == "on" ? ConsoleLog : DevNullLog
|
data/rsettings.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "rsettings"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ben Biddington"]
|
12
|
-
s.date = "2013-12-
|
12
|
+
s.date = "2013-12-16"
|
13
13
|
s.description = "Settings"
|
14
14
|
s.email = "ben.biddington@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,16 +27,20 @@ Gem::Specification.new do |s|
|
|
27
27
|
"lib/rsettings.rb",
|
28
28
|
"lib/rsettings/adapters/basic_disk_settings.rb",
|
29
29
|
"lib/rsettings/adapters/environment_settings.rb",
|
30
|
-
"lib/rsettings/internal/
|
31
|
-
"lib/rsettings/internal/
|
32
|
-
"lib/rsettings/internal/
|
33
|
-
"lib/rsettings/
|
34
|
-
"lib/rsettings/
|
30
|
+
"lib/rsettings/core/internal/defaults.rb",
|
31
|
+
"lib/rsettings/core/internal/settings_chain.rb",
|
32
|
+
"lib/rsettings/core/internal/settings_configuration.rb",
|
33
|
+
"lib/rsettings/core/missing.rb",
|
34
|
+
"lib/rsettings/core/names.rb",
|
35
|
+
"lib/rsettings/core/rsettings.rb",
|
36
|
+
"lib/rsettings/core/setting.rb",
|
37
|
+
"lib/rsettings/core/setting_name.rb",
|
35
38
|
"rsettings.gemspec",
|
36
39
|
"spec/integration.tests/adapters/basic_disk_settings_spec.rb",
|
37
40
|
"spec/spec_helper.rb",
|
38
41
|
"spec/system.tests/can_fall_back_to_file_settings_spec.rb",
|
39
42
|
"spec/system.tests/can_specify_flags_spec.rb",
|
43
|
+
"spec/system.tests/can_supply_defaults_for_missing_settings_spec.rb",
|
40
44
|
"spec/system.tests/can_use_env_spec.rb",
|
41
45
|
"spec/system.tests/options_spec.rb"
|
42
46
|
]
|
@@ -7,37 +7,40 @@ describe BasicDiskSettings do
|
|
7
7
|
settings.clear
|
8
8
|
end
|
9
9
|
|
10
|
+
let(:a) { SettingName.new :a}
|
11
|
+
let(:b) { SettingName.new :b}
|
12
|
+
|
10
13
|
it "can store single setting" do
|
11
|
-
settings.set :
|
14
|
+
settings.set :a => "value"
|
12
15
|
|
13
|
-
expect(settings.get(
|
16
|
+
expect(settings.get(a).to_s).to eql "value"
|
14
17
|
end
|
15
18
|
|
16
19
|
it "can store multiple settings at once" do
|
17
20
|
settings.set :a => "value a", :b => "value b"
|
18
21
|
|
19
|
-
expect(settings.get(
|
20
|
-
expect(settings.get(
|
22
|
+
expect(settings.get(a).to_s).to eql "value a"
|
23
|
+
expect(settings.get(b).to_s).to eql "value b"
|
21
24
|
end
|
22
25
|
|
23
26
|
it "overwrites duplicates" do
|
24
27
|
settings.set :a => "value a"
|
25
28
|
settings.set :a => "another value for a"
|
26
29
|
|
27
|
-
expect(settings.get(
|
30
|
+
expect(settings.get(a).to_s).to eql "another value for a"
|
28
31
|
end
|
29
32
|
|
30
33
|
it "overwrites duplicates even when setting the same setting" do
|
31
34
|
settings.set :a => "value a", :a => "another value for a"
|
32
35
|
|
33
|
-
expect(settings.get(
|
36
|
+
expect(settings.get(a).to_s).to eql "another value for a"
|
34
37
|
end
|
35
38
|
|
36
39
|
it "records all settings when you call set multiple times" do
|
37
40
|
settings.set :a => "value a"
|
38
41
|
settings.set :b => "value b"
|
39
42
|
|
40
|
-
expect(settings.get(
|
41
|
-
expect(settings.get(
|
43
|
+
expect(settings.get(a).to_s).to_not be_nil
|
44
|
+
expect(settings.get(b).to_s).to_not be_nil
|
42
45
|
end
|
43
46
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "default values for missing settings" do
|
4
|
+
it "can be configured to use a default when setting is missing" do
|
5
|
+
settings = Settings.new do
|
6
|
+
default :colour, :to => "red"
|
7
|
+
end
|
8
|
+
|
9
|
+
expect(settings.colour).to eql "red"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can be overwritten -- the last set one wins" do
|
13
|
+
settings = Settings.new do
|
14
|
+
default :colour, :to => "red"
|
15
|
+
default :colour, :to => "gold"
|
16
|
+
end
|
17
|
+
|
18
|
+
expect(settings.colour).to eql "gold"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "you can default a mapped setting" do
|
22
|
+
settings = Settings.new do
|
23
|
+
let "C" => :colour
|
24
|
+
default :colour, :to => "green"
|
25
|
+
end
|
26
|
+
|
27
|
+
expect(settings.colour).to eql "green"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "is ignored if present elsewhere" do
|
31
|
+
settings = Settings.new do
|
32
|
+
default :colour, :to => "red"
|
33
|
+
end
|
34
|
+
|
35
|
+
ENV["colour"] = "pink"
|
36
|
+
|
37
|
+
expect(settings.colour).to eql "pink"
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsettings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: audible
|
@@ -109,16 +109,20 @@ files:
|
|
109
109
|
- lib/rsettings.rb
|
110
110
|
- lib/rsettings/adapters/basic_disk_settings.rb
|
111
111
|
- lib/rsettings/adapters/environment_settings.rb
|
112
|
-
- lib/rsettings/internal/
|
113
|
-
- lib/rsettings/internal/
|
114
|
-
- lib/rsettings/internal/
|
115
|
-
- lib/rsettings/
|
116
|
-
- lib/rsettings/
|
112
|
+
- lib/rsettings/core/internal/defaults.rb
|
113
|
+
- lib/rsettings/core/internal/settings_chain.rb
|
114
|
+
- lib/rsettings/core/internal/settings_configuration.rb
|
115
|
+
- lib/rsettings/core/missing.rb
|
116
|
+
- lib/rsettings/core/names.rb
|
117
|
+
- lib/rsettings/core/rsettings.rb
|
118
|
+
- lib/rsettings/core/setting.rb
|
119
|
+
- lib/rsettings/core/setting_name.rb
|
117
120
|
- rsettings.gemspec
|
118
121
|
- spec/integration.tests/adapters/basic_disk_settings_spec.rb
|
119
122
|
- spec/spec_helper.rb
|
120
123
|
- spec/system.tests/can_fall_back_to_file_settings_spec.rb
|
121
124
|
- spec/system.tests/can_specify_flags_spec.rb
|
125
|
+
- spec/system.tests/can_supply_defaults_for_missing_settings_spec.rb
|
122
126
|
- spec/system.tests/can_use_env_spec.rb
|
123
127
|
- spec/system.tests/options_spec.rb
|
124
128
|
homepage: http://github.com/ben-biddington/rsettings
|
@@ -136,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
140
|
version: '0'
|
137
141
|
segments:
|
138
142
|
- 0
|
139
|
-
hash: -
|
143
|
+
hash: -70081105
|
140
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
145
|
none: false
|
142
146
|
requirements:
|
@@ -1,29 +0,0 @@
|
|
1
|
-
class RSettings
|
2
|
-
def initialize(opts={})
|
3
|
-
@settings,@missing,@names = opts[:settings],opts[:missing],opts[:names]
|
4
|
-
|
5
|
-
@settings.on :missing do |e,args|
|
6
|
-
@missing.on_missing args.first
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def find(m)
|
11
|
-
query = m.to_s.end_with? "?"
|
12
|
-
|
13
|
-
m = m.to_s.delete "?" if query
|
14
|
-
|
15
|
-
setting_name = @names.for(m)
|
16
|
-
|
17
|
-
if query
|
18
|
-
setting_name = @names.for(m)
|
19
|
-
|
20
|
-
value = Setting.new @settings.get(setting_name)
|
21
|
-
|
22
|
-
fail "Unable to convert setting <#{setting_name}> to flag" unless value.truthy?
|
23
|
-
|
24
|
-
return value.to_s.downcase === "yes"
|
25
|
-
end
|
26
|
-
|
27
|
-
@settings.get(setting_name)
|
28
|
-
end
|
29
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
class SettingsChain
|
2
|
-
require "audible"; include Audible
|
3
|
-
|
4
|
-
def initialize(*links)
|
5
|
-
@settings = *links.flatten.map{|klass| klass.new}
|
6
|
-
end
|
7
|
-
|
8
|
-
def get(name)
|
9
|
-
@settings.each do |setting|
|
10
|
-
result = setting.get(name)
|
11
|
-
return result if result
|
12
|
-
end
|
13
|
-
|
14
|
-
notify_missing name
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def notify_missing(name)
|
20
|
-
notify :missing, name
|
21
|
-
end
|
22
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
class SettingsConfiguration
|
2
|
-
attr_reader :missing, :settings, :names
|
3
|
-
|
4
|
-
def initialize(&block)
|
5
|
-
@missing = FailOnMissing.new
|
6
|
-
@settings = EnvironmentSettings.new
|
7
|
-
@names = Names.new
|
8
|
-
instance_exec &block if block_given?
|
9
|
-
end
|
10
|
-
|
11
|
-
def let(opts = {})
|
12
|
-
@names.add opts
|
13
|
-
end
|
14
|
-
|
15
|
-
def when_missing(type)
|
16
|
-
@missing = IgnoreMissing.new if type === :ignore
|
17
|
-
end
|
18
|
-
|
19
|
-
def with_settings(opts={})
|
20
|
-
if opts.is_a? Hash
|
21
|
-
@settings = SettingsChain.new(opts[:chain]) if opts[:chain]
|
22
|
-
else
|
23
|
-
@settings = opts.new
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class IgnoreMissing
|
29
|
-
def on_missing(name); end
|
30
|
-
end
|
31
|
-
|
32
|
-
class FailOnMissing
|
33
|
-
def on_missing(name)
|
34
|
-
fail "Setting <#{name}> not found"
|
35
|
-
end
|
36
|
-
end
|