rsettings 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rsettings ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ :ben_rules: 'yes'
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- settings
1
+ RSettings
2
2
  ========
3
3
 
4
+ [![Build Status](https://secure.travis-ci.org/ben-biddington/rsettings.png)](http://travis-ci.org/ben-biddington/rsettings)
5
+
4
6
  Settings for ruby programs
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,51 @@
1
+ class BasicDiskSettings
2
+ require "fileutils"; include FileUtils
3
+ require "audible"; include Audible
4
+ require "yaml"
5
+
6
+ def initialize(&block)
7
+ @file = ".rsettings"
8
+ _ensure
9
+ instance_eval &block if block_given?
10
+ end
11
+
12
+ def clear
13
+ rm file if exists?
14
+ end
15
+
16
+ def set(opts ={})
17
+ opts = all.merge opts
18
+
19
+ File.open file, "w+" do |io|
20
+ io.puts opts.to_yaml
21
+ end
22
+
23
+ notify :set, @settings
24
+ end
25
+
26
+ def get(name)
27
+ settings[name].tap{|result| notify_missing name unless result}
28
+ end
29
+
30
+ def file; @file; end
31
+
32
+ private
33
+
34
+ def settings; @settings ||= all; end
35
+
36
+ def all
37
+ return {} unless exists?
38
+
39
+ YAML.load(IO.read(file))
40
+ end
41
+
42
+ def notify_missing(name)
43
+ notify :missing, name
44
+ end
45
+
46
+ def exists?; File.exists? file; end
47
+
48
+ def _ensure
49
+ touch file
50
+ end
51
+ end
@@ -1,12 +1,7 @@
1
1
  class EnvironmentSettings
2
2
  require "audible"; include Audible;
3
-
4
- def initialize
5
- @vars = VariableList.new
6
- end
7
3
 
8
4
  def get(name)
9
- real_name = @vars.name name
10
5
  ENV[name.to_s].tap{|it| notify_missing(name) if it.nil?}
11
6
  end
12
7
 
@@ -16,9 +11,3 @@ class EnvironmentSettings
16
11
  notify :missing, name
17
12
  end
18
13
  end
19
-
20
- class VariableList
21
- def name(from)
22
- from
23
- end
24
- end
@@ -0,0 +1,29 @@
1
+ class RSettings
2
+ def initialize(settings, config)
3
+ @settings,@config = settings,config
4
+
5
+ @settings.on :missing do |e,args|
6
+ @config.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 = @config.name_for(m)
16
+
17
+ if query
18
+ setting_name = @config.name_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
@@ -0,0 +1,10 @@
1
+ class Setting
2
+ def truthy?; @truthy; end
3
+
4
+ def initialize(text)
5
+ @text = text.to_s
6
+ @truthy = text.downcase.match /(yes|no)/
7
+ end
8
+
9
+ def to_s; @text; end
10
+ end
@@ -0,0 +1,22 @@
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,8 +1,9 @@
1
1
  class SettingsConfiguration
2
- attr_reader :missing
2
+ attr_reader :missing, :settings
3
3
 
4
4
  def initialize
5
5
  @missing = FailOnMissing.new
6
+ @settings = EnvironmentSettings.new
6
7
  end
7
8
 
8
9
  def let(opts = {})
@@ -17,6 +18,14 @@ class SettingsConfiguration
17
18
  _opts[setting] || setting
18
19
  end
19
20
 
21
+ def with_settings(opts={})
22
+ if opts.is_a? Hash
23
+ @settings = SettingsChain.new(opts[:chain]) if opts[:chain]
24
+ else
25
+ @settings = opts.new
26
+ end
27
+ end
28
+
20
29
  private
21
30
 
22
31
  def _opts; @opts ||= {}; end
data/lib/rsettings.rb CHANGED
@@ -4,7 +4,6 @@ Dir.glob(File.join(dir, "rsettings", "**", "*.rb")).each {|f| require f}
4
4
 
5
5
  class Settings
6
6
  def initialize(&block)
7
- @settings = EnvironmentSettings.new
8
7
  @config = SettingsConfiguration.new
9
8
 
10
9
  if block_given?
@@ -13,16 +12,14 @@ class Settings
13
12
  end
14
13
  end
15
14
 
16
- @settings.on :missing do |e,args|
17
- @config.missing.on_missing args.first
18
- end
15
+ @settings = @config.settings
16
+
17
+ fail "Error" unless @config.settings
19
18
  end
20
19
 
21
20
  def method_missing(m, *args, &block)
22
21
  fail "Only support queries, cannot do <#{m}>" unless args.empty?
23
22
 
24
- setting_name = @config.name_for m
25
-
26
- @settings.get(setting_name)
23
+ RSettings.new(@settings, @config).find m
27
24
  end
28
25
  end
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.2.0"
8
+ s.version = "0.3.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-10"
12
+ s.date = "2013-12-11"
13
13
  s.description = "Settings"
14
14
  s.email = "ben.biddington@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  ]
20
20
  s.files = [
21
21
  ".document",
22
+ ".rsettings",
22
23
  ".travis.yml",
23
24
  "Gemfile",
24
25
  "Gemfile.lock",
@@ -28,11 +29,18 @@ Gem::Specification.new do |s|
28
29
  "Rakefile",
29
30
  "VERSION",
30
31
  "lib/rsettings.rb",
32
+ "lib/rsettings/adapters/basic_disk_settings.rb",
31
33
  "lib/rsettings/adapters/environment_settings.rb",
32
- "lib/rsettings/settings_configuration.rb",
34
+ "lib/rsettings/internal/rsettings.rb",
35
+ "lib/rsettings/internal/setting.rb",
36
+ "lib/rsettings/internal/settings_chain.rb",
37
+ "lib/rsettings/internal/settings_configuration.rb",
33
38
  "rsettings.gemspec",
34
39
  "settings.gemspec",
40
+ "spec/integration.tests/adapters/basic_disk_settings_spec.rb",
35
41
  "spec/spec_helper.rb",
42
+ "spec/system.tests/can_fall_back_to_file_settings_spec.rb",
43
+ "spec/system.tests/can_specify_flags_spec.rb",
36
44
  "spec/system.tests/can_use_env_spec.rb",
37
45
  "spec/system.tests/options_spec.rb"
38
46
  ]
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe BasicDiskSettings do
4
+ let(:settings) {BasicDiskSettings.new}
5
+
6
+ before do
7
+ settings.clear
8
+ end
9
+
10
+ it "can store single setting" do
11
+ settings.set :name => "value"
12
+
13
+ expect(settings.get(:name)).to eql "value"
14
+ end
15
+
16
+ it "can store multiple settings at once" do
17
+ settings.set :a => "value a", :b => "value b"
18
+
19
+ expect(settings.get(:a)).to eql "value a"
20
+ expect(settings.get(:b)).to eql "value b"
21
+ end
22
+
23
+ it "overwrites duplicates" do
24
+ settings.set :a => "value a"
25
+ settings.set :a => "another value for a"
26
+
27
+ expect(settings.get(:a)).to eql "another value for a"
28
+ end
29
+
30
+ it "overwrites duplicates even when setting the same setting" do
31
+ settings.set :a => "value a", :a => "another value for a"
32
+
33
+ expect(settings.get(:a)).to eql "another value for a"
34
+ end
35
+
36
+ it "records all settings when you call set multiple times" do
37
+ settings.set :a => "value a"
38
+ settings.set :b => "value b"
39
+
40
+ expect(settings.get(:a)).to_not be_nil
41
+ expect(settings.get(:b)).to_not be_nil
42
+ end
43
+ end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe "a settings fallback" do
4
+ before do
5
+ ENV.clear
6
+
7
+ @disk_settings = BasicDiskSettings.new do
8
+ clear
9
+ set :jazzs_bike_shorts => "tight"
10
+ end
11
+ end
12
+
13
+ let (:settings) do
14
+ Settings.new do
15
+ with_settings :chain => [EnvironmentSettings, BasicDiskSettings]
16
+ end
17
+ end
18
+
19
+ let (:disk_settings) do
20
+ @disk_settings
21
+ end
22
+
23
+ it "for example, you can fall back to a setting on disk" do
24
+ expect(settings.jazzs_bike_shorts).to eql "tight"
25
+ end
26
+
27
+ it "you get the environment setting when it exists" do
28
+ ENV["jazzs_bike_shorts"] = "this is from the environment variables"
29
+
30
+ expect(settings.jazzs_bike_shorts).to eql "this is from the environment variables"
31
+ end
32
+
33
+ it "you can fallback one and not another" do
34
+ ENV["b"] = "env b"
35
+
36
+ disk_settings.clear
37
+ disk_settings.set :a => "disk a", :b => "disk b"
38
+
39
+ expect(settings.a).to eql "disk a"
40
+ expect(settings.b).to eql "env b"
41
+ end
42
+
43
+ it "you can use ONLY disk if you like" do
44
+ settings = Settings.new do
45
+ with_settings BasicDiskSettings
46
+ end
47
+
48
+ ENV["ben_rules"] = "expect this to be ignored"
49
+
50
+ disk_settings.clear
51
+ disk_settings.on(:set){|e,args| puts args.first}
52
+ disk_settings.set :ben_rules => "yes"
53
+
54
+ expect(settings.ben_rules).to eql "yes"
55
+ end
56
+ end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ describe "settings that are either true or false" do
4
+ let(:settings){Settings.new}
5
+
6
+ before { ENV.clear }
7
+
8
+ it "returns a flag when value is 'yes' or 'no'" do
9
+ ENV["allowed"] = "yes"
10
+ ENV["disallowed"] = "no"
11
+
12
+ expect(settings.allowed?).to be_true
13
+ expect(settings.disallowed?).to be_false
14
+ end
15
+
16
+ it "any other value returns setting not found" do
17
+ ENV["allowed"] = "xxx"
18
+
19
+ expect{settings.allowed?}.to raise_error /Unable to convert setting <allowed> to flag/
20
+ end
21
+
22
+ it "accepts mapped setting names, too" do
23
+ settings = Settings.new do
24
+ let "V" => "verbose"
25
+ end
26
+
27
+ ENV["V"] = "yes"
28
+
29
+ expect(settings.verbose?).to be_true
30
+ end
31
+ 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.2.0
4
+ version: 0.3.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-10 00:00:00.000000000 Z
12
+ date: 2013-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: audible
@@ -101,6 +101,7 @@ extra_rdoc_files:
101
101
  - README.rdoc
102
102
  files:
103
103
  - .document
104
+ - .rsettings
104
105
  - .travis.yml
105
106
  - Gemfile
106
107
  - Gemfile.lock
@@ -110,11 +111,18 @@ files:
110
111
  - Rakefile
111
112
  - VERSION
112
113
  - lib/rsettings.rb
114
+ - lib/rsettings/adapters/basic_disk_settings.rb
113
115
  - lib/rsettings/adapters/environment_settings.rb
114
- - lib/rsettings/settings_configuration.rb
116
+ - lib/rsettings/internal/rsettings.rb
117
+ - lib/rsettings/internal/setting.rb
118
+ - lib/rsettings/internal/settings_chain.rb
119
+ - lib/rsettings/internal/settings_configuration.rb
115
120
  - rsettings.gemspec
116
121
  - settings.gemspec
122
+ - spec/integration.tests/adapters/basic_disk_settings_spec.rb
117
123
  - spec/spec_helper.rb
124
+ - spec/system.tests/can_fall_back_to_file_settings_spec.rb
125
+ - spec/system.tests/can_specify_flags_spec.rb
118
126
  - spec/system.tests/can_use_env_spec.rb
119
127
  - spec/system.tests/options_spec.rb
120
128
  homepage: http://github.com/ben-biddington/rsettings
@@ -132,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
140
  version: '0'
133
141
  segments:
134
142
  - 0
135
- hash: 106447213
143
+ hash: -382109293
136
144
  required_rubygems_version: !ruby/object:Gem::Requirement
137
145
  none: false
138
146
  requirements: