sugar-high 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,10 +9,9 @@ begin
9
9
  gem.email = "kmandrup@gmail.com"
10
10
  gem.homepage = "http://github.com/kristianmandrup/sugar-high"
11
11
  gem.authors = ["Kristian Mandrup"]
12
- gem.add_development_dependency "rspec", ">= 2.4.1"
12
+ gem.add_development_dependency "rspec", ">= 2.4.1"
13
+ # gem.add_dependency "require_all", "~> 1.2.0"
13
14
 
14
- gem.add_dependency "require_all", "~> 1.2.0"
15
- gem.add_dependency "mocha", ">= 0.9.8"
16
15
  end
17
16
  Jeweler::GemcutterTasks.new
18
17
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.4.0
data/lib/sugar-high.rb CHANGED
@@ -1,2 +0,0 @@
1
- require 'require_all'
2
- require File.dirname(__FILE__) + '/sugar-high'
@@ -1,24 +1,34 @@
1
- class NilClass
1
+ class NilClass #:nodoc:
2
2
  def blank?
3
3
  true
4
4
  end
5
-
6
- alias_method :wblank?, :blank?
7
5
  alias_method :empty?, :blank?
6
+ end
7
+
8
+ class FalseClass #:nodoc:
9
+ def blank?
10
+ true
11
+ end
12
+ end
8
13
 
9
- def any?
14
+ class TrueClass #:nodoc:
15
+ def blank?
10
16
  false
11
17
  end
12
18
  end
13
19
 
20
+ class Array #:nodoc:
21
+ alias_method :blank?, :empty?
22
+ end
23
+
24
+ class Hash #:nodoc:
25
+ alias_method :blank?, :empty?
26
+ end
27
+
14
28
  class String
15
- def wblank?
16
- self.strip == ''
17
- end
18
-
19
- if !defined? ::Rails
29
+ if !"".respond_to? :blank
20
30
  def blank?
21
- self.strip == ''
31
+ self !~ /\S/
22
32
  end
23
33
  end
24
34
  end
@@ -76,10 +76,23 @@ class String
76
76
  def valid_file_command?
77
77
  self.to_sym.valid_file_command?
78
78
  end
79
+
80
+ def file
81
+ return ::File.new(self) if ::File.file?(self)
82
+ raise "No file found at #{self}"
83
+ end
84
+
85
+ def dir
86
+ return ::Dir.new(self) if ::File.directory?(self)
87
+ raise "No file found at #{self}"
88
+ end
79
89
 
80
90
  def new_file
81
- return File.new(self) if File.exist?(self)
82
- File.open(self, 'w')
91
+ begin
92
+ file
93
+ rescue
94
+ File.open(self, 'w')
95
+ end
83
96
  end
84
97
  end
85
98
 
@@ -140,9 +140,10 @@ class File
140
140
  marker_found = (File.new(file.path).read =~ /#{marker}/)
141
141
  return nil if !marker_found
142
142
 
143
- Mutate.mutate_file file.path, marker, place do
143
+ res = Mutate.mutate_file file.path, marker, place do
144
144
  content
145
145
  end
146
+ res
146
147
  end
147
148
 
148
149
  module EscapedString
@@ -155,11 +156,11 @@ class File
155
156
  def self.get_marker marker
156
157
  return marker if marker.respond_to?(:escaped?) && marker.escaped?
157
158
  marker = case marker
159
+ when Regexp
160
+ marker
158
161
  when String
159
162
  Regexp.escape(marker).extend(EscapedString)
160
- when Regexp
161
- marker.source
162
- end
163
+ end
163
164
  end
164
165
 
165
166
  def self.content options = {}, *args, &block
@@ -187,12 +188,14 @@ class File
187
188
  end
188
189
 
189
190
  marker = Insert.get_marker marker
191
+
190
192
  marker_found = (File.new(file.path).read =~ /#{marker}/)
191
193
  return nil if !marker_found
192
194
 
193
195
  replace_in_file file, /(#{marker})/mi do |match|
194
196
  place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
195
197
  end
198
+ true
196
199
  end
197
200
 
198
201
  def self.replace_in_file(path, regexp, *args, &block)
@@ -1,6 +1,10 @@
1
1
  require "active_support/inflector"
2
2
 
3
3
  class Object
4
+ def kinda_file?
5
+ any_kind_of?(File, Dir)
6
+ end
7
+
4
8
  def any_kind_of? *kinds
5
9
  kinds.all_kinds.each do |kind|
6
10
  return true if self.kind_of? kind
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Fixtures::Application.initialize!
File without changes
@@ -2,6 +2,10 @@ Fixtures::Application.routes.draw do
2
2
 
3
3
 
4
4
 
5
+
6
+
7
+
8
+
5
9
  resources :users
6
10
 
7
11
  # The priority is based upon order of creation:
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'rspec/core'
2
2
  require 'sugar-high'
3
3
 
4
4
  RSpec.configure do |config|
5
- config.mock_with :mocha
5
+ # config.mock_with :mocha
6
6
  end
7
7
 
8
8
  def fixtures_dir
@@ -10,25 +10,11 @@ describe "SugarHigh" do
10
10
  end
11
11
  end
12
12
 
13
- describe '#wblank' do
14
- it "nil and empty string should be blank" do
15
- nil.wblank?.should be_true
16
- ' '.wblank?.should be_true
17
- end
18
- end
19
-
20
13
  describe '#empty' do
21
14
  it "nil and empty string should be empty" do
22
15
  nil.empty?.should be_true
23
16
  ''.empty?.should be_true
24
17
  end
25
-
26
- context 'Array' do
27
- it "nil and empty array should not have any" do
28
- nil.any?.should be_false
29
- [].any?.should be_false
30
- end
31
- end
32
18
  end
33
19
  end
34
20
  end
@@ -8,6 +8,7 @@ describe "SugarHigh::File" do
8
8
  let(:replace_file) { fixture_file 'file.txt' }
9
9
  let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
10
10
  let(:routes_file) { fixture_file 'routes_file.rb' }
11
+ let(:app_file) { fixture_file 'application_file.rb' }
11
12
 
12
13
  after do
13
14
  File.overwrite class_file do
@@ -15,7 +16,16 @@ describe "SugarHigh::File" do
15
16
  def begin
16
17
  end
17
18
  end}
18
- end
19
+ end
20
+
21
+ File.overwrite app_file do
22
+ %q{# Load the rails application
23
+ require File.expand_path('../application', __FILE__)
24
+
25
+ # Initialize the rails application
26
+ Fixtures::Application.initialize!
27
+ }
28
+ end
19
29
  end
20
30
 
21
31
  before :each do
@@ -31,7 +41,7 @@ end}
31
41
  File.exist?(file_to_delete).should be_false
32
42
  end
33
43
  end
34
-
44
+
35
45
  describe '#delete_file! (class)' do
36
46
  it 'should delete file' do
37
47
  File.overwrite(file_to_delete) do
@@ -41,7 +51,7 @@ end}
41
51
  File.exist?(file_to_delete).should be_false
42
52
  end
43
53
  end
44
-
54
+
45
55
  describe '#delete! (instance)' do
46
56
  it 'should delete file' do
47
57
  File.overwrite(file_to_delete) do
@@ -51,7 +61,7 @@ end}
51
61
  File.exist?(file_to_delete).should be_false
52
62
  end
53
63
  end
54
-
64
+
55
65
  describe '#delete_file! (instance)' do
56
66
  it 'should delete file' do
57
67
  File.overwrite(file_to_delete) do
@@ -61,7 +71,7 @@ end}
61
71
  File.exist?(file_to_delete).should be_false
62
72
  end
63
73
  end
64
-
74
+
65
75
  describe '#append with :content option' do
66
76
  let(:append_file) { fixture_file 'file.txt' }
67
77
 
@@ -74,7 +84,7 @@ end}
74
84
  content.should match /Hello You/
75
85
  content.should match /Appended/
76
86
  end
77
-
87
+
78
88
  it 'should append content to existing file - instance method' do
79
89
  File.overwrite(append_file) do
80
90
  'Hello You'
@@ -85,7 +95,7 @@ end}
85
95
  content.should match /Appended/
86
96
  end
87
97
  end
88
-
98
+
89
99
  describe '#append with block' do
90
100
  let(:append_file) { fixture_file 'file.txt' }
91
101
 
@@ -100,7 +110,7 @@ end}
100
110
  content.should match /Hello You/
101
111
  content.should match /Appended/
102
112
  end
103
-
113
+
104
114
  it "should append content to existing file using block arg - instance method" do
105
115
  File.overwrite(append_file) do
106
116
  'Hello You'
@@ -125,7 +135,7 @@ end}
125
135
  File.replace_content_from replace_file, :where => 'You', :with => 'Me'
126
136
  File.read(replace_file).should_not match /You/
127
137
  end
128
-
138
+
129
139
  it 'should remove content from existing file - instance method #replace_content' do
130
140
  File.overwrite(replace_file) do
131
141
  'Hello You'
@@ -145,7 +155,7 @@ end}
145
155
  File.remove_content_from replace_file, :where => 'You'
146
156
  File.read(replace_file).should_not match /You/
147
157
  end
148
-
158
+
149
159
  it "should remove content from existing file - instance method #remove_content" do
150
160
  File.overwrite(replace_file) do
151
161
  'Hello You'
@@ -153,7 +163,7 @@ end}
153
163
  File.new(replace_file).remove_content :where => 'You'
154
164
  File.read(replace_file).should_not match /You/
155
165
  end
156
-
166
+
157
167
  end
158
168
 
159
169
  describe '#remove_content_from with :content option' do
@@ -166,7 +176,7 @@ end}
166
176
  File.remove_content_from replace_file, :content => 'You'
167
177
  File.read(replace_file).should_not match /You/
168
178
  end
169
-
179
+
170
180
  it "should remove content from existing file - instance method #remove_content" do
171
181
  File.overwrite(replace_file) do
172
182
  'Hello You'
@@ -208,7 +218,7 @@ end}
208
218
  end
209
219
  File.read(replace_file).should_not match /You/
210
220
  end
211
-
221
+
212
222
  it "should remove content from existing file - instance method #remove" do
213
223
  File.overwrite(replace_file) do
214
224
  'Hello You'
@@ -223,53 +233,62 @@ end}
223
233
 
224
234
  describe '#insert_into' do
225
235
  let(:insertion_file) { fixture_file 'insertion.txt' }
226
-
236
+
227
237
  before :each do
228
238
  File.overwrite(insertion_file) do
229
239
  'Goodbye'
230
240
  end
231
241
  end
232
-
242
+
233
243
  after :each do
234
244
  File.delete insertion_file if File.file?(insertion_file)
235
245
  end
236
-
246
+
237
247
  it "should insert Hello before Goodbye using a block as content" do
238
248
  File.insert_into insertion_file, :before => 'Goodbye' do
239
249
  'Hello'
240
250
  end
241
251
  File.read(insertion_file).should match /Hello\s+Goodbye/
242
252
  end
243
-
253
+
244
254
  it "should insert Hello before Goodbye using a block as content - instance method #insert" do
245
255
  File.new(insertion_file).insert :before => 'Goodbye' do
246
256
  'Hello'
247
257
  end
248
258
  File.read(insertion_file).should match /Hello\s+Goodbye/
249
259
  end
250
-
251
-
260
+
261
+
252
262
  it "should insert Hello before Goodbye using a content string arg" do
253
263
  File.insert_into insertion_file, "Hello ", :before => 'Goodbye'
254
264
  File.read(insertion_file).should match /Hello\s+Goodbye/
255
265
  end
256
-
266
+
257
267
  it "should insert Hello before Goodbye using a :content option" do
258
268
  File.insert_into insertion_file, :content => 'Hello', :before => 'Goodbye'
259
269
  File.read(insertion_file).should match /Hello\s+Goodbye/
260
270
  end
261
-
271
+
262
272
  it "should insert Hello before Goodbye using a block as content to insert" do
263
273
  File.insert_into insertion_file, :before => 'Goodbye' do
264
274
  'Hello'
265
275
  end
266
276
  File.read(insertion_file).should match /Hello\s+Goodbye/
267
277
  end
268
-
278
+
269
279
  it "should insert Hello after Goodbye using a :with option and a Regexp for the after expression" do
270
280
  File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
271
281
  File.read(insertion_file).should match /Goodbye\s+Hello/
272
282
  end
283
+
284
+ it "should insert Hello after Goodbye using a :with option and a Regexp for the after expression" do
285
+
286
+ pattern = Regexp.escape('::Application.initialize!')
287
+ File.insert_into app_file, :after => /\w+#{pattern}/ do
288
+ 'hello'
289
+ end
290
+ File.read(app_file).should match /hello/
291
+ end
273
292
 
274
293
  it "should insert Hello before last end statement" do
275
294
  File.insert_into class_file, :content => ' # Hello', :before_last => 'end'
@@ -277,14 +296,14 @@ end}
277
296
  File.read(class_file).should match /end\s+# Hello\s+end/
278
297
  File.remove_content_from class_file, :content => ' # Hello'
279
298
  end
280
-
299
+
281
300
  it "should insert Hello before last end statement but don't repeat" do
282
301
  File.insert_into class_file, :content => ' # Hello', :before_last => 'end', :repeat => true
283
302
  puts File.read(class_file)
284
303
  File.read(class_file).should match /end\s+# Hello\s+end/
285
304
  File.remove_content_from class_file, :content => ' # Hello'
286
305
  end
287
-
306
+
288
307
  it "should insert Hello before last end statement - block" do
289
308
  File.insert_into class_file, :before_last => 'end' do
290
309
  ' # Hello'
@@ -293,7 +312,7 @@ end}
293
312
  File.read(class_file).should match /end\s+# Hello\s+end/
294
313
  File.remove_content_from class_file, :content => ' # Hello'
295
314
  end
296
-
315
+
297
316
  it "should insert devise routes statement as first route statement" do
298
317
  File.insert_into routes_file, :after => 'routes.draw do' do
299
318
  'devise :users'
@@ -112,7 +112,7 @@ describe "SugarHigh" do
112
112
 
113
113
  describe "Array" do
114
114
  describe '#file_names' do
115
- let(:replace_file) { fixture_file 'file.txt' }
115
+ let(:replace_file) { fixture_file 'file.txt' }
116
116
 
117
117
  before :each do
118
118
  File.delete replace_file if File.file?(replace_file)
@@ -127,17 +127,29 @@ describe "SugarHigh" do
127
127
 
128
128
  describe "String" do
129
129
  describe '#new_file' do
130
- let(:replace_file) { fixture_file 'file.txt' }
131
-
132
- after :each do
133
- File.delete replace_file if File.file?(replace_file)
134
- end
130
+ let(:class_file) { fixture_file 'class_file.txt' }
135
131
 
136
- it "should return all file names of an array of paths to files" do
137
- replace_file.new_file
138
- File.exist?(replace_file).should be_true
132
+ it "should get the existing file" do
133
+ class_file.new_file.path.should =~ /class_file/
134
+ end
135
+ end
136
+
137
+ describe '#file' do
138
+ let(:class_file) { fixture_file 'class_file.txt' }
139
+
140
+ it "should get the file" do
141
+ class_file.file.path.should =~ /class_file/
142
+ end
143
+ end
144
+
145
+ describe '#dir' do
146
+ it "should get the dir" do
147
+ fix_path = fixtures_dir.path
148
+ # puts fix_path
149
+ # puts fix_path.dir
150
+ fix_path.dir.path.should =~ /fixtures/
139
151
  end
140
152
  end
141
- end
153
+ end
142
154
  end
143
155
 
data/sugar-high.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sugar-high}
8
- s.version = "0.3.7"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2011-02-28}
12
+ s.date = %q{2011-03-02}
13
13
  s.description = %q{More Ruby sugar - inspired by the 'zuker' project}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -43,7 +43,9 @@ Gem::Specification.new do |s|
43
43
  "lib/sugar-high/rspec/matchers/have_aliases.rb",
44
44
  "lib/sugar-high/string.rb",
45
45
  "sandbox/test_routes_mutate.rb",
46
+ "spec/fixtures/application_file.rb",
46
47
  "spec/fixtures/class_file.rb",
48
+ "spec/fixtures/class_file.txt",
47
49
  "spec/fixtures/empty.txt",
48
50
  "spec/fixtures/non-empty.txt",
49
51
  "spec/fixtures/routes_file.rb",
@@ -67,9 +69,10 @@ Gem::Specification.new do |s|
67
69
  ]
68
70
  s.homepage = %q{http://github.com/kristianmandrup/sugar-high}
69
71
  s.require_paths = ["lib"]
70
- s.rubygems_version = %q{1.5.2}
72
+ s.rubygems_version = %q{1.5.3}
71
73
  s.summary = %q{Ruby convenience sugar packs!}
72
74
  s.test_files = [
75
+ "spec/fixtures/application_file.rb",
73
76
  "spec/fixtures/class_file.rb",
74
77
  "spec/fixtures/routes_file.rb",
75
78
  "spec/spec_helper.rb",
@@ -94,17 +97,11 @@ Gem::Specification.new do |s|
94
97
 
95
98
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
96
99
  s.add_development_dependency(%q<rspec>, [">= 2.4.1"])
97
- s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
98
- s.add_runtime_dependency(%q<mocha>, [">= 0.9.8"])
99
100
  else
100
101
  s.add_dependency(%q<rspec>, [">= 2.4.1"])
101
- s.add_dependency(%q<require_all>, ["~> 1.2.0"])
102
- s.add_dependency(%q<mocha>, [">= 0.9.8"])
103
102
  end
104
103
  else
105
104
  s.add_dependency(%q<rspec>, [">= 2.4.1"])
106
- s.add_dependency(%q<require_all>, ["~> 1.2.0"])
107
- s.add_dependency(%q<mocha>, [">= 0.9.8"])
108
105
  end
109
106
  end
110
107
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugar-high
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-28 00:00:00.000000000 +01:00
12
+ date: 2011-03-02 00:00:00.000000000 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2158375700 !ruby/object:Gem::Requirement
17
+ requirement: &2164404920 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,29 +22,7 @@ dependencies:
22
22
  version: 2.4.1
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2158375700
26
- - !ruby/object:Gem::Dependency
27
- name: require_all
28
- requirement: &2158375100 !ruby/object:Gem::Requirement
29
- none: false
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: 1.2.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: *2158375100
37
- - !ruby/object:Gem::Dependency
38
- name: mocha
39
- requirement: &2158374580 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ! '>='
43
- - !ruby/object:Gem::Version
44
- version: 0.9.8
45
- type: :runtime
46
- prerelease: false
47
- version_requirements: *2158374580
25
+ version_requirements: *2164404920
48
26
  description: More Ruby sugar - inspired by the 'zuker' project
49
27
  email: kmandrup@gmail.com
50
28
  executables: []
@@ -79,7 +57,9 @@ files:
79
57
  - lib/sugar-high/rspec/matchers/have_aliases.rb
80
58
  - lib/sugar-high/string.rb
81
59
  - sandbox/test_routes_mutate.rb
60
+ - spec/fixtures/application_file.rb
82
61
  - spec/fixtures/class_file.rb
62
+ - spec/fixtures/class_file.txt
83
63
  - spec/fixtures/empty.txt
84
64
  - spec/fixtures/non-empty.txt
85
65
  - spec/fixtures/routes_file.rb
@@ -121,11 +101,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
101
  version: '0'
122
102
  requirements: []
123
103
  rubyforge_project:
124
- rubygems_version: 1.5.2
104
+ rubygems_version: 1.5.3
125
105
  signing_key:
126
106
  specification_version: 3
127
107
  summary: Ruby convenience sugar packs!
128
108
  test_files:
109
+ - spec/fixtures/application_file.rb
129
110
  - spec/fixtures/class_file.rb
130
111
  - spec/fixtures/routes_file.rb
131
112
  - spec/spec_helper.rb