konfigurator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +72 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/konfigurator.rb +141 -0
- data/test/conf.yml +6 -0
- data/test/helper.rb +7 -0
- data/test/test_konfigurator.rb +96 -0
- metadata +92 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kriss Kowalik
|
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,72 @@
|
|
1
|
+
= Konfigurator
|
2
|
+
|
3
|
+
Konfigurator is a configuration toolkit strongly inspired by Sinatra framework
|
4
|
+
settings. Thanks to it you can easy implement configuration options to your apps,
|
5
|
+
modules or classes. Take a look at simple example.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
You can install Konfigurator simply using rubygems:
|
10
|
+
|
11
|
+
sudo gem install konfigurator
|
12
|
+
|
13
|
+
...or install it from source:
|
14
|
+
|
15
|
+
git clone git://github.com/nu7hatch/konfigurator.git
|
16
|
+
cd konfigurator
|
17
|
+
rake install
|
18
|
+
|
19
|
+
== Basic usage
|
20
|
+
|
21
|
+
Konfigurator is very easy to use. Take a look at simple example.
|
22
|
+
|
23
|
+
class MyClass do
|
24
|
+
include Konfigurator
|
25
|
+
|
26
|
+
set :foo, "bar"
|
27
|
+
enable :bar
|
28
|
+
disable :bla
|
29
|
+
|
30
|
+
configure :production
|
31
|
+
enable :bla
|
32
|
+
set :spam, "eggs!"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Now you can get configured options directly from your class:
|
37
|
+
|
38
|
+
MyClass.foo # => "bar"
|
39
|
+
MyClass.bar # => true
|
40
|
+
MyClass.bla # => false
|
41
|
+
|
42
|
+
... or when current environment is set to <tt>:production</tt>:
|
43
|
+
|
44
|
+
MyClass.bla # => true
|
45
|
+
MyClass.spam # => "eggs!"
|
46
|
+
|
47
|
+
All settings are also available from objects via <tt>#settings</tt> method:
|
48
|
+
|
49
|
+
obj = MyObject.new
|
50
|
+
obj.settings.foo # => "bar"
|
51
|
+
obk.settings.bar # => true
|
52
|
+
|
53
|
+
<b>Remember!</b> when option is not set then <tt>NoMethodError</tt> will be
|
54
|
+
raised after try to get it direcly from class, eg:
|
55
|
+
|
56
|
+
MyObject.set :exist
|
57
|
+
MyObject.exist # => true
|
58
|
+
MyObject.not_exist # => will raise NoMethodError
|
59
|
+
|
60
|
+
== Note on Patches/Pull Requests
|
61
|
+
|
62
|
+
* Fork the project.
|
63
|
+
* Make your feature addition or bug fix.
|
64
|
+
* Add tests for it. This is important so I don't break it in a
|
65
|
+
future version unintentionally.
|
66
|
+
* Commit, do not mess with rakefile, version, or history.
|
67
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
68
|
+
* Send me a pull request. Bonus points for topic branches.
|
69
|
+
|
70
|
+
== Copyright
|
71
|
+
|
72
|
+
Copyright (c) 2010 Kriss 'nu7hatch' Kowalik. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "konfigurator"
|
8
|
+
gem.summary = %Q{Configuration toolkit inspired by Sinatra settings.}
|
9
|
+
gem.description = <<-DESCR
|
10
|
+
Konfigurator is a configuration toolkit strongly ispired by Sinatra framework
|
11
|
+
settings. Thanks to it you can easy implement configuration options to your apps,
|
12
|
+
modules or classes.
|
13
|
+
DESCR
|
14
|
+
gem.email = "kriss.kowalik@gmail.com"
|
15
|
+
gem.homepage = "http://github.com/nu7hatch/konfigurator"
|
16
|
+
gem.authors = ["Kriss 'nu7hatch' Kowalik"]
|
17
|
+
gem.add_development_dependency "contest", ">= 0.1.2"
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'rcov/rcovtask'
|
33
|
+
Rcov::RcovTask.new do |test|
|
34
|
+
test.libs << 'test'
|
35
|
+
test.pattern = 'test/**/test_*.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
rescue LoadError
|
39
|
+
task :rcov do
|
40
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "Trolley #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/konfigurator.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
# Konfigurator is a configuration toolkit strongly inspired by Sinatra framework
|
4
|
+
# settings. Thanks to it you can easy implement configuration options to your apps,
|
5
|
+
# modules or classes. Take a look at simple example.
|
6
|
+
#
|
7
|
+
# class MyClass do
|
8
|
+
# set :foo, "bar"
|
9
|
+
# enable :bar
|
10
|
+
# disable :bla
|
11
|
+
#
|
12
|
+
# configure :production
|
13
|
+
# enable :bla
|
14
|
+
# set :spam, "eggs!"
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Now you can get configured options directly from your class:
|
19
|
+
#
|
20
|
+
# MyClass.foo # => "bar"
|
21
|
+
# MyClass.bar # => true
|
22
|
+
# MyClass.bla # => false
|
23
|
+
#
|
24
|
+
# ... or when current environment is set to <tt>:production</tt>:
|
25
|
+
#
|
26
|
+
# MyClass.bla # => true
|
27
|
+
# MyClass.spam # => "eggs!"
|
28
|
+
#
|
29
|
+
# All settings are also available from objects via <tt>#settings</tt> method:
|
30
|
+
#
|
31
|
+
# obj = MyObject.new
|
32
|
+
# obj.settings.foo # => "bar"
|
33
|
+
# obk.settings.bar # => true
|
34
|
+
#
|
35
|
+
# <b>Remember!</b> when option is not set then <tt>NoMethodError</tt> will be
|
36
|
+
# raised after try to get it direcly from class, eg:
|
37
|
+
#
|
38
|
+
# MyObject.set :exist
|
39
|
+
# MyObject.exist # => true
|
40
|
+
# MyObject.not_exist # => will raise NoMethodError
|
41
|
+
module Konfigurator
|
42
|
+
|
43
|
+
def self.included(base) # :nodoc:
|
44
|
+
base.send(:extend, ClassMethods)
|
45
|
+
base.send(:include, InstanceMethods)
|
46
|
+
end
|
47
|
+
|
48
|
+
module InstanceMethods
|
49
|
+
# Access to configuration options is defined in the class methods, so it's
|
50
|
+
# only syntactic sugar for <tt>self.class</tt>.
|
51
|
+
def settings
|
52
|
+
self.class
|
53
|
+
end
|
54
|
+
alias :config :settings
|
55
|
+
end # InstanceMethods
|
56
|
+
|
57
|
+
module ClassMethods
|
58
|
+
# Run once, at startup, in any environment. To add an option use the
|
59
|
+
# <tt>set</tt> method (For boolean values You can use <tt>#enable</tt> and
|
60
|
+
# <tt>#disable</tt> methods):
|
61
|
+
#
|
62
|
+
# configure do
|
63
|
+
# set :foo, 'bar'
|
64
|
+
# enable :bar
|
65
|
+
# disable :yadayada
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# Run only when the environment (or the <tt>APP_ENV</tt> env variable)is set
|
69
|
+
# to <tt>:production</tt>:
|
70
|
+
#
|
71
|
+
# configure :production do
|
72
|
+
# ...
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# Run when the environment is set to either <tt>:production</tt> or <tt>:test</tt>:
|
76
|
+
#
|
77
|
+
# configure :production, :test do
|
78
|
+
# ...
|
79
|
+
# end
|
80
|
+
def configure(*envs, &block)
|
81
|
+
class_eval(&block) if envs.empty? || envs.include?(env.to_sym)
|
82
|
+
end
|
83
|
+
|
84
|
+
# It loads settings from given <tt>.yml</tt> file. File should have structure
|
85
|
+
# like this one:
|
86
|
+
#
|
87
|
+
# development:
|
88
|
+
# foo: bar
|
89
|
+
# bar: true
|
90
|
+
# production:
|
91
|
+
# bla: foobar
|
92
|
+
def load_settings(fname)
|
93
|
+
conf = YAML.load_file(fname)
|
94
|
+
conf[env.to_s].each {|k,v| set k.to_sym, v }
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns hash with defined configuration options.
|
98
|
+
def settings
|
99
|
+
@settings ||= {}
|
100
|
+
end
|
101
|
+
alias :config :settings
|
102
|
+
|
103
|
+
# It "enables" given setting. It means that it assigns <tt>true</tt> value
|
104
|
+
# to the specified setting key.
|
105
|
+
#
|
106
|
+
# enable :foo # => set :foo, true
|
107
|
+
def enable(name)
|
108
|
+
set(name, true)
|
109
|
+
end
|
110
|
+
|
111
|
+
# It "disables" given setting. It means that it assigns <tt>false</tt> value
|
112
|
+
# to the specified setting key.
|
113
|
+
#
|
114
|
+
# disable :foo # => set :foo, false
|
115
|
+
def disable(name)
|
116
|
+
set(name, false)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Assigns given value to the specified setting key.
|
120
|
+
#
|
121
|
+
# set :foo, "YadaYadaYaday!"
|
122
|
+
# set :bae, true
|
123
|
+
#
|
124
|
+
# See also shortcuts for boolean settings: <tt>#enable</tt> and
|
125
|
+
# <tt>#disable</tt> methods.
|
126
|
+
def set(name, value)
|
127
|
+
name = name.to_sym
|
128
|
+
unless self.respond_to?(name)
|
129
|
+
meta = class << self; self; end
|
130
|
+
meta.send(:define_method, name) { settings[name] }
|
131
|
+
end
|
132
|
+
settings[name] = value
|
133
|
+
end
|
134
|
+
|
135
|
+
# It returns name of current environment.
|
136
|
+
def environment
|
137
|
+
settings[:environment] ||= settings[:env] || ENV["APP_ENV"] || :development
|
138
|
+
end
|
139
|
+
alias :env :environment
|
140
|
+
end # ClassMethods
|
141
|
+
end # Konfigurator
|
data/test/conf.yml
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class Configured
|
4
|
+
include Konfigurator
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestKonfigurator < Test::Unit::TestCase
|
8
|
+
describe "Configured class" do
|
9
|
+
should "respond to #set" do
|
10
|
+
assert Configured.respond_to?(:set)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "respond to #enable" do
|
14
|
+
assert Configured.respond_to?(:enable)
|
15
|
+
end
|
16
|
+
|
17
|
+
should "respond to #disable" do
|
18
|
+
assert Configured.respond_to?(:disable)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "respond to #environment and #env" do
|
22
|
+
assert Configured.respond_to?(:environment)
|
23
|
+
assert Configured.respond_to?(:env)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "respond to #settings and #config" do
|
27
|
+
assert Configured.respond_to?(:settings)
|
28
|
+
assert Configured.respond_to?(:config)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "respond to #configure" do
|
32
|
+
assert Configured.respond_to?(:configure)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "allow to set given option" do
|
36
|
+
assert_equal "bar", Configured.set(:foo, "bar")
|
37
|
+
assert_equal "bar", Configured.settings[:foo]
|
38
|
+
end
|
39
|
+
|
40
|
+
should "allow to quick disable given option" do
|
41
|
+
assert !Configured.disable(:foo)
|
42
|
+
assert !Configured.foo
|
43
|
+
end
|
44
|
+
|
45
|
+
should "allow to quick enable given option" do
|
46
|
+
assert Configured.enable(:foo)
|
47
|
+
assert Configured.foo
|
48
|
+
end
|
49
|
+
|
50
|
+
should "allow for quick access to current environment name" do
|
51
|
+
Configured.set :environment, :production
|
52
|
+
assert_equal :production, Configured.environment
|
53
|
+
end
|
54
|
+
|
55
|
+
should "allow to define configuration specific for current env" do
|
56
|
+
Configured.set :environment, :development
|
57
|
+
Configured.disable :prod
|
58
|
+
Configured.configure :production do
|
59
|
+
enable :prod
|
60
|
+
end
|
61
|
+
Configured.configure :development do
|
62
|
+
enable :dev
|
63
|
+
end
|
64
|
+
Configured.configure :development, :production do
|
65
|
+
set :bar, :foo
|
66
|
+
end
|
67
|
+
assert Configured.dev
|
68
|
+
assert !Configured.prod
|
69
|
+
assert_equal :foo, Configured.bar
|
70
|
+
end
|
71
|
+
|
72
|
+
should "allow to load settings from yaml file" do
|
73
|
+
Configured.set :environment, :production
|
74
|
+
Configured.load_settings(File.dirname(__FILE__)+"/conf.yml")
|
75
|
+
assert Configured.bla
|
76
|
+
assert_equal "bar", Configured.foo
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "Configured object" do
|
81
|
+
setup do
|
82
|
+
@configured = Configured.new
|
83
|
+
end
|
84
|
+
|
85
|
+
should "respond to #settings and #config" do
|
86
|
+
assert @configured.respond_to?(:settings)
|
87
|
+
assert @configured.respond_to?(:config)
|
88
|
+
end
|
89
|
+
|
90
|
+
should "#settings works properly" do
|
91
|
+
Configured.set(:fooo, :bar)
|
92
|
+
@configured = Configured.new
|
93
|
+
assert_equal :bar, @configured.settings.fooo
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konfigurator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kriss 'nu7hatch' Kowalik
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-22 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: contest
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 31
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 1
|
33
|
+
- 2
|
34
|
+
version: 0.1.2
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: " Konfigurator is a configuration toolkit strongly ispired by Sinatra framework\n settings. Thanks to it you can easy implement configuration options to your apps, \n modules or classes. \n"
|
38
|
+
email: kriss.kowalik@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- lib/konfigurator.rb
|
53
|
+
- test/conf.yml
|
54
|
+
- test/helper.rb
|
55
|
+
- test/test_konfigurator.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/nu7hatch/konfigurator
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Configuration toolkit inspired by Sinatra settings.
|
90
|
+
test_files:
|
91
|
+
- test/test_konfigurator.rb
|
92
|
+
- test/helper.rb
|