tg_config 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ script: "rake spec"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ notifications:
7
+ recipients:
8
+ - wael.nasreddine@gmail.com
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tg_config.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'bundler' do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard 'rspec', :version => 2 do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ module TechnoGate
2
+ module TgConfig
3
+ TgConfigError = Class.new Exception
4
+
5
+ NotReadableError = Class.new TgConfigError
6
+ NotDefinedError = Class.new TgConfigError
7
+ NotWritableError = Class.new TgConfigError
8
+ NotValidError = Class.new TgConfigError
9
+ IsEmptyError = Class.new TgConfigError
10
+ ConfigFileNotSetError = Class.new TgConfigError
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module TechnoGate
2
+ module TgConfig
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+ PRE = ''
7
+
8
+ def self.version
9
+ # Init the version
10
+ version = [MAJOR, MINOR, TINY]
11
+ # Add the pre if available
12
+ version << PRE unless PRE.nil? || PRE !~ /\S/
13
+ # Return the version joined by a dot
14
+ version.join('.')
15
+ end
16
+ end
17
+ end
data/lib/tg_config.rb ADDED
@@ -0,0 +1,102 @@
1
+ require "yaml"
2
+ require "active_support/core_ext/hash/indifferent_access"
3
+ require "tg_config/errors"
4
+ require "tg_config/version"
5
+
6
+ module TechnoGate
7
+ module TgConfig
8
+ extend self
9
+
10
+ # Define the config class variable
11
+ @@config = nil
12
+
13
+ # Define the config file
14
+ @@config_file = nil
15
+
16
+ # Return a particular config variable from the parsed config file
17
+ #
18
+ # @param [String|Symbol] config
19
+ # @return mixed
20
+ # @raise [Void]
21
+ def [](config)
22
+ if @@config.nil?
23
+ check_config_file
24
+ @@config ||= parse_config_file
25
+ end
26
+
27
+ @@config.send(:[], config)
28
+ end
29
+
30
+ # Update the config file
31
+ #
32
+ # @param [String] config
33
+ # @param [Mixed] Values
34
+ def []=(config, value)
35
+ if @@config.nil?
36
+ check_config_file
37
+ @@config ||= parse_config_file
38
+ end
39
+
40
+ @@config.send(:[]=, config, value)
41
+ end
42
+
43
+ # Get the config file
44
+ #
45
+ # @return [String] Absolute path to the config file
46
+ def config_file
47
+ raise ConfigFileNotSetError unless @@config_file
48
+
49
+ @@config_file
50
+ end
51
+
52
+ # Set the config file
53
+ #
54
+ # @param [String] Absolute path to the config file
55
+ def config_file=(config_file)
56
+ @@config_file = config_file
57
+ end
58
+
59
+ # Save the config file
60
+ def save
61
+ # Make sure the config file is writable
62
+ check_config_file(true)
63
+ # Write the config file
64
+ write_config_file
65
+ end
66
+
67
+ protected
68
+ # Initialize the configuration file
69
+ def initialize_config_file
70
+ File.open(config_file, 'w') do |f|
71
+ f.write ""
72
+ end
73
+ end
74
+
75
+ # Check the config file
76
+ def check_config_file(writable = false)
77
+ # Check that config_file is defined
78
+ raise NotDefinedError unless config_file
79
+ # Check that the config file exists
80
+ initialize_config_file unless ::File.exists?(config_file)
81
+ # Check that the config file is readable?
82
+ raise NotReadableError unless ::File.readable?(config_file)
83
+ # Checl that the Config file is writable?
84
+ raise NotWritableError unless ::File.writable?(config_file) if writable
85
+ end
86
+
87
+ # Parse the config file
88
+ #
89
+ # @return [HashWithIndifferentAccess] The config
90
+ def parse_config_file
91
+ YAML.load_file(config_file).with_indifferent_access
92
+ end
93
+
94
+ # Write the config file
95
+ def write_config_file
96
+ raise IsEmptyError unless @@config
97
+ File.open config_file, 'w' do |f|
98
+ f.write(@@config.to_hash.to_yaml)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,231 @@
1
+ require 'spec_helper'
2
+
3
+ describe TgConfig do
4
+ before(:each) do
5
+ @config = {:submodules => [:pathogen]}
6
+ @config_path = '/valid/path'
7
+ @invalid_config_path = '/invalid/path'
8
+ YAML.stubs(:load_file).with(@config_path).returns(@config)
9
+ TgConfig.send(:class_variable_set, :@@config_file, @config_path)
10
+
11
+ ::File.stubs(:exists?).with(@config_path).returns(true)
12
+ ::File.stubs(:readable?).with(@config_path).returns(true)
13
+ ::File.stubs(:writable?).with(@config_path).returns(true)
14
+
15
+ ::File.stubs(:exists?).with(@invalid_config_path).returns(false)
16
+ ::File.stubs(:readable?).with(@invalid_config_path).returns(false)
17
+ ::File.stubs(:writable?).with(@invalid_config_path).returns(false)
18
+
19
+ @file_handler = mock "file handler"
20
+ @file_handler.stubs(:write)
21
+
22
+ ::File.stubs(:open).with(@config_path, 'w').yields(@file_handler)
23
+ end
24
+
25
+ describe "@@config" do
26
+ it "should have a class_variable @@config" do
27
+ lambda { subject.send(:class_variable_get, :@@config) }.should_not raise_error NameError
28
+ end
29
+ end
30
+
31
+ describe "@@config_file" do
32
+ it "should have a class_variable @@config_file" do
33
+ lambda {subject.send(:class_variable_get, :@@config_file) }.should_not raise_error NameError
34
+ end
35
+ end
36
+
37
+ describe "#config_file" do
38
+ it { should respond_to :config_file }
39
+
40
+ it "should return @@config_file" do
41
+ subject.send(:class_variable_set, :@@config_file, @invalid_config_path)
42
+
43
+ subject.config_file.should == @invalid_config_path
44
+ end
45
+
46
+ it "should raise ConfigFileNotSetError if @@config_file is not set" do
47
+ subject.send(:class_variable_set, :@@config_file, nil)
48
+
49
+ lambda { subject.config_file }.should raise_error TgConfig::ConfigFileNotSetError
50
+ end
51
+ end
52
+
53
+ describe "#config_file=" do
54
+ it { should respond_to :config_file= }
55
+
56
+ it "should set @@config_file" do
57
+ subject.config_file = @invalid_config_path
58
+ subject.config_file.should == @invalid_config_path
59
+ end
60
+ end
61
+
62
+ describe "#check_config_file" do
63
+ before(:each) do
64
+ TgConfig.stubs(:initialize_config_file)
65
+ end
66
+
67
+ it { should respond_to :check_config_file }
68
+
69
+ it "should call File.exists?" do
70
+ ::File.expects(:exists?).with(@config_path).returns(true).once
71
+
72
+ subject.send(:check_config_file)
73
+ end
74
+
75
+ it "should call File.readable?" do
76
+ ::File.expects(:readable?).with(@config_path).returns(true).once
77
+
78
+ subject.send(:check_config_file)
79
+ end
80
+
81
+ it "should call File.writable?" do
82
+ ::File.expects(:writable?).with(@config_path).returns(true).once
83
+
84
+ subject.send(:check_config_file, true)
85
+ end
86
+
87
+ it "should not call File.writable? if no arguments were passed" do
88
+ ::File.expects(:writable?).with(@config_path).returns(true).never
89
+
90
+ subject.send(:check_config_file)
91
+ end
92
+
93
+ it "should raise TgConfig::NotReadableError if config not readable" do
94
+ TgConfig.stubs(:config_file).returns(@invalid_config_path)
95
+ ::File.stubs(:readable?).with(@invalid_config_path).returns(false)
96
+
97
+ lambda { subject.send(:check_config_file) }.should raise_error TgConfig::NotReadableError
98
+ end
99
+
100
+ it "should raise TgConfig::NotWritableError if config not readable" do
101
+ TgConfig.stubs(:config_file).returns(@config_path)
102
+ ::File.stubs(:writable?).with(@config_path).returns(false)
103
+
104
+ lambda { subject.send(:check_config_file, true) }.should raise_error TgConfig::NotWritableError
105
+ end
106
+
107
+ end
108
+
109
+ describe "#initialize_config_file" do
110
+ it { should respond_to :initialize_config_file }
111
+
112
+ it "should be able to create the config file from the template" do
113
+ config_file = mock
114
+ config_file.expects(:write).once
115
+ File.expects(:open).with(TgConfig.config_file, 'w').yields(config_file).once
116
+
117
+ subject.send :initialize_config_file
118
+ end
119
+ end
120
+
121
+ describe "#parse_config_file" do
122
+ before(:each) do
123
+ TgConfig.send(:class_variable_set, :@@config, nil)
124
+ TgConfig.stubs(:initialize_config_file)
125
+ end
126
+
127
+ it { should respond_to :parse_config_file }
128
+
129
+ it "should parse the config file and return an instance of HashWithIndifferentAccess" do
130
+ subject.send(:parse_config_file).should be_instance_of HashWithIndifferentAccess
131
+ end
132
+ end
133
+
134
+ describe "#[]" do
135
+ before(:each) do
136
+ TgConfig.send(:class_variable_set, :@@config, nil)
137
+ TgConfig.stubs(:initialize_config_file)
138
+ end
139
+
140
+ it "should call check_config_file" do
141
+ TgConfig.expects(:check_config_file).once
142
+
143
+ subject[:submodules]
144
+ end
145
+
146
+ it "should call parse_config_file" do
147
+ TgConfig.expects(:parse_config_file).returns(@config).once
148
+
149
+ subject[:submodules]
150
+ end
151
+ end
152
+
153
+ describe "#[]=" do
154
+ after(:each) do
155
+ TgConfig.send(:class_variable_set, :@@config, nil)
156
+ end
157
+
158
+ it { should respond_to :[]= }
159
+
160
+ it "should set the new config in @@config" do
161
+ subject[:submodules] = [:pathogen, :github]
162
+ subject.send(:class_variable_get, :@@config)[:submodules].should ==
163
+ [:pathogen, :github]
164
+ end
165
+ end
166
+
167
+ describe "#write_config_file" do
168
+ before(:each) do
169
+ subject.send(:class_variable_set, :@@config, @config)
170
+ subject.send(:class_variable_get, :@@config).stubs(:to_hash).returns(@config)
171
+ end
172
+
173
+ it { should respond_to :write_config_file }
174
+
175
+ it "should call to_hash on @@config" do
176
+ subject.send(:class_variable_get, :@@config).expects(:to_hash).returns(@config).once
177
+
178
+ subject.send :write_config_file
179
+ end
180
+
181
+ it "should call to_yaml on @@config.to_hash" do
182
+ @config.expects(:to_yaml).returns(@config.to_yaml).twice # => XXX: Why twice ?
183
+ subject.send(:class_variable_get, :@@config).stubs(:to_hash).returns(@config)
184
+
185
+ subject.send :write_config_file
186
+ end
187
+
188
+ it "should call File.open with config_file" do
189
+ ::File.expects(:open).with(@config_path, 'w').yields(@file_handler).once
190
+
191
+ subject.send :write_config_file
192
+ end
193
+
194
+ it "should write the yaml contents to the config file" do
195
+ @file_handler.expects(:write).with(@config.to_yaml).once
196
+ ::File.stubs(:open).with(@config_path, 'w').yields(@file_handler)
197
+
198
+ subject.send :write_config_file
199
+ end
200
+
201
+ it "should raise TgConfig::IsEmptyError" do
202
+ subject.send(:class_variable_set, :@@config, nil)
203
+
204
+ lambda { subject.send :write_config_file }.should raise_error TgConfig::IsEmptyError
205
+ end
206
+ end
207
+
208
+ describe "#save" do
209
+ before(:each) do
210
+ subject.send(:class_variable_set, :@@config, @config)
211
+ subject.send(:class_variable_get, :@@config).stubs(:to_hash).returns(@config)
212
+ end
213
+
214
+ it { should respond_to :save }
215
+
216
+ it "should call check_config_file to make sure it is writable" do
217
+ TgConfig.expects(:check_config_file).with(true).once
218
+
219
+ subject.save
220
+ end
221
+
222
+ it "should call write_config_file" do
223
+ TgConfig.expects(:write_config_file).once
224
+
225
+ subject.save
226
+ end
227
+
228
+ it "should clear the cache" do
229
+ end
230
+ end
231
+ end
@@ -0,0 +1,16 @@
1
+ $:.push File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rspec'
3
+ require 'tg_config'
4
+
5
+ include TechnoGate
6
+
7
+ RSpec.configure do |config|
8
+ # == Mock Framework
9
+ #
10
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
11
+ #
12
+ config.mock_with :mocha
13
+ # config.mock_with :flexmock
14
+ # config.mock_with :rr
15
+ # config.mock_with :rspec
16
+ end
data/tg_config.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tg_config/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tg_config"
7
+ s.version = TechnoGate::TgConfig.version
8
+ s.authors = ["Wael Nasreddine"]
9
+ s.email = ["wael.nasreddine@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{A simple YAML configuration reader and writer}
12
+ s.description = s.summary
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
20
+
21
+ ####
22
+ # Run-time dependencies
23
+ ####
24
+
25
+ # Active Support
26
+ s.add_dependency 'activesupport', '~>3.1.1'
27
+
28
+ ####
29
+ # Development dependencies
30
+ ####
31
+
32
+ # Guard
33
+ s.add_development_dependency 'guard', '~>0.8.4'
34
+ s.add_development_dependency 'guard-bundler', '~>0.1.3'
35
+ s.add_development_dependency 'guard-rspec', '~>0.4.5'
36
+
37
+ # Documentation
38
+ s.add_development_dependency 'yard', '~>0.7.2'
39
+
40
+ ####
41
+ # Development / Test dependencies
42
+ ####
43
+
44
+ # RSpec / Capybara
45
+ s.add_development_dependency 'rspec', '~>2.6.0'
46
+
47
+ # Mocha
48
+ s.add_development_dependency 'mocha', '~>0.10.0'
49
+
50
+ ####
51
+ # Debugging
52
+ ####
53
+ s.add_development_dependency 'pry', '~>0.9.6.2'
54
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tg_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Wael Nasreddine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &14181700 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *14181700
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &14177540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.4
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *14177540
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-bundler
38
+ requirement: &14175700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *14175700
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &14185340 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.5
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *14185340
58
+ - !ruby/object:Gem::Dependency
59
+ name: yard
60
+ requirement: &14204580 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 0.7.2
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *14204580
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &14200620 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *14200620
80
+ - !ruby/object:Gem::Dependency
81
+ name: mocha
82
+ requirement: &14233720 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.10.0
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *14233720
91
+ - !ruby/object:Gem::Dependency
92
+ name: pry
93
+ requirement: &14231720 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 0.9.6.2
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *14231720
102
+ description: A simple YAML configuration reader and writer
103
+ email:
104
+ - wael.nasreddine@gmail.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - .gitignore
110
+ - .rspec
111
+ - .travis.yml
112
+ - Gemfile
113
+ - Guardfile
114
+ - Rakefile
115
+ - lib/tg_config.rb
116
+ - lib/tg_config/errors.rb
117
+ - lib/tg_config/version.rb
118
+ - spec/lib/tg_config_spec.rb
119
+ - spec/spec_helper.rb
120
+ - tg_config.gemspec
121
+ homepage: ''
122
+ licenses: []
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: 1.8.7
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: -2304515669427187554
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.11
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: A simple YAML configuration reader and writer
148
+ test_files:
149
+ - spec/lib/tg_config_spec.rb
150
+ - spec/spec_helper.rb
151
+ has_rdoc: