awesome_conf 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ 0.1.0 (November 07, 2010)
2
+
3
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano
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.
@@ -0,0 +1,58 @@
1
+ = AwesomeConf
2
+
3
+ AwesomeConf is a really simple and powerful ruby gem built with performance in mind, it's the easiest and the cleanest way to add some configuration variables to any ruby project.
4
+
5
+ It does almost nothing, it just give you the ability to create and use easily a configuration class for any of your ruby project.
6
+
7
+ It's like an Hash but it's not an Hash.
8
+
9
+ It's like an OpenStruct but it's not an OpenStruct, it's faster.
10
+
11
+ It's like a bird, but it's not a bird.
12
+
13
+ == AwesomeConf example
14
+
15
+ require 'awesome_conf'
16
+
17
+ ac = AwesomeConf.new(:key => 'value')
18
+ ac.key # => 'value'
19
+
20
+ Ok, nothing is magic here, it can be any easy ruby tricks returning the value of this hash, like using method_missing[http://apidock.com/ruby/Kernel/method_missing] or using an OpenStruct[http://apidock.com/ruby/OpenStruct].
21
+ But this is not, it's a real brand new method created dynamically returning a constant.
22
+
23
+ <b>Why? For speed, and to be as faster as hash[:key] and two times faster than hash['key']</b> (at least using the classic ruby VM 1.8 and 1.9, with jRuby: hash['key'] is faster than everything !)
24
+
25
+ So to conclude, AwesomeConf is just an easy replacement of conf['my_conf_variable'] to conf.my_conf_variable without <b>sacrificing performance</b>. (benchmarks are available in the specs)
26
+
27
+ == Other use cases:
28
+
29
+ === AwesomeConf with YAML
30
+
31
+ AwesomeConf.new('path_to_a_yaml_file.yaml')
32
+
33
+
34
+ === AwesomeConf and strict mode
35
+
36
+ ac = AwesomeConf.new({:key => 'value'})
37
+ ac.key # => 'value'
38
+ ac.something_not_set # => nil
39
+
40
+ # After enabling the strict mode:
41
+
42
+ ac.strict!
43
+ ac.something_not_set # Raise an exception
44
+
45
+ === AwesomeConf with inheritance
46
+
47
+ class MyConfClass < AwesomeConf
48
+
49
+ def initialize(an_hash)
50
+ super(an_hash)
51
+ end
52
+
53
+ ... your_awesome_custom_methods ...
54
+
55
+ end
56
+
57
+
58
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano, released under the MIT license
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+
5
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
6
+
7
+
8
+ desc "Run specs for current Rails version"
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.spec_files = spec_files
11
+ t.spec_opts = ["-c --format specdoc"]
12
+ end
13
+
14
+ task :default => :spec
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'awesome_conf'
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ class AwesomeConf
4
+ @@strict = false
5
+
6
+ # Create a new conf object
7
+ # You can instantiate a conf object with an Hash
8
+ # Or with the path of a YAML file
9
+ def initialize(hash)
10
+
11
+ begin
12
+ hash = YAML::load_file(hash) if String === hash
13
+ rescue Exception => e
14
+ raise e.class.new("AwesomeConf: Error loading yaml file: #{hash} (#{e})")
15
+ end
16
+
17
+ m = Module.new do
18
+ instance_methods.each { |selector| remove_method(selector) }
19
+
20
+ hash.each do |k, v|
21
+ const_set('AC_' + k.to_s, v)
22
+ module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
23
+ def #{k}
24
+ #{'AC_'+ k.to_s}
25
+ end
26
+ END_EVAL
27
+ end
28
+
29
+ end
30
+
31
+ extend m
32
+ freeze
33
+ end
34
+
35
+ # Set to strict in order to raise when using an unfound conf variable
36
+ def strict!
37
+ @@strict = true
38
+ self
39
+ end
40
+
41
+ def method_missing(m)
42
+ raise "AwesomeConf: trying to access an inexisting configuration variable in a strict configuration environment: #{m}" if @@strict
43
+ nil
44
+ end
45
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe :awesome_conf do
4
+ context 'initialization' do
5
+ it 'with a hash' do
6
+ AwesomeConf.new(:key => 'value').key.should == 'value'
7
+ end
8
+
9
+ it 'with a yaml path file' do
10
+ path = File.join(File.dirname(__FILE__), '../conf/sample.yml')
11
+ AwesomeConf.new(path).key.should == 'value'
12
+ end
13
+
14
+ it 'raise correctly with a mal formed yaml file' do
15
+ path = File.join(File.dirname(__FILE__), '../conf/mal_formed.yml')
16
+ e = nil
17
+ begin
18
+ AwesomeConf.new(path)
19
+ rescue Exception => e
20
+ end
21
+ e.to_s.index('AwesomeConf: Error').should_not be_nil
22
+ end
23
+
24
+ it 'raise correctly with a non extising yaml file' do
25
+ e = nil
26
+ begin
27
+ AwesomeConf.new('not_existing.yaml')
28
+ rescue Exception => e
29
+ end
30
+ e.to_s.index('AwesomeConf: Error').should_not be_nil
31
+ end
32
+
33
+ end
34
+
35
+ context 'normal behavior' do
36
+ before :all do
37
+ path = File.join(File.dirname(__FILE__), '../conf/sample.yml')
38
+ @ac = AwesomeConf.new(path)
39
+ end
40
+
41
+ it 'works with a string' do
42
+ @ac.key.should == 'value'
43
+ end
44
+
45
+ it 'works with a symbol' do
46
+ @ac.sym_key.should == 'value'
47
+ @ac.sym_key_s.should == :sym_value
48
+ end
49
+
50
+ it 'works with an array' do
51
+ @ac.array.last.should == 3
52
+ @ac.array[0].should == 1
53
+ end
54
+
55
+ it 'works with a hash' do
56
+ @ac.hash['k1'].should == 'val'
57
+ @ac.hash[:k2].should == 'val2'
58
+ end
59
+
60
+
61
+ end
62
+
63
+ context 'method missing behavior' do
64
+ it 'should return nil when a method does not exists' do
65
+ AwesomeConf.new(:key => 'value').no.should == nil
66
+ end
67
+
68
+ it 'should raise when a method does not exists in a strict env' do
69
+ e = nil
70
+ begin
71
+ AwesomeConf.new(:key => 'value').strict!.no
72
+ rescue Exception => e
73
+ end
74
+ e.should_not be_nil
75
+ end
76
+
77
+ end
78
+
79
+ context 'benchmark (500 000 times)' do
80
+ @h = {:key => 'value', 'kes' => 'value_s'}
81
+ @a = AwesomeConf.new(@h)
82
+
83
+ class ProxyConf
84
+ def initialize(h)
85
+ @h = h
86
+ end
87
+
88
+ def key
89
+ @h[:key]
90
+ end
91
+ end
92
+
93
+ @c = ProxyConf.new(@h)
94
+
95
+ b_h_s = Benchmark.measure { 500_000.times { @h[:key] } }.to_s.strip
96
+ b_h_str = Benchmark.measure { 500_000.times { @h['kes'] } }.to_s.strip
97
+ b_a = Benchmark.measure { 500_000.times { @a.key } }.to_s.strip
98
+ b_c = Benchmark.measure { 500_000.times { @c.key } }.to_s.strip
99
+
100
+ it "awesome_conf.key: #{b_a}" do
101
+ end
102
+
103
+ it "h[:key]: #{b_h_s}" do
104
+ end
105
+
106
+ it "h['key']: #{b_h_str}" do
107
+ end
108
+
109
+ it "proxy_h_class.key: #{b_c}" do
110
+ end
111
+
112
+ end
113
+ end
@@ -0,0 +1,2 @@
1
+ key: value
2
+ misskey error
@@ -0,0 +1,10 @@
1
+ key: value
2
+ :sym_key: value
3
+ :sym_key_s: :sym_value
4
+ :hash:
5
+ k1: val
6
+ :k2: val2
7
+ array:
8
+ - 1
9
+ - 2
10
+ - 3
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'benchmark'
4
+
5
+
6
+ require File.dirname(__FILE__) + '/../init.rb'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awesome_conf
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Guillaume Luccisano
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-11-07 01:00:00 -07:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: The easiest and the cleanest way to add some configuration variables to any ruby project.
21
+ email: guillaume.luccisano@gmail.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/awesome_conf.rb
30
+ - spec/awesome_conf/awesome_conf_spec.rb
31
+ - spec/conf/mal_formed.yml
32
+ - spec/conf/sample.yml
33
+ - spec/spec_helper.rb
34
+ - CHANGELOG.rdoc
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - README.rdoc
38
+ - init.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/kwi/awesome_conf
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 1
63
+ - 3
64
+ - 4
65
+ version: 1.3.4
66
+ requirements: []
67
+
68
+ rubyforge_project: awesome_conf
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: The easiest and the cleanest way to add some configuration variables to any ruby project.
73
+ test_files: []
74
+