Skip to content

Commit 8007ac0

Browse files
committedOct 31, 2008
Initial commit
0 parents  commit 8007ac0

16 files changed

+418
-0
lines changed
 

‎.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
*.log
3+
*.sqlite3
4+
pkg/*
5+
coverage/*
6+
doc/*
7+
benchmarks/*
8+

‎CHANGELOG.rdoc

Whitespace-only changes.

‎MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -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.

‎Manifest

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CHANGELOG.rdoc
2+
init.rb
3+
lib/settingasm/config.rb
4+
lib/settingasm/setting.rb
5+
lib/settingasm/version.rb
6+
lib/settingasm.rb
7+
MIT-LICENSE
8+
Rakefile
9+
README.rdoc
10+
test/test_config.rb
11+
test/test_helper.rb
12+
test/test_setting.rb
13+
Manifest

‎README.rdoc

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
= Settingasm
2+
3+
Settingasm is my personal solution to application wide settings that uses an ERB enabled YAML file. It's pretty simple and straightforward, which is why I like it. I decided to go ahead and share it with the world.
4+
5+
So here is my question to you.....is Settingasm a great settings solution or the greatest?
6+
7+
== 1. Configure
8+
9+
Configuration is optional. See Settingasm::Config for more details.
10+
11+
# config/initializers/settingasm.rb
12+
Settingasm::Config.configure do |config|
13+
config.file_name = :config # will look for config/config.yml
14+
config.file_name = "config" # will look for config
15+
config.file_name = "config.yaml" # will look for confg.yaml
16+
config.file_name = "/absolute/path/config.yml" # will look for /absolute/path/config.yaml
17+
18+
config.class_name = "Setting" # use Setting.my_setting instead of Settingasm::Setting.my_setting
19+
end
20+
21+
== 2. Create your settings
22+
23+
This is typical YAML file, notice ERB is allowed.
24+
25+
# app/config/application.yml
26+
defaults: &defaults
27+
cool_setting: this is cool
28+
saweet: nested settings
29+
neat_setting: 24
30+
awesome_setting: <%= "Did you know 5 + 5 = " + (5 + 5) + "?" %>
31+
32+
development:
33+
<<: *defaults
34+
neat_setting: 800
35+
36+
test:
37+
<<: *defaults
38+
39+
production:
40+
<<: *defaults
41+
42+
== Access your settings
43+
44+
I use Setting because I specified it in my configuration. Otherwise you would have to use Settingasm::Setting. Since Setting is such a generic name I let you specify your own in the configuration. You could use anything you want.
45+
46+
>> RAILS_ENV
47+
=> "development"
48+
49+
>> Setting.cool
50+
=> "this is cool"
51+
52+
>> Setting.cool.saweet
53+
=> "nested settings"
54+
55+
>> Setting.new_setting
56+
=> 800
57+
58+
>> Setting.awesome_setting
59+
=> "Did you know 5 + 5 = 10?"
60+
61+
== Multiple settings
62+
63+
settings1 = Setting.new(:settings1) # looks for config/settings1.yml
64+
settings2 = Setting.new("setting2.yaml") # looks for config/settings2.yml
65+
settings3 = Setting.new(:some_setting => "some value")
66+
67+
== Helpful links
68+
69+
* <b>Documentation:</b> http://settingasm.rubyforge.org
70+
* <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/19028-settingasm
71+
72+
73+
Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license

‎Rakefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'rubygems'
2+
require 'echoe'
3+
4+
require File.dirname(__FILE__) << "/lib/settingasm/version"
5+
6+
Echoe.new 'settingasm' do |p|
7+
p.version = Settingasm::Version::STRING
8+
p.author = "Ben Johnson of Binary Logic"
9+
p.email = 'bjohnson@binarylogic.com'
10+
p.project = 'settingasm'
11+
p.summary = "Simple and straightforward application wide settings"
12+
p.url = "http://github.com/binarylogic/settingasm"
13+
p.dependencies = []
14+
p.include_rakefile = true
15+
end

‎init.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "settingasm"

‎lib/settingasm.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require "yaml"
2+
require "erb"
3+
require File.dirname(__FILE__) + "/settingasm/config"
4+
require File.dirname(__FILE__) + "/settingasm/setting"

‎lib/settingasm/config.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Settingasm
2+
# = Config
3+
# Sets configuration on Settingasm.
4+
class Config
5+
class << self
6+
def configure
7+
yield self
8+
end
9+
10+
# The name of the class you want to use to access your settings. Maybe you don't like "Setting" or maybe it will conflict with a model you have. Just set this to "AppSetting" or "Config", whatever you want.
11+
#
12+
# * <tt>Default:</tt> "Setting"
13+
# * <tt>Accepts:</tt> Symbol or String
14+
def class_name
15+
@class_name
16+
end
17+
18+
def class_name=(value) # :nodoc:
19+
eval("::#{value} = Setting")
20+
@class_name = value
21+
end
22+
23+
# 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:
24+
#
25+
# Setting.setting1
26+
# Setting.setting2
27+
# # etc...
28+
#
29+
# All that you need to do is specify the name of the file. It will automatically look in the config directory.
30+
#
31+
# * <tt>Default:</tt> :application
32+
# * <tt>Accepts:</tt> Symbol or String
33+
def settings_file
34+
@settings_file ||= :application
35+
end
36+
attr_writer :settings_file
37+
end
38+
end
39+
end

‎lib/settingasm/setting.rb

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module Settingasm
2+
# = Setting
3+
#
4+
# A simple settings solution using a YAML file. See README for more information.
5+
class Setting
6+
class << self
7+
def name # :nodoc:
8+
if instance._settings.key?("name")
9+
instance.name
10+
else
11+
super
12+
end
13+
end
14+
15+
# Resets the singleton instance. Useful if you are changing the configuration on the fly. If you are changing the configuration on the fly you could consider creating instances.
16+
def reset!
17+
@instance = nil
18+
end
19+
20+
private
21+
def instance
22+
@instance ||= new
23+
end
24+
25+
def method_missing(name, *args, &block)
26+
instance.send(name, *args, &block)
27+
end
28+
end
29+
30+
attr_accessor :_settings
31+
32+
# Initializes a new settings object. You can initialize an object in any of the following ways:
33+
#
34+
# Setting.new(:application) # will look for config/application.yml
35+
# Setting.new("application.yaml") # will look for application.yaml
36+
# Setting.new("/var/configs/application.yml") # will look for /var/configs/application.yml
37+
# Setting.new(:config1 => 1, :config2 => 2)
38+
#
39+
# 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.
40+
# Then you can pass a hash, and it just allows you to access the hash via methods.
41+
def initialize(name_or_hash = Config.settings_file)
42+
case name_or_hash
43+
when Hash
44+
self._settings = name_or_hash
45+
when String, Symbol
46+
root_path = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/" : ""
47+
file_path = name_or_hash.is_a?(Symbol) ? "#{root_path}#{name_or_hash}.yml" : name_or_hash
48+
self._settings = YAML.load(ERB.new(File.read(file_path)).result)
49+
self._settings = _settings[RAILS_ENV] if defined?(RAILS_ENV)
50+
else
51+
raise ArgumentError.new("Your settings must be a hash or a name of a file via a String or a Symbol")
52+
end
53+
define_settings!
54+
end
55+
56+
private
57+
def method_missing(name, *args, &block)
58+
raise NoMethodError.new("no configuration was specified for #{name}")
59+
end
60+
61+
def define_settings!
62+
_settings.each do |key, value|
63+
case value
64+
when Hash
65+
instance_eval <<-"end_eval", __FILE__, __LINE__
66+
def #{key}
67+
@#{key} ||= self.class.new(_settings["#{key}"])
68+
end
69+
end_eval
70+
else
71+
instance_eval <<-"end_eval", __FILE__, __LINE__
72+
def #{key}
73+
@#{key} ||= _settings["#{key}"]
74+
end
75+
end_eval
76+
end
77+
end
78+
end
79+
end
80+
end

‎lib/settingasm/version.rb

+79
Original file line numberDiff line numberDiff line change
@@ -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 Settingasm
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

‎test/application.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setting1:
2+
setting1_child: saweet
3+
4+
setting2: 5
5+
setting3: <%= 5 * 5 %>

‎test/application2.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
neat:
2+
cool:
3+
awesome: <%= "Ben" + "Johnson" %>
4+
5+
silly: 5
6+
fun: <%= 5 * 5 %>

‎test/test_config.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require File.dirname(__FILE__) + '/test_helper.rb'
2+
3+
class TestConfig < Test::Unit::TestCase
4+
def test_class_name
5+
Settingasm::Config.configure do |config|
6+
config.class_name = "Conf"
7+
end
8+
9+
assert_equal Conf, Conf.setting1.class
10+
assert_equal "saweet", Conf.setting1.setting1_child
11+
assert_equal 5, Conf.setting2
12+
assert_equal 25, Conf.setting3
13+
end
14+
15+
def test_settings_file
16+
Settingasm::Config.configure do |config|
17+
config.settings_file = File.dirname(__FILE__) + '/application2.yml'
18+
end
19+
20+
Setting.reset!
21+
assert_equal "BenJohnson", Setting.neat.cool.awesome
22+
assert_equal 5, Setting.silly
23+
assert_equal 25, Setting.fun
24+
end
25+
end

0 commit comments

Comments
 (0)
Please sign in to comment.