simple_config 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,33 @@
1
+ # ~.autotest
2
+ require 'autotest_notification'
3
+ SPEAKING = false
4
+ DOOM_EDITION = false
5
+ BUUF = true
6
+ PENDING = false
7
+ STICKY = false
8
+ SUCCESS_SOUND = ''
9
+ FAILURE_SOUND = ''
10
+
11
+ Autotest.add_hook :initialize do |at|
12
+ at.clear_exceptions()
13
+ at.add_mapping(%r%^spec/.*rb$%) {|filename, _|
14
+ filename
15
+ }
16
+
17
+ at.add_mapping(%r%^lib/.*rb$%, true) {|filename, _|
18
+
19
+ basename = File.basename(filename, '.rb')
20
+
21
+ at.files_matching(%r%^spec/#{basename}_spec.rb$%)
22
+ }
23
+
24
+ at.add_exception(%r%^*autotest*%)
25
+ at.add_exception(%r%^*document*%)
26
+ at.add_exception(%r%^*Gemfile*%)
27
+ at.add_exception(%r%^*gitignore*%)
28
+ at.add_exception(%r%^*LICENSE*%)
29
+ at.add_exception(%r%^*Rakefile*%)
30
+ at.add_exception(%r%^*README*%)
31
+ at.add_exception(%r%^*rspec*%)
32
+ nil
33
+ end
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.1.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.1)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.1.0)
13
+ rspec-core (~> 2.1.0)
14
+ rspec-expectations (~> 2.1.0)
15
+ rspec-mocks (~> 2.1.0)
16
+ rspec-core (2.1.0)
17
+ rspec-expectations (2.1.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.1.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.1)
27
+ rcov
28
+ rspec (~> 2.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Valeriy Prokopchuk
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,19 @@
1
+ = simple_config
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to simple_config
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Valeriy Prokopchuk. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "simple_config"
16
+ gem.homepage = "http://github.com/vprokopchuk256/simple_config"
17
+ gem.license = "MIT"
18
+ gem.summary = "Simple configuration utility that allows reading config data as a file properties"
19
+ gem.description = "Simle confuguration utilies"
20
+ gem.email = "vprokopchuk@gmail.com"
21
+ gem.authors = ["Valeriy Prokopchuk"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : "1.0.0"
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "simple_config #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,33 @@
1
+ class SimpleConfig
2
+ class << self
3
+ attr_writer :directory
4
+
5
+ def directory
6
+ @directory ||= 'config'
7
+ end
8
+
9
+ attr_writer :default
10
+
11
+ def default
12
+ @default ||= :config
13
+ end
14
+
15
+ def method_missing method_name, *args
16
+ if (File.exists?(full_file_name = full_file_name(method_name)))
17
+ data(full_file_name)
18
+ else
19
+ data(full_file_name(@default)).send(method_name, *args)
20
+ end
21
+ end
22
+
23
+ def full_file_name file_name
24
+ File.join(@directory, "#{file_name}.yml")
25
+ end
26
+ private :full_file_name
27
+
28
+ def data full_file_name
29
+ SimpleStruct.create(YAML.load_file(full_file_name))
30
+ end
31
+ private :data
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ require 'ostruct'
2
+
3
+ class SimpleStruct
4
+
5
+ def initialize hash
6
+ @a = hash.inject({}){ |res, (key, value)| res[key] = SimpleStruct.create(value); res }
7
+ end
8
+
9
+ def [](key)
10
+ @a[real_key(key)]
11
+ end
12
+
13
+ def []=(key, value)
14
+ @a[real_key(key)] = value
15
+ end
16
+
17
+ def method_missing method_name, *args
18
+ return self[method_name.to_s.delete("=")] = args.first if method_name[-1] == "="
19
+ return self[method_name] if @a.has_key?(method_name)
20
+
21
+ super method_name, *args
22
+ end
23
+
24
+ def real_key key
25
+ @a.keys.find { |k| k.to_s == key.to_s} || key
26
+ end
27
+ private :real_key
28
+
29
+ class << self
30
+ def create obj
31
+ obj.is_a?(Hash) ? SimpleStruct.new(obj) : obj
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simple_config}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Valeriy Prokopchuk"]
12
+ s.date = %q{2010-12-02}
13
+ s.description = %q{Simle confuguration utilies}
14
+ s.email = %q{vprokopchuk@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".autotest",
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/simple_config.rb",
30
+ "lib/simple_struct.rb",
31
+ "simple_config.gemspec",
32
+ "spec/simple_config_spec.rb",
33
+ "spec/simple_struct_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/vprokopchuk256/simple_config}
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Simple configuration utility that allows reading config data as a file properties}
41
+ s.test_files = [
42
+ "spec/simple_config_spec.rb",
43
+ "spec/simple_struct_spec.rb",
44
+ "spec/spec_helper.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
55
+ s.add_development_dependency(%q<rcov>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleConfig do
4
+ it "should have 'config' as default directory" do
5
+ SimpleConfig.directory.should == 'config'
6
+ end
7
+
8
+ it "should have 'config.yml' as default config file" do
9
+ SimpleConfig.default.should == :config
10
+ end
11
+
12
+ describe "configuration file exists" do
13
+ before :each do
14
+ File.open( '/tmp/test.yml', 'w+' ) do |out|
15
+ YAML.dump( {:key1 => "value1", :key2 => "value2"}, out )
16
+ end
17
+
18
+ SimpleConfig.directory = "/tmp"
19
+ end
20
+
21
+ it "should return SimpleStruct as file content" do
22
+ SimpleConfig.test.should be_kind_of(SimpleStruct)
23
+ end
24
+
25
+ describe "default file" do
26
+ before :each do
27
+ SimpleConfig.default = :test
28
+ end
29
+
30
+ it "should be accessed if there is no file with required property name" do
31
+ SimpleConfig.key1.should == "value1"
32
+ end
33
+ end
34
+
35
+ after :each do
36
+ File.delete("/tmp/test.yml")
37
+ end
38
+ end
39
+
40
+ describe "configuration file does not exist" do
41
+ it "should raise exception" do
42
+ lambda {
43
+ SimpleConfig.some_wrong_file
44
+ }.should raise_error
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleStruct do
4
+ describe "create" do
5
+ it "should return Simple Struct if hash is passed as parameter" do
6
+ SimpleStruct.create({ :key => "value" }).should be_kind_of(SimpleStruct)
7
+ end
8
+
9
+ it "should return the same value if passed value is not hash" do
10
+ SimpleStruct.create("Some value").should == "Some value"
11
+ end
12
+
13
+ it "converts iternal hashed into the SimpleStructs" do
14
+ SimpleStruct.create({ :key => "value", :key1 => {} })[:key1].should be_kind_of(SimpleStruct)
15
+ end
16
+ end
17
+
18
+ describe "indexer" do
19
+ before :each do
20
+ @simple_struct = SimpleStruct.create({ :key => "value" })
21
+ end
22
+
23
+ describe "read" do
24
+ describe "existing key" do
25
+ it "index - like" do
26
+ @simple_struct[:key].should == "value"
27
+ end
28
+
29
+ it "does not depends on key type" do
30
+ @simple_struct["key"].should == "value"
31
+ end
32
+
33
+ it "as property" do
34
+ @simple_struct.key.should == "value"
35
+ end
36
+ end
37
+
38
+ describe "not existing property" do
39
+ it "return nil if accessed via indexer" do
40
+ @simple_struct[:key1].should be_nil
41
+ end
42
+
43
+ it "raise an exception if accessed like property" do
44
+ lambda {
45
+ @simple_struct.key1
46
+ }.should raise_error
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "write" do
52
+ it "index - like" do
53
+ @simple_struct[:key] = "value1"
54
+ @simple_struct[:key].should == "value1"
55
+ end
56
+
57
+ it "does not depends on key type" do
58
+ @simple_struct["key"] = "value1"
59
+ @simple_struct[:key].should == "value1"
60
+ end
61
+
62
+ it "as property" do
63
+ @simple_struct.key = "value1"
64
+ @simple_struct.key.should == "value1"
65
+ @simple_struct[:key].should == "value1"
66
+ end
67
+ end
68
+ end
69
+
70
+
71
+ end
72
+
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'simple_struct'
5
+ require 'simple_config'
6
+ require 'yaml'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_config
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Valeriy Prokopchuk
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-02 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 5
60
+ - 1
61
+ version: 1.5.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rcov
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ description: Simle confuguration utilies
79
+ email: vprokopchuk@gmail.com
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files:
85
+ - LICENSE.txt
86
+ - README.rdoc
87
+ files:
88
+ - .autotest
89
+ - .document
90
+ - .rspec
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/simple_config.rb
98
+ - lib/simple_struct.rb
99
+ - simple_config.gemspec
100
+ - spec/simple_config_spec.rb
101
+ - spec/simple_struct_spec.rb
102
+ - spec/spec_helper.rb
103
+ has_rdoc: true
104
+ homepage: http://github.com/vprokopchuk256/simple_config
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 917526611
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ requirements: []
130
+
131
+ rubyforge_project:
132
+ rubygems_version: 1.3.7
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Simple configuration utility that allows reading config data as a file properties
136
+ test_files:
137
+ - spec/simple_config_spec.rb
138
+ - spec/simple_struct_spec.rb
139
+ - spec/spec_helper.rb