settingslogic 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == 0.9.0 released 2008-10-30
2
+
3
+ * Initial release
data/MIT-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/Manifest ADDED
@@ -0,0 +1,15 @@
1
+ CHANGELOG.rdoc
2
+ init.rb
3
+ lib/settingslogic/config.rb
4
+ lib/settingslogic/settings.rb
5
+ lib/settingslogic/version.rb
6
+ lib/settingslogic.rb
7
+ Manifest
8
+ MIT-LICENSE
9
+ Rakefile
10
+ README.rdoc
11
+ test/application.yml
12
+ test/application2.yml
13
+ test/test_config.rb
14
+ test/test_helper.rb
15
+ test/test_setting.rb
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = Settingslogic
2
+
3
+ Settingslogic is an old library of mine that I decided to go ahead and share with the world. It's nothing crazy or new. Just a simple solution to a simple problem. Settingslogic provides globally accessible settings via an ERB enabled YAML file. It has been great for my apps, maybe you will enjoy it too.
4
+
5
+ So here is my question to you.....is Settingslogic a great settings solution or the greatest?
6
+
7
+ == Create your settings
8
+
9
+ By default Settingslogic tries to load config/application.yml. This is just a typical YAML file, notice ERB is allowed.
10
+
11
+ # app/config/application.yml
12
+ defaults: &defaults
13
+ cool:
14
+ saweet: nested settings
15
+ neat_setting: 24
16
+ awesome_setting: <%= "Did you know 5 + 5 = " + (5 + 5) + "?" %>
17
+
18
+ development:
19
+ <<: *defaults
20
+ neat_setting: 800
21
+
22
+ test:
23
+ <<: *defaults
24
+
25
+ production:
26
+ <<: *defaults
27
+
28
+ == Access your settings
29
+
30
+ >> RAILS_ENV
31
+ => "development"
32
+
33
+ >> Settings.cool
34
+ => "#<Settingslogic::Settings ... >"
35
+
36
+ >> Settings.cool.saweet
37
+ => "nested settings"
38
+
39
+ >> Settings.neat_setting
40
+ => 800
41
+
42
+ >> Settings.awesome_setting
43
+ => "Did you know 5 + 5 = 10?"
44
+
45
+ == Multiple settings
46
+
47
+ settings1 = Settings.new(:settings1) # looks for config/settings1.yml
48
+ settings2 = Settings.new("settings2.yaml") # looks for settings2.yml
49
+ settings2 = Settings.new("/abs/path/settings2.yaml") # looks for /abs/path/settings2.yml
50
+ settings3 = Settings.new(:some_setting => "some value")
51
+
52
+ == Configure
53
+
54
+ Configuration is optional. See Settingslogic::Config for more details.
55
+
56
+ # config/initializers/settingslogic.rb
57
+ Settingslogic::Config.configure do |config|
58
+ config.file_name = :config # will look for config/config.yml
59
+ config.file_name = "config" # will look for config
60
+ config.file_name = "config.yaml" # will look for confg.yaml
61
+ config.file_name = "/absolute/path/config.yml" # will look for /absolute/path/config.yaml
62
+ end
63
+
64
+ == Helpful links
65
+
66
+ * <b>Documentation:</b> http://settingslogic.rubyforge.org
67
+ * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/19028-settingslogic
68
+
69
+
70
+ Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'echoe'
3
+
4
+ require File.dirname(__FILE__) << "/lib/settingslogic/version"
5
+
6
+ Echoe.new 'settingslogic' do |p|
7
+ p.version = Settingslogic::Version::STRING
8
+ p.author = "Ben Johnson of Binary Logic"
9
+ p.email = 'bjohnson@binarylogic.com'
10
+ p.project = 'settingslogic'
11
+ p.summary = "Simple and straightforward application wide settings"
12
+ p.url = "http://github.com/binarylogic/settingslogic"
13
+ p.dependencies = []
14
+ p.include_rakefile = true
15
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "settingslogic"
@@ -0,0 +1,13 @@
1
+ require "yaml"
2
+ require "erb"
3
+ require File.dirname(__FILE__) + "/settingslogic/config"
4
+ require File.dirname(__FILE__) + "/settingslogic/settings"
5
+
6
+ # Try to load conflicting Settings classes
7
+ begin
8
+ Settings
9
+ rescue(NameError)
10
+ end
11
+
12
+ # Since we don't have a Settings constant, lets go ahead and use it
13
+ ::Settings = Settingslogic::Settings unless defined?(Settings)
@@ -0,0 +1,26 @@
1
+ module Settingslogic
2
+ # = Config
3
+ # Sets configuration on Settingslogic.
4
+ class Config
5
+ class << self
6
+ def configure
7
+ yield self
8
+ end
9
+
10
+ # The name of the file that your settings will be stored for singleton access. Meaning the settings file that will be used when calling methods on the class level:
11
+ #
12
+ # Settings.setting1
13
+ # Settings.setting2
14
+ # # etc...
15
+ #
16
+ # All that you need to do is specify the name of the file. It will automatically look in the config directory.
17
+ #
18
+ # * <tt>Default:</tt> :application
19
+ # * <tt>Accepts:</tt> Symbol or String
20
+ def settings_file
21
+ @settings_file ||= :application
22
+ end
23
+ attr_writer :settings_file
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,76 @@
1
+ module Settingslogic
2
+ # = Setting
3
+ #
4
+ # A simple settings solution using a YAML file. See README for more information.
5
+ class Settings
6
+ class << self
7
+ def name # :nodoc:
8
+ instance._settings.key?("name") ? instance.name : super
9
+ end
10
+
11
+ # Resets the singleton instance. Useful if you are changing the configuration on the fly. If you are changing the configuration on the fly you should consider creating instances.
12
+ def reset!
13
+ @instance = nil
14
+ end
15
+
16
+ private
17
+ def instance
18
+ @instance ||= new
19
+ end
20
+
21
+ def method_missing(name, *args, &block)
22
+ instance.send(name, *args, &block)
23
+ end
24
+ end
25
+
26
+ attr_accessor :_settings
27
+
28
+ # Initializes a new settings object. You can initialize an object in any of the following ways:
29
+ #
30
+ # Settings.new(:application) # will look for config/application.yml
31
+ # Settings.new("application.yaml") # will look for application.yaml
32
+ # Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
33
+ # Settings.new(:config1 => 1, :config2 => 2)
34
+ #
35
+ # Basically if you pass a symbol it will look for that file in the configs directory of your rails app, if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
36
+ # Then you can pass a hash, and it just allows you to access the hash via methods.
37
+ def initialize(name_or_hash = Config.settings_file)
38
+ case name_or_hash
39
+ when Hash
40
+ self._settings = name_or_hash
41
+ when String, Symbol
42
+ root_path = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/" : ""
43
+ file_path = name_or_hash.is_a?(Symbol) ? "#{root_path}#{name_or_hash}.yml" : name_or_hash
44
+ self._settings = YAML.load(ERB.new(File.read(file_path)).result)
45
+ self._settings = _settings[RAILS_ENV] if defined?(RAILS_ENV)
46
+ else
47
+ raise ArgumentError.new("Your settings must be a hash or a name of a file via a String or a Symbol")
48
+ end
49
+ define_settings!
50
+ end
51
+
52
+ private
53
+ def method_missing(name, *args, &block)
54
+ raise NoMethodError.new("no configuration was specified for #{name}")
55
+ end
56
+
57
+ def define_settings!
58
+ _settings.each do |key, value|
59
+ case value
60
+ when Hash
61
+ instance_eval <<-"end_eval", __FILE__, __LINE__
62
+ def #{key}
63
+ @#{key} ||= self.class.new(_settings["#{key}"])
64
+ end
65
+ end_eval
66
+ else
67
+ instance_eval <<-"end_eval", __FILE__, __LINE__
68
+ def #{key}
69
+ @#{key} ||= _settings["#{key}"]
70
+ end
71
+ end_eval
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,79 @@
1
+ # (The MIT License)
2
+ #
3
+ # Copyright (c) 2008 Jamis Buck <jamis@37signals.com>,
4
+ # with modifications by Bruce Williams <bruce@fiveruns.com>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # 'Software'), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ module Settingslogic
25
+ # = Version
26
+ #
27
+ # A class for describing the current version of a library. The version
28
+ # consists of three parts: the +major+ number, the +minor+ number, and the
29
+ # +tiny+ (or +patch+) number.
30
+ class Version
31
+
32
+ include Comparable
33
+
34
+ # A convenience method for instantiating a new Version instance with the
35
+ # given +major+, +minor+, and +tiny+ components.
36
+ def self.[](major, minor, tiny)
37
+ new(major, minor, tiny)
38
+ end
39
+
40
+ attr_reader :major, :minor, :tiny
41
+
42
+ # Create a new Version object with the given components.
43
+ def initialize(major, minor, tiny)
44
+ @major, @minor, @tiny = major, minor, tiny
45
+ end
46
+
47
+ # Compare this version to the given +version+ object.
48
+ def <=>(version)
49
+ to_i <=> version.to_i
50
+ end
51
+
52
+ # Converts this version object to a string, where each of the three
53
+ # version components are joined by the '.' character. E.g., 2.0.0.
54
+ def to_s
55
+ @to_s ||= [@major, @minor, @tiny].join(".")
56
+ end
57
+
58
+ # Converts this version to a canonical integer that may be compared
59
+ # against other version objects.
60
+ def to_i
61
+ @to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
62
+ end
63
+
64
+ def to_a
65
+ [@major, @minor, @tiny]
66
+ end
67
+
68
+ MAJOR = 0
69
+ MINOR = 9
70
+ TINY = 0
71
+
72
+ # The current version as a Version instance
73
+ CURRENT = new(MAJOR, MINOR, TINY)
74
+ # The current version as a String
75
+ STRING = CURRENT.to_s
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{settingslogic}
3
+ s.version = "0.9.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Ben Johnson of Binary Logic"]
7
+ s.date = %q{2008-11-02}
8
+ s.description = %q{Simple and straightforward application wide settings}
9
+ s.email = %q{bjohnson@binarylogic.com}
10
+ s.extra_rdoc_files = ["CHANGELOG.rdoc", "lib/settingslogic/config.rb", "lib/settingslogic/settings.rb", "lib/settingslogic/version.rb", "lib/settingslogic.rb", "README.rdoc"]
11
+ s.files = ["CHANGELOG.rdoc", "init.rb", "lib/settingslogic/config.rb", "lib/settingslogic/settings.rb", "lib/settingslogic/version.rb", "lib/settingslogic.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "test/application.yml", "test/application2.yml", "test/test_config.rb", "test/test_helper.rb", "test/test_setting.rb", "settingslogic.gemspec"]
12
+ s.has_rdoc = true
13
+ s.homepage = %q{http://github.com/binarylogic/settingslogic}
14
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Settingslogic", "--main", "README.rdoc"]
15
+ s.require_paths = ["lib"]
16
+ s.rubyforge_project = %q{settingslogic}
17
+ s.rubygems_version = %q{1.2.0}
18
+ s.summary = %q{Simple and straightforward application wide settings}
19
+ s.test_files = ["test/test_config.rb", "test/test_helper.rb", "test/test_setting.rb"]
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 2
24
+
25
+ if current_version >= 3 then
26
+ s.add_development_dependency(%q<echoe>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<echoe>, [">= 0"])
29
+ end
30
+ else
31
+ s.add_dependency(%q<echoe>, [">= 0"])
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ setting1:
2
+ setting1_child: saweet
3
+
4
+ setting2: 5
5
+ setting3: <%= 5 * 5 %>
@@ -0,0 +1,6 @@
1
+ neat:
2
+ cool:
3
+ awesome: <%= "Ben" + "Johnson" %>
4
+
5
+ silly: 5
6
+ fun: <%= 5 * 5 %>
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestConfig < Test::Unit::TestCase
4
+ def test_settings_file
5
+ Settingslogic::Config.configure do |config|
6
+ config.settings_file = File.dirname(__FILE__) + '/application2.yml'
7
+ end
8
+
9
+ Settings.reset!
10
+ assert_equal "BenJohnson", Settings.neat.cool.awesome
11
+ assert_equal 5, Settings.silly
12
+ assert_equal 25, Settings.fun
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "ruby-debug"
4
+ require File.dirname(__FILE__) + "/../lib/settingslogic"
5
+
6
+ class Test::Unit::TestCase
7
+ def configure
8
+ Settingslogic::Config.configure do |config|
9
+ config.settings_file = File.dirname(__FILE__) + "/application.yml"
10
+ end
11
+ end
12
+
13
+ def setup
14
+ configure
15
+ end
16
+
17
+ def teardown
18
+ configure
19
+ Settingslogic::Settings.reset!
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSetting < Test::Unit::TestCase
4
+ def test_singleton_access
5
+ assert_equal Settings, Settings.setting1.class
6
+ assert_equal "saweet", Settings.setting1.setting1_child
7
+ assert_equal 5, Settings.setting2
8
+ assert_equal 25, Settings.setting3
9
+ end
10
+
11
+ def test_instances
12
+ settings1 = Settings.new(File.dirname(__FILE__) + '/application.yml')
13
+ assert_equal "saweet", settings1.setting1.setting1_child
14
+ assert_equal 5, settings1.setting2
15
+ assert_equal 25, settings1.setting3
16
+
17
+ settings2 = Settings.new(File.dirname(__FILE__) + '/application2.yml')
18
+ assert_equal "BenJohnson", settings2.neat.cool.awesome
19
+ assert_equal 5, settings2.silly
20
+ assert_equal 25, settings2.fun
21
+ end
22
+
23
+ def test_method_missing
24
+ assert_raise(NoMethodError) { Settings.doesnt_exist }
25
+ settings1 = Settings.new(File.dirname(__FILE__) + '/application.yml')
26
+ assert_raise(NoMethodError) { settings1.doesnt_exist }
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settingslogic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Johnson of Binary Logic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-02 01:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Simple and straightforward application wide settings
26
+ email: bjohnson@binarylogic.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - CHANGELOG.rdoc
33
+ - lib/settingslogic/config.rb
34
+ - lib/settingslogic/settings.rb
35
+ - lib/settingslogic/version.rb
36
+ - lib/settingslogic.rb
37
+ - README.rdoc
38
+ files:
39
+ - CHANGELOG.rdoc
40
+ - init.rb
41
+ - lib/settingslogic/config.rb
42
+ - lib/settingslogic/settings.rb
43
+ - lib/settingslogic/version.rb
44
+ - lib/settingslogic.rb
45
+ - Manifest
46
+ - MIT-LICENSE
47
+ - Rakefile
48
+ - README.rdoc
49
+ - test/application.yml
50
+ - test/application2.yml
51
+ - test/test_config.rb
52
+ - test/test_helper.rb
53
+ - test/test_setting.rb
54
+ - settingslogic.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/binarylogic/settingslogic
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --line-numbers
60
+ - --inline-source
61
+ - --title
62
+ - Settingslogic
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "1.2"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project: settingslogic
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: Simple and straightforward application wide settings
86
+ test_files:
87
+ - test/test_config.rb
88
+ - test/test_helper.rb
89
+ - test/test_setting.rb