tg_config 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,4 +3,38 @@ require "bundler/gem_tasks"
3
3
  require 'rspec/core/rake_task'
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
+ # Monkey patch Bundler::GemHelper
7
+ #
8
+ # Git flow create the tag after finishing a release however this breaks
9
+ # <b>rake release</b> because it expects that no tag for the current version
10
+ # is present, this patch overrides this behaviour to skip version tagging if
11
+ # the tag already exists instead of raising an exception
12
+ Bundler::GemHelper.class_eval <<-END, __FILE__, __LINE__ + 1
13
+ # Tag the current version
14
+ def tag_version
15
+ unless already_tagged?
16
+ sh %(git tag -a -m "Version \#{version}" \#{version_tag})
17
+ Bundler.ui.confirm "Tagged \#{version_tag}"
18
+ end
19
+ yield if block_given?
20
+ rescue
21
+ Bundler.ui.error "Untagged \#{version_tag} due to error"
22
+ sh_with_code "git tag -d \#{version_tag}"
23
+ raise
24
+ end
25
+
26
+ # The original method raises an exception, we should override it
27
+ def guard_already_tagged
28
+ end
29
+
30
+ # This method check if the tag has already been tagged
31
+ def already_tagged?
32
+ if sh('git tag').split(/\n/).include?(version_tag)
33
+ true
34
+ else
35
+ false
36
+ end
37
+ end
38
+ END
39
+
6
40
  task :default => :spec
@@ -1,5 +1,5 @@
1
1
  module TechnoGate
2
- module TgConfig
2
+ class TgConfig
3
3
  TgConfigError = Class.new Exception
4
4
 
5
5
  NotReadableError = Class.new TgConfigError
@@ -1,8 +1,8 @@
1
1
  module TechnoGate
2
- module TgConfig
2
+ class TgConfig
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 1
4
+ MINOR = 1
5
+ TINY = 0
6
6
  PRE = ''
7
7
 
8
8
  def self.version
data/lib/tg_config.rb CHANGED
@@ -4,14 +4,15 @@ require "tg_config/errors"
4
4
  require "tg_config/version"
5
5
 
6
6
  module TechnoGate
7
- module TgConfig
8
- extend self
7
+ class TgConfig
9
8
 
10
- # Define the config class variable
11
- @@config = nil
9
+ attr_reader :config_file
12
10
 
13
- # Define the config file
14
- @@config_file = nil
11
+ def initialize(config_file)
12
+ @config_file = config_file
13
+ check_config_file
14
+ @config = parse_config_file
15
+ end
15
16
 
16
17
  # Return a particular config variable from the parsed config file
17
18
  #
@@ -19,12 +20,7 @@ module TechnoGate
19
20
  # @return mixed
20
21
  # @raise [Void]
21
22
  def [](config)
22
- if @@config.nil?
23
- check_config_file
24
- @@config ||= parse_config_file
25
- end
26
-
27
- @@config.send(:[], config)
23
+ @config.send(:[], config)
28
24
  end
29
25
 
30
26
  # Update the config file
@@ -32,28 +28,7 @@ module TechnoGate
32
28
  # @param [String] config
33
29
  # @param [Mixed] Values
34
30
  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
31
+ @config.send(:[]=, config, value)
57
32
  end
58
33
 
59
34
  # Save the config file
@@ -67,9 +42,8 @@ module TechnoGate
67
42
  protected
68
43
  # Initialize the configuration file
69
44
  def initialize_config_file
70
- File.open(config_file, 'w') do |f|
71
- f.write ""
72
- end
45
+ @config = HashWithIndifferentAccess.new
46
+ write_config_file
73
47
  end
74
48
 
75
49
  # Check the config file
@@ -93,9 +67,9 @@ module TechnoGate
93
67
 
94
68
  # Write the config file
95
69
  def write_config_file
96
- raise IsEmptyError unless @@config
70
+ raise IsEmptyError unless @config
97
71
  File.open config_file, 'w' do |f|
98
- f.write(@@config.to_hash.to_yaml)
72
+ f.write(@config.to_hash.to_yaml)
99
73
  end
100
74
  end
101
75
  end
@@ -1,105 +1,75 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe TgConfig do
4
+ let(:config) { {:submodules => [:pathogen]} }
5
+ let(:config_path) { '/valid/path' }
6
+ let(:invalid_config_path) { '/invalid/path' }
7
+
8
+ subject { TgConfig.new config_path }
9
+
4
10
  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)
11
+ YAML.stubs(:load_file).with(config_path).returns(config)
12
+ TgConfig.send(:instance_variable_set, :@config_file, config_path)
10
13
 
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
+ ::File.stubs(:exists?).with(config_path).returns(true)
15
+ ::File.stubs(:readable?).with(config_path).returns(true)
16
+ ::File.stubs(:writable?).with(config_path).returns(true)
14
17
 
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
+ ::File.stubs(:exists?).with(invalid_config_path).returns(false)
19
+ ::File.stubs(:readable?).with(invalid_config_path).returns(false)
20
+ ::File.stubs(:writable?).with(invalid_config_path).returns(false)
18
21
 
19
22
  @file_handler = mock "file handler"
20
23
  @file_handler.stubs(:write)
21
24
 
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
25
+ ::File.stubs(:open).with(config_path, 'w').yields(@file_handler)
35
26
  end
36
27
 
37
28
  describe "#config_file" do
38
29
  it { should respond_to :config_file }
39
30
 
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= }
31
+ it "should return @config_file" do
32
+ subject.send(:instance_variable_set, :@config_file, invalid_config_path)
55
33
 
56
- it "should set @@config_file" do
57
- subject.config_file = @invalid_config_path
58
- subject.config_file.should == @invalid_config_path
34
+ subject.config_file.should == invalid_config_path
59
35
  end
60
36
  end
61
37
 
62
38
  describe "#check_config_file" do
63
- before(:each) do
64
- TgConfig.stubs(:initialize_config_file)
65
- end
66
-
67
39
  it { should respond_to :check_config_file }
68
40
 
69
41
  it "should call File.exists?" do
70
- ::File.expects(:exists?).with(@config_path).returns(true).once
42
+ ::File.expects(:exists?).with(config_path).returns(true).once
71
43
 
72
44
  subject.send(:check_config_file)
73
45
  end
74
46
 
75
47
  it "should call File.readable?" do
76
- ::File.expects(:readable?).with(@config_path).returns(true).once
48
+ ::File.expects(:readable?).with(config_path).returns(true).once
77
49
 
78
50
  subject.send(:check_config_file)
79
51
  end
80
52
 
81
53
  it "should call File.writable?" do
82
- ::File.expects(:writable?).with(@config_path).returns(true).once
54
+ ::File.expects(:writable?).with(config_path).returns(true).once
83
55
 
84
56
  subject.send(:check_config_file, true)
85
57
  end
86
58
 
87
59
  it "should not call File.writable? if no arguments were passed" do
88
- ::File.expects(:writable?).with(@config_path).returns(true).never
60
+ ::File.expects(:writable?).with(config_path).returns(true).never
89
61
 
90
62
  subject.send(:check_config_file)
91
63
  end
92
64
 
93
65
  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)
66
+ ::File.stubs(:readable?).with(config_path).returns(false)
96
67
 
97
68
  lambda { subject.send(:check_config_file) }.should raise_error TgConfig::NotReadableError
98
69
  end
99
70
 
100
71
  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)
72
+ ::File.stubs(:writable?).with(config_path).returns(false)
103
73
 
104
74
  lambda { subject.send(:check_config_file, true) }.should raise_error TgConfig::NotWritableError
105
75
  end
@@ -109,19 +79,23 @@ describe TgConfig do
109
79
  describe "#initialize_config_file" do
110
80
  it { should respond_to :initialize_config_file }
111
81
 
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
82
+ it "should set @config to an empty HashWithIndifferentAccess" do
83
+ subject.send(:instance_variable_set, :@config, nil)
84
+ subject.send(:initialize_config_file)
85
+ subject.send(:instance_variable_get, :@config).should == HashWithIndifferentAccess.new
86
+ end
116
87
 
117
- subject.send :initialize_config_file
88
+ it "should call :write_config_file" do
89
+ subject.expects(:write_config_file).once
90
+
91
+ subject.send(:initialize_config_file)
118
92
  end
93
+
119
94
  end
120
95
 
121
96
  describe "#parse_config_file" do
122
97
  before(:each) do
123
- TgConfig.send(:class_variable_set, :@@config, nil)
124
- TgConfig.stubs(:initialize_config_file)
98
+ subject.send(:instance_variable_set, :@config, nil)
125
99
  end
126
100
 
127
101
  it { should respond_to :parse_config_file }
@@ -132,74 +106,63 @@ describe TgConfig do
132
106
  end
133
107
 
134
108
  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
109
+ it { should respond_to :[] }
142
110
 
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]
111
+ it "should return [:pathogen] for submodules" do
112
+ subject[:submodules].should == [:pathogen]
150
113
  end
151
114
  end
152
115
 
153
116
  describe "#[]=" do
154
117
  after(:each) do
155
- TgConfig.send(:class_variable_set, :@@config, nil)
118
+ subject.send(:instance_variable_set, :@config, nil)
156
119
  end
157
120
 
158
121
  it { should respond_to :[]= }
159
122
 
160
- it "should set the new config in @@config" do
123
+ it "should set the new config in @config" do
161
124
  subject[:submodules] = [:pathogen, :github]
162
- subject.send(:class_variable_get, :@@config)[:submodules].should ==
125
+ subject.send(:instance_variable_get, :@config)[:submodules].should ==
163
126
  [:pathogen, :github]
164
127
  end
165
128
  end
166
129
 
167
130
  describe "#write_config_file" do
168
131
  before(:each) do
169
- subject.send(:class_variable_set, :@@config, @config)
170
- subject.send(:class_variable_get, :@@config).stubs(:to_hash).returns(@config)
132
+ subject.send(:instance_variable_set, :@config, config)
133
+ subject.send(:instance_variable_get, :@config).stubs(:to_hash).returns(config)
171
134
  end
172
135
 
173
136
  it { should respond_to :write_config_file }
174
137
 
175
- it "should call to_hash on @@config" do
176
- subject.send(:class_variable_get, :@@config).expects(:to_hash).returns(@config).once
138
+ it "should call to_hash on @config" do
139
+ subject.send(:instance_variable_get, :@config).expects(:to_hash).returns(config).once
177
140
 
178
141
  subject.send :write_config_file
179
142
  end
180
143
 
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)
144
+ it "should call to_yaml on @config.to_hash" do
145
+ config.expects(:to_yaml).returns(config.to_yaml).twice # => XXX: Why twice ?
146
+ subject.send(:instance_variable_get, :@config).stubs(:to_hash).returns(config)
184
147
 
185
148
  subject.send :write_config_file
186
149
  end
187
150
 
188
151
  it "should call File.open with config_file" do
189
- ::File.expects(:open).with(@config_path, 'w').yields(@file_handler).once
152
+ ::File.expects(:open).with(config_path, 'w').yields(@file_handler).once
190
153
 
191
154
  subject.send :write_config_file
192
155
  end
193
156
 
194
157
  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)
158
+ @file_handler.expects(:write).with(config.to_yaml).once
159
+ ::File.stubs(:open).with(config_path, 'w').yields(@file_handler)
197
160
 
198
161
  subject.send :write_config_file
199
162
  end
200
163
 
201
164
  it "should raise TgConfig::IsEmptyError" do
202
- subject.send(:class_variable_set, :@@config, nil)
165
+ subject.send(:instance_variable_set, :@config, nil)
203
166
 
204
167
  lambda { subject.send :write_config_file }.should raise_error TgConfig::IsEmptyError
205
168
  end
@@ -207,25 +170,22 @@ describe TgConfig do
207
170
 
208
171
  describe "#save" do
209
172
  before(:each) do
210
- subject.send(:class_variable_set, :@@config, @config)
211
- subject.send(:class_variable_get, :@@config).stubs(:to_hash).returns(@config)
173
+ subject.send(:instance_variable_set, :@config, config)
174
+ subject.send(:instance_variable_get, :@config).stubs(:to_hash).returns(config)
212
175
  end
213
176
 
214
177
  it { should respond_to :save }
215
178
 
216
179
  it "should call check_config_file to make sure it is writable" do
217
- TgConfig.expects(:check_config_file).with(true).once
180
+ TgConfig.any_instance.expects(:check_config_file).with(true).once
218
181
 
219
182
  subject.save
220
183
  end
221
184
 
222
185
  it "should call write_config_file" do
223
- TgConfig.expects(:write_config_file).once
186
+ TgConfig.any_instance.expects(:write_config_file).once
224
187
 
225
188
  subject.save
226
189
  end
227
-
228
- it "should clear the cache" do
229
- end
230
190
  end
231
191
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tg_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &14181700 !ruby/object:Gem::Requirement
16
+ requirement: &21161080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14181700
24
+ version_requirements: *21161080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard
27
- requirement: &14177540 !ruby/object:Gem::Requirement
27
+ requirement: &21174660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.8.4
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14177540
35
+ version_requirements: *21174660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-bundler
38
- requirement: &14175700 !ruby/object:Gem::Requirement
38
+ requirement: &21172060 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.1.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14175700
46
+ version_requirements: *21172060
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-rspec
49
- requirement: &14185340 !ruby/object:Gem::Requirement
49
+ requirement: &21169760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.4.5
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *14185340
57
+ version_requirements: *21169760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &14204580 !ruby/object:Gem::Requirement
60
+ requirement: &21197220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.7.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *14204580
68
+ version_requirements: *21197220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &14200620 !ruby/object:Gem::Requirement
71
+ requirement: &21195700 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 2.6.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *14200620
79
+ version_requirements: *21195700
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: mocha
82
- requirement: &14233720 !ruby/object:Gem::Requirement
82
+ requirement: &21193100 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 0.10.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *14233720
90
+ version_requirements: *21193100
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: pry
93
- requirement: &14231720 !ruby/object:Gem::Requirement
93
+ requirement: &21217660 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: 0.9.6.2
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *14231720
101
+ version_requirements: *21217660
102
102
  description: A simple YAML configuration reader and writer
103
103
  email:
104
104
  - wael.nasreddine@gmail.com
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  segments:
140
140
  - 0
141
- hash: -2304515669427187554
141
+ hash: -2039472653472340343
142
142
  requirements: []
143
143
  rubyforge_project:
144
144
  rubygems_version: 1.8.11