cubus-settingslogic 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ *.log
3
+ *.sqlite3
4
+ pkg/*
5
+ coverage/*
6
+ doc/*
7
+ benchmarks/*
8
+
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ == 2.0.2 released 2009-08-22
2
+
3
+ * Define methods during method_missing instead of during initialization. Allows for modification on the fly.
4
+
5
+ == 2.0.0 released 2009-08-22
6
+
7
+ * Less magic, instead of automatically defining a Settings constant, you should define your own constant. See the readme for an example.
8
+
9
+ == 1.0.3 released 2009-04-23
10
+
11
+ * Fix Settings initialized with a Hash to work with both symbol and string hash keys.
12
+
13
+ == 1.0.2 released 2009-04-09
14
+
15
+ * Call key? off of the self in the class level name method.
16
+
17
+ == 1.0.1 released 2009-04-02
18
+
19
+ * Inherit from hash.
20
+
21
+ == 1.0.0 released 2008-12-05
22
+
23
+ * Only define methods if we have settings.
24
+
25
+ == 0.9.0 released 2008-10-30
26
+
27
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ben Johnson of Binary Logic (binarylogic.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,145 @@
1
+ = Settingslogic
2
+
3
+ Settingslogic is a simple configuration / settings solution that uses an ERB enabled YAML file. It has been great for
4
+ our apps, maybe you will enjoy it too. Settingslogic works with Rails, Sinatra, or any Ruby project.
5
+
6
+ So here is my question to you.....is Settingslogic a great settings solution or the greatest?
7
+
8
+ == Helpful links
9
+
10
+ * <b>Documentation:</b> http://rdoc.info/projects/binarylogic/settingslogic
11
+ * <b>Repository:</b> http://github.com/binarylogic/settingslogic/tree/master
12
+ * <b>Issues:</b> http://github.com/binarylogic/settingslogic/issues
13
+
14
+ == Installation
15
+
16
+ Install from rubyforge/gemcutter:
17
+
18
+ sudo gem install settingslogic
19
+
20
+ Or as a Rails plugin:
21
+
22
+ script/plugin install git://github.com/binarylogic/settingslogic.git
23
+
24
+ Settingslogic does not have any dependencies on Rails. Installing as a gem is recommended.
25
+
26
+ == Usage
27
+
28
+ === 1. Define your class
29
+
30
+ Instead of defining a Settings constant for you, that task is left to you. Simply create a class in your application
31
+ that looks like:
32
+
33
+ class Settings < Settingslogic
34
+ source "#{Rails.root}/config/application.yml"
35
+ namespace Rails.env
36
+ end
37
+
38
+ Name it Settings, name it Config, name it whatever you want. Add as many or as few as you like. A good place to put
39
+ this file in a rails app is app/models/settings.rb
40
+
41
+ I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
42
+
43
+ === 2. Create your settings
44
+
45
+ Notice above we specified an absolute path to our settings file called "application.yml". This is just a typical YAML file.
46
+ Also notice above that we specified a namespace for our environment. A namespace is just an optional string that corresponds
47
+ to a key in the YAML file.
48
+
49
+ Using a namespace allows us to change our configuration depending on our environment:
50
+
51
+ # app/config/application.yml
52
+ defaults: &defaults
53
+ cool:
54
+ saweet: nested settings
55
+ neat_setting: 24
56
+ awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
57
+
58
+ development:
59
+ <<: *defaults
60
+ neat_setting: 800
61
+
62
+ test:
63
+ <<: *defaults
64
+
65
+ production:
66
+ <<: *defaults
67
+
68
+ === 3. Access your settings
69
+
70
+ >> Rails.env
71
+ => "development"
72
+
73
+ >> Settings.cool
74
+ => "#<Settingslogic::Settings ... >"
75
+
76
+ >> Settings.cool.saweet
77
+ => "nested settings"
78
+
79
+ >> Settings.neat_setting
80
+ => 800
81
+
82
+ >> Settings.awesome_setting
83
+ => "Did you know 5 + 5 = 10?"
84
+
85
+ You can use these settings anywhere, for example in a model:
86
+
87
+ class Post < ActiveRecord::Base
88
+ self.per_page = Settings.pagination.posts_per_page
89
+ end
90
+
91
+ === 4. Optional / dynamic settings
92
+
93
+ Often, you will want to handle defaults in your application logic itself, to reduce the number of settings
94
+ you need to put in your YAML file. You can access an optional setting by using Hash notation:
95
+
96
+ >> Settings.messaging.queue_name
97
+ => Exception: Missing setting 'queue_name' in 'message' section in 'application.yml'
98
+
99
+ >> Settings.messaging['queue_name']
100
+ => nil
101
+
102
+ >> Settings.messaging['queue_name'] ||= 'user_mail'
103
+ => "user_mail"
104
+
105
+ >> Settings.messaging.queue_name
106
+ => "user_mail"
107
+
108
+ Modifying our model example:
109
+
110
+ class Post < ActiveRecord::Base
111
+ self.per_page = Settings.posts['per_page'] || Settings.pagination.per_page
112
+ end
113
+
114
+ This would allow you to specify a custom value for per_page just for posts, or
115
+ to fall back to your default value if not specified.
116
+
117
+ == Note on Sinatra / Capistrano / Vlad
118
+
119
+ Each of these frameworks uses a +set+ convention for settings, which actually defines methods
120
+ in the global Object namespace:
121
+
122
+ set :application, "myapp" # does "def application" globally
123
+
124
+ This can cause collisions with Settingslogic, since those methods are global. Luckily, the
125
+ solution is to just add a call to load! in your class:
126
+
127
+ class Settings < Settingslogic
128
+ source "#{Rails.root}/config/application.yml"
129
+ namespace Rails.env
130
+ load!
131
+ end
132
+
133
+ It's probably always safest to add load! to your class, since this guarantees settings will be
134
+ loaded at that time, rather than lazily later via method_missing.
135
+
136
+ Finally, you can reload all your settings later as well:
137
+
138
+ Settings.reload!
139
+
140
+ This is useful if you want to support changing your settings YAML without restarting your app.
141
+
142
+ == Author
143
+
144
+ Copyright (c) 2008-2010 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com],
145
+ released under the MIT license. Support for optional settings and reloading by {Nate Wiger}[http://nate.wiger.org].
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "settingslogic"
8
+ gem.summary = "A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern."
9
+ gem.email = "bjohnson@binarylogic.com"
10
+ gem.homepage = "http://github.com/binarylogic/settingslogic"
11
+ gem.authors = ["Ben Johnson of Binary Logic"]
12
+ end
13
+ Jeweler::GemcutterTasks.new
14
+ rescue LoadError
15
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
16
+ end
17
+
18
+ require 'spec/rake/spectask'
19
+ Spec::Rake::SpecTask.new(:spec) do |spec|
20
+ spec.libs << 'lib' << 'spec'
21
+ spec.spec_files = FileList['spec/**/*_spec.rb']
22
+ end
23
+
24
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.pattern = 'spec/**/*_spec.rb'
27
+ spec.rcov = true
28
+ end
29
+
30
+ task :spec => :check_dependencies
31
+
32
+ task :default => :spec
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 2
3
+ :minor: 0
4
+ :build:
5
+ :patch: 5
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,126 @@
1
+ require "yaml"
2
+ require "erb"
3
+
4
+ # A simple settings solution using a YAML file. See README for more information.
5
+ class Settingslogic < Hash
6
+ class MissingSetting < StandardError; end
7
+
8
+ class << self
9
+ def name # :nodoc:
10
+ instance.key?("name") ? instance.name : super
11
+ end
12
+
13
+ def source(value = nil)
14
+ if value.nil?
15
+ @source
16
+ else
17
+ @source = value
18
+ end
19
+ end
20
+
21
+ def namespace(value = nil)
22
+ if value.nil?
23
+ @namespace
24
+ else
25
+ @namespace = value
26
+ end
27
+ end
28
+
29
+ def key_by_path(key_path, separator = ".")
30
+ # Settings.get_nested_key('some.nested.setting')
31
+ tmp = instance
32
+ key_path.split(separator).each do |k|
33
+ if tmp[k].respond_to?("[]") && !tmp[k].nil?
34
+ tmp = tmp[k]
35
+ else
36
+ return nil
37
+ end
38
+ end
39
+ tmp
40
+ end
41
+
42
+ def [](key)
43
+ # Setting.key.value or Setting[:key][:value] or Setting['key']['value']
44
+ fetch(key.to_s,nil)
45
+ end
46
+
47
+ def []=(key,val)
48
+ # Setting[:key] = 'value' for dynamic settings
49
+ store(key.to_s,val)
50
+ end
51
+
52
+ def load!
53
+ instance
54
+ true
55
+ end
56
+
57
+ def reload!
58
+ @instance = nil
59
+ load!
60
+ end
61
+
62
+ private
63
+ def instance
64
+ @instance ||= new
65
+ end
66
+
67
+ def method_missing(name, *args, &block)
68
+ instance.send(name, *args, &block)
69
+ end
70
+ end
71
+
72
+ # Initializes a new settings object. You can initialize an object in any of the following ways:
73
+ #
74
+ # Settings.new(:application) # will look for config/application.yml
75
+ # Settings.new("application.yaml") # will look for application.yaml
76
+ # Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
77
+ # Settings.new(:config1 => 1, :config2 => 2)
78
+ #
79
+ # Basically if you pass a symbol it will look for that file in the configs directory of your rails app,
80
+ # if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
81
+ # Then you can pass a hash, and it just allows you to access the hash via methods.
82
+ def initialize(hash_or_file = self.class.source, section = nil)
83
+ case hash_or_file
84
+ when Hash
85
+ self.replace hash_or_file
86
+ else
87
+ hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
88
+ hash = hash[self.class.namespace] if self.class.namespace
89
+ self.replace hash
90
+ end
91
+ @section = section || hash_or_file # so end of error says "in application.yml"
92
+ create_accessors!
93
+ end
94
+
95
+ # Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used.
96
+ # Otherwise, create_accessors! (called by new) will have created actual methods for each key.
97
+ def method_missing(key, *args, &block)
98
+ begin
99
+ value = fetch(key.to_s)
100
+ rescue IndexError
101
+ raise MissingSetting, "Missing setting '#{key}' in #{@section}"
102
+ end
103
+ value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
104
+ end
105
+
106
+ private
107
+ # This handles naming collisions with Sinatra/Vlad/Capistrano. Since these use a set()
108
+ # helper that defines methods in Object, ANY method_missing ANYWHERE picks up the Vlad/Sinatra
109
+ # settings! So settings.deploy_to title actually calls Object.deploy_to (from set :deploy_to, "host"),
110
+ # rather than the app_yml['deploy_to'] hash. Jeezus.
111
+ def create_accessors!
112
+ self.each do |key,val|
113
+ # Use instance_eval/class_eval because they're actually more efficient than define_method{}
114
+ # http://stackoverflow.com/questions/185947/ruby-definemethod-vs-def
115
+ # http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
116
+ self.class.class_eval <<-EndEval
117
+ def #{key}
118
+ return @#{key} if @#{key} # cache (performance)
119
+ value = fetch('#{key}')
120
+ @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
121
+ end
122
+ EndEval
123
+ end
124
+ end
125
+
126
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "settingslogic"
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "cubus-settingslogic"
8
+ s.version = "2.0.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ben Johnson of Binary Logic"]
12
+ s.date = %q{2010-02-01}
13
+ s.email = %q{bjohnson@binarylogic.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG.rdoc",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "init.rb",
26
+ "lib/settingslogic.rb",
27
+ "rails/init.rb",
28
+ "settingslogic.gemspec",
29
+ "spec/settings.rb",
30
+ "spec/settings.yml",
31
+ "spec/settings2.rb",
32
+ "spec/settings3.rb",
33
+ "spec/settingslogic_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/binarylogic/settingslogic}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.5}
40
+ s.summary = %q{A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.}
41
+ s.test_files = [
42
+ "spec/settings.rb",
43
+ "spec/settings2.rb",
44
+ "spec/settings3.rb",
45
+ "spec/settingslogic_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ else
55
+ end
56
+ else
57
+ end
58
+ end
59
+
data/spec/settings.rb ADDED
@@ -0,0 +1,3 @@
1
+ class Settings < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ end
data/spec/settings.yml ADDED
@@ -0,0 +1,19 @@
1
+ setting1:
2
+ setting1_child: saweet
3
+ deep:
4
+ another: my value
5
+ child:
6
+ value: 2
7
+
8
+ setting2: 5
9
+ setting3: <%= 5 * 5 %>
10
+ name: test
11
+
12
+ language:
13
+ haskell:
14
+ paradigm: functional
15
+ smalltalk:
16
+ paradigm: object oriented
17
+
18
+ collides:
19
+ does: not
data/spec/settings2.rb ADDED
@@ -0,0 +1,4 @@
1
+ class Settings2 < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ namespace "setting1"
4
+ end
data/spec/settings3.rb ADDED
@@ -0,0 +1,4 @@
1
+ class Settings3 < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ load! # test of load
4
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe "Settingslogic" do
4
+ it "should access settings" do
5
+ Settings.setting2.should == 5
6
+ end
7
+
8
+ it "should access nested settings" do
9
+ Settings.setting1.setting1_child.should == "saweet"
10
+ end
11
+
12
+ it "should access deep nested settings" do
13
+ Settings.setting1.deep.another.should == "my value"
14
+ end
15
+
16
+ it "should access extra deep nested settings" do
17
+ Settings.setting1.deep.child.value.should == 2
18
+ end
19
+
20
+ it "should enable erb" do
21
+ Settings.setting3.should == 25
22
+ end
23
+
24
+ it "should namespace settings" do
25
+ Settings2.setting1_child.should == "saweet"
26
+ Settings2.deep.another.should == "my value"
27
+ end
28
+
29
+ it "should return the namespace" do
30
+ Settings.namespace.should be_nil
31
+ Settings2.namespace.should == 'setting1'
32
+ end
33
+
34
+ it "should get a key by a path" do
35
+ Settings.key_by_path("language.haskell.paradigm").should == "functional"
36
+ end
37
+
38
+ it "should distinguish nested keys" do
39
+ Settings.language.haskell.paradigm.should == 'functional'
40
+ Settings.language.smalltalk.paradigm.should == 'object oriented'
41
+ end
42
+
43
+ it "should not collide with global methods" do
44
+ Settings3.collides.does.should == 'not'
45
+ end
46
+
47
+ it "should raise a helpful error message" do
48
+ e = nil
49
+ begin
50
+ Settings.missing
51
+ rescue => e
52
+ e.should be_kind_of Settingslogic::MissingSetting
53
+ end
54
+ e.should_not be_nil
55
+ e.message.should =~ /Missing setting 'missing' in/
56
+
57
+ e = nil
58
+ begin
59
+ Settings.language.missing
60
+ rescue => e
61
+ e.should be_kind_of Settingslogic::MissingSetting
62
+ end
63
+ e.should_not be_nil
64
+ e.message.should =~ /Missing setting 'missing' in 'language' section/
65
+ end
66
+
67
+ it "should handle optional / dynamic settings" do
68
+ e = nil
69
+ begin
70
+ Settings.language.erlang
71
+ rescue => e
72
+ e.should be_kind_of Settingslogic::MissingSetting
73
+ end
74
+ e.should_not be_nil
75
+ e.message.should =~ /Missing setting 'erlang' in 'language' section/
76
+
77
+ Settings.language['erlang'].should be_nil
78
+ Settings.language['erlang'] ||= 5
79
+ Settings.language['erlang'].should == 5
80
+
81
+ Settings.language['erlang'] = {'paradigm' => 'functional'}
82
+ Settings.language.erlang.paradigm.should == 'functional'
83
+
84
+ Settings.reload!
85
+ Settings.language['erlang'].should be_nil
86
+ end
87
+
88
+ # Put this test last or else call to .instance will load @instance,
89
+ # masking bugs.
90
+ it "should be a hash" do
91
+ Settings.send(:instance).should be_is_a(Hash)
92
+ end
93
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec'
2
+ require 'rubygems'
3
+ require 'ruby-debug' if RUBY_VERSION < '1.9' # ruby-debug does not work on 1.9.1 yet
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'settingslogic'
8
+ require 'settings'
9
+ require 'settings2'
10
+ require 'settings3'
11
+
12
+ # Needed to test Settings3
13
+ def collides
14
+ 'collision'
15
+ end
16
+
17
+ Spec::Runner.configure do |config|
18
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cubus-settingslogic
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Ben Johnson of Binary Logic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-01 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: bjohnson@binarylogic.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - CHANGELOG.rdoc
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - init.rb
33
+ - lib/settingslogic.rb
34
+ - rails/init.rb
35
+ - settingslogic.gemspec
36
+ - spec/settings.rb
37
+ - spec/settings.yml
38
+ - spec/settings2.rb
39
+ - spec/settings3.rb
40
+ - spec/settingslogic_spec.rb
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/binarylogic/settingslogic
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.
70
+ test_files:
71
+ - spec/settings.rb
72
+ - spec/settings2.rb
73
+ - spec/settings3.rb
74
+ - spec/settingslogic_spec.rb
75
+ - spec/spec_helper.rb