sugar-high 0.4.0 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/VERSION +1 -1
  2. data/lib/sugar-high/array.rb +58 -7
  3. data/lib/sugar-high/class_ext.rb +11 -0
  4. data/lib/sugar-high/enumerable.rb +67 -0
  5. data/lib/sugar-high/file.rb +24 -76
  6. data/lib/sugar-high/file_ext.rb +65 -0
  7. data/lib/sugar-high/file_mutate.rb +45 -192
  8. data/lib/sugar-high/file_mutate/append_content.rb +17 -0
  9. data/lib/sugar-high/file_mutate/delete.rb +29 -0
  10. data/lib/sugar-high/file_mutate/insert_content.rb +62 -0
  11. data/lib/sugar-high/file_mutate/mutate.rb +58 -0
  12. data/lib/sugar-high/file_mutate/overwrite_content.rb +17 -0
  13. data/lib/sugar-high/file_mutate/remove_content.rb +33 -0
  14. data/lib/sugar-high/file_mutate/replace_content.rb +45 -0
  15. data/lib/sugar-high/kind_of.rb +8 -43
  16. data/lib/sugar-high/string.rb +5 -0
  17. data/spec/fixtures/application_file.rb +1 -0
  18. data/spec/fixtures/class_file.rb +15 -0
  19. data/spec/fixtures/content_file.txt +1 -0
  20. data/spec/fixtures/file.txt +1 -0
  21. data/spec/fixtures/routes_file.rb +16 -0
  22. data/spec/sugar-high/array_spec.rb +44 -3
  23. data/spec/sugar-high/file/file_dsl_spec.rb +4 -0
  24. data/spec/sugar-high/file_mutate/append_content_spec.rb +60 -0
  25. data/spec/sugar-high/file_mutate/delete_spec.rb +47 -0
  26. data/spec/sugar-high/file_mutate/insert_before_last_spec.rb +56 -0
  27. data/spec/sugar-high/file_mutate/insert_content_spec.rb +111 -0
  28. data/spec/sugar-high/file_mutate/overwrite_content_spec.rb +80 -0
  29. data/spec/sugar-high/file_mutate/remove_content_spec.rb +109 -0
  30. data/spec/sugar-high/file_mutate/replace_content_spec.rb +33 -0
  31. data/spec/sugar-high/{file/file_spec.rb → file_spec.rb} +1 -0
  32. data/sugar-high.gemspec +26 -27
  33. metadata +27 -28
  34. data/spec/sugar-high/file/file_mutate_spec.rb +0 -325
@@ -0,0 +1,4 @@
1
+ # replace_content_from_asset(:backbone).between(/Hello/, /end/).with('Bye')
2
+ # remove_content_from_asset(:backbone).between(/Hello/, /end/)
3
+ # remove_content_from_asset(:backbone).after(/Hello/)
4
+ # remove_content_from_asset(:backbone).before(/end/)
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "SugarHigh::File" do
6
+ let(:empty_file) { fixture_file 'empty.txt' }
7
+ let(:replace_file) { fixture_file 'file.txt' }
8
+
9
+ describe '#append with :content option' do
10
+ let(:append_file) { fixture_file 'file.txt' }
11
+
12
+ it 'should append content to existing file - class method' do
13
+ File.overwrite(append_file) do
14
+ 'Hello You'
15
+ end
16
+ File.append append_file, :content => 'Appended'
17
+ content = File.read(append_file)
18
+ content.should match /Hello You/
19
+ content.should match /Appended/
20
+ end
21
+
22
+ it 'should append content to existing file - instance method' do
23
+ File.overwrite(append_file) do
24
+ 'Hello You'
25
+ end
26
+ File.new(append_file).append :content => 'Appended'
27
+ content = File.read(append_file)
28
+ content.should match /Hello You/
29
+ content.should match /Appended/
30
+ end
31
+ end
32
+
33
+ describe '#append with block' do
34
+ let(:append_file) { fixture_file 'file.txt' }
35
+
36
+ it "should append content to existing file using block arg - class method" do
37
+ File.overwrite(append_file) do
38
+ 'Hello You'
39
+ end
40
+ File.append append_file do
41
+ 'Appended'
42
+ end
43
+ content = File.read(replace_file)
44
+ content.should match /Hello You/
45
+ content.should match /Appended/
46
+ end
47
+
48
+ it "should append content to existing file using block arg - instance method" do
49
+ File.overwrite(append_file) do
50
+ 'Hello You'
51
+ end
52
+ File.new(append_file).append do
53
+ 'Appended'
54
+ end
55
+ content = File.read(replace_file)
56
+ content.should match /Hello You/
57
+ content.should match /Appended/
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "SugarHigh::File" do
6
+ let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
7
+
8
+ describe '#delete! (class)' do
9
+ it 'should delete file' do
10
+ File.overwrite(file_to_delete) do
11
+ 'Delete this!'
12
+ end
13
+ File.delete! file_to_delete
14
+ File.exist?(file_to_delete).should be_false
15
+ end
16
+ end
17
+
18
+ describe '#delete_file! (class)' do
19
+ it 'should delete file' do
20
+ File.overwrite(file_to_delete) do
21
+ 'Delete this!'
22
+ end
23
+ File.delete_file! file_to_delete
24
+ File.exist?(file_to_delete).should be_false
25
+ end
26
+ end
27
+
28
+ describe '#delete! (instance)' do
29
+ it 'should delete file' do
30
+ File.overwrite(file_to_delete) do
31
+ 'Delete this!'
32
+ end
33
+ File.new(file_to_delete).delete!
34
+ File.exist?(file_to_delete).should be_false
35
+ end
36
+ end
37
+
38
+ describe '#delete_file! (instance)' do
39
+ it 'should delete file' do
40
+ File.overwrite(file_to_delete) do
41
+ 'Delete this!'
42
+ end
43
+ File.new(file_to_delete).delete_file!
44
+ File.exist?(file_to_delete).should be_false
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "Insert content" do
6
+ let(:empty_file) { fixture_file 'empty.txt' }
7
+ let(:class_file) { fixture_file 'class_file.rb'}
8
+ let(:routes_file) { fixture_file 'routes_file.rb' }
9
+ let(:app_file) { fixture_file 'application_file.rb' }
10
+
11
+ describe '#insert_into' do
12
+ let(:insertion_file) { fixture_file 'insertion.txt' }
13
+
14
+ before :each do
15
+ File.overwrite(insertion_file) do
16
+ 'Goodbye'
17
+ end
18
+ end
19
+
20
+ after :each do
21
+ File.delete insertion_file if File.file?(insertion_file)
22
+ end
23
+
24
+
25
+ describe '#insert before last' do
26
+ context ':content option and :before_last option' do
27
+ it "should insert Hello before last end statement" do
28
+ File.insert_into class_file, :content => ' # Hello', :before_last => 'end'
29
+ puts File.read(class_file)
30
+ File.read(class_file).should match /end\s+# Hello\s+end/
31
+ File.remove_content_from class_file, :content => ' # Hello'
32
+ end
33
+ end
34
+
35
+ context ':content option and :before_last option and repeat=true' do
36
+ it "should insert Hello before last end statement but don't repeat" do
37
+ File.insert_into class_file, :content => ' # Hello', :before_last => 'end', :no_repeat => true
38
+ puts File.read(class_file)
39
+ File.read(class_file).should match /end\s+# Hello\s+end/
40
+ File.remove_content_from class_file, :content => ' # Hello'
41
+ end
42
+ end
43
+
44
+ context 'block content and :before_last' do
45
+ it "should insert Hello before last end statement" do
46
+ File.insert_into class_file, :before_last => 'end' do
47
+ ' # Hello'
48
+ end
49
+ puts File.read(class_file)
50
+ File.read(class_file).should match /end\s+# Hello\s+end/
51
+ File.remove_content_from class_file, :content => ' # Hello'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "Insert content" do
6
+ let(:empty_file) { fixture_file 'empty.txt' }
7
+ let(:class_file) { fixture_file 'class_file.rb'}
8
+ let(:routes_file) { fixture_file 'routes_file.rb' }
9
+ let(:app_file) { fixture_file 'application_file.rb' }
10
+
11
+ describe '#insert_into' do
12
+ let(:insertion_file) { fixture_file 'insertion.txt' }
13
+
14
+ before :each do
15
+ File.overwrite(insertion_file) do
16
+ 'Goodbye'
17
+ end
18
+ end
19
+
20
+ after :each do
21
+ File.delete insertion_file if File.file?(insertion_file)
22
+ end
23
+
24
+ context 'File object as target' do
25
+ it "should insert Hello before Goodbye" do
26
+ ins_file = File.new(insertion_file)
27
+ ins_file.read_content.should match /Goodbye/
28
+
29
+ ins_file.has_content?('Goodbye').should be_true
30
+
31
+ File.insert_into ins_file, :before => 'Goodbye' do
32
+ 'Hello'
33
+ end
34
+ ins_file.read_content.should match /Hello\s+Goodbye/
35
+ end
36
+
37
+ context 'content as block argument' do
38
+ it "should insert Hello before Goodbye" do
39
+ File.insert_into insertion_file, :before => 'Goodbye' do
40
+ 'Hello'
41
+ end
42
+ File.read(insertion_file).should match /Hello\s+Goodbye/
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#insert instance method' do
48
+ context 'content as block argument' do
49
+ it "should insert Hello before Goodbye" do
50
+ File.new(insertion_file).insert :before => 'Goodbye' do
51
+ 'Hello'
52
+ end
53
+ File.read(insertion_file).should match /Hello\s+Goodbye/
54
+ end
55
+ end
56
+
57
+ context 'content as String argument' do
58
+ it "should insert Hello before Goodbye using a content string arg" do
59
+ File.insert_into insertion_file, "Hello ", :before => 'Goodbye'
60
+ File.read(insertion_file).should match /Hello\s+Goodbye/
61
+ end
62
+ end
63
+
64
+ context 'using a :content option' do
65
+ it "should insert Hello before Goodbye" do
66
+ File.insert_into insertion_file, :content => 'Hello', :before => 'Goodbye'
67
+ File.read(insertion_file).should match /Hello\s+Goodbye/
68
+ end
69
+ end
70
+
71
+ describe 'insert after' do
72
+ context 'Regexp for the :after expression' do
73
+ it "should insert Hello after Goodbye using a :content option" do
74
+ File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
75
+ File.read(insertion_file).should match /Goodbye\s+Hello/
76
+ end
77
+
78
+ it "should work with an escaped Regexp matching" do
79
+ pattern = Regexp.escape('::Application.initialize!')
80
+ File.insert_into app_file, :after => /\w+#{pattern}/ do
81
+ 'hello'
82
+ end
83
+ File.read(app_file).should match /hello/
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ context 'routes file - String path' do
90
+ it "should insert devise routes statement as first route statement" do
91
+ File.insert_into routes_file, :after => 'routes.draw do' do
92
+ 'devise :users'
93
+ end
94
+ # puts File.read(routes_file)
95
+ File.read(routes_file).should match /routes.draw\s+do\s+devise :users/
96
+ File.remove_content_from routes_file, :content => 'devise :users'
97
+ end
98
+ end
99
+
100
+ context 'routes file - File obj' do
101
+ it "should insert devise routes statement as first route statement" do
102
+ File.insert_into File.new(routes_file), :after => 'routes.draw do' do
103
+ 'devise :users'
104
+ end
105
+ # puts File.read(routes_file)
106
+ File.read(routes_file).should match /routes.draw\s+do\s+devise :users/
107
+ File.remove_content_from routes_file, :content => 'devise :users'
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "Overwrite file" do
6
+ let(:empty_file) { fixture_file 'empty.txt' }
7
+ let(:non_empty_file) { fixture_file 'non-empty.txt' }
8
+ let(:content_file) { fixture_file 'content_file.txt' }
9
+
10
+ before :each do
11
+ File.overwrite(content_file) do
12
+ 'Goodbye'
13
+ end
14
+ end
15
+
16
+ describe 'File #overwrite class method' do
17
+ describe '#overwrite with block arg' do
18
+ context 'empty file' do
19
+ let (:content) { 'Overwritten!!!' }
20
+
21
+ it 'should overwrite empty file' do
22
+ File.overwrite content_file do
23
+ content
24
+ end
25
+ cf = File.new(content_file)
26
+ cf.has_content?(content).should be_true
27
+ cf.delete!
28
+ end
29
+ end
30
+
31
+ context 'non-empty file' do
32
+ let (:content) { 'Overwritten!!!' }
33
+
34
+ it 'should overwrite file content with new content, erasing the old' do
35
+ File.overwrite content_file do
36
+ content
37
+ end
38
+
39
+ new_content = "New content"
40
+ File.overwrite content_file do
41
+ new_content
42
+ end
43
+
44
+ cf = File.new(content_file)
45
+ cf.has_content?(content).should be_false
46
+ cf.has_content?(new_content).should be_true
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#overwrite! (instance)' do
52
+ let (:content) { 'Overwritten!!!' }
53
+
54
+ describe '#overwrite with block argument' do
55
+ it 'should overwrite empty file' do
56
+ cf = File.new(content_file)
57
+ cf.overwrite do
58
+ content
59
+ end
60
+ cf.has_content?(content).should be_true
61
+ File.delete! content_file
62
+ end
63
+ end
64
+
65
+ describe '#overwrite using :with option arg' do
66
+ it 'should overwrite file content with new content, erasing the old' do
67
+ cf = File.new(content_file)
68
+
69
+ cf.overwrite :with => content
70
+
71
+ new_content = "New content"
72
+ cf.overwrite :with => new_content
73
+
74
+ cf.has_content?(content).should be_false
75
+ cf.has_content?(new_content).should be_true
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+ require 'sugar-high/file_mutate'
3
+ File.mutate_ext :all
4
+
5
+ describe "SugarHigh::File" do
6
+ let(:empty_file) { fixture_file 'empty.txt' }
7
+ let(:non_empty_file) { fixture_file 'non-empty.txt'}
8
+ let(:class_file) { fixture_file 'class_file.rb'}
9
+ let(:replace_file) { fixture_file 'file.txt' }
10
+ let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
11
+ let(:routes_file) { fixture_file 'routes_file.rb' }
12
+ let(:app_file) { fixture_file 'application_file.rb' }
13
+
14
+ describe '#remove_content_from with :where option' do
15
+ let(:replace_file) { fixture_file 'file.txt' }
16
+
17
+ it "should remove content from existing file - class method" do
18
+ File.overwrite(replace_file) do
19
+ 'Hello You'
20
+ end
21
+ File.remove_content_from replace_file, :where => 'You'
22
+ File.read(replace_file).should_not match /You/
23
+ end
24
+
25
+ it "should remove content from existing file - instance method #remove_content" do
26
+ File.overwrite(replace_file) do
27
+ 'Hello You'
28
+ end
29
+ File.new(replace_file).remove_content :where => 'You'
30
+ File.read(replace_file).should_not match /You/
31
+ end
32
+
33
+ end
34
+
35
+ describe '#remove_content_from with :content option' do
36
+ let(:replace_file) { fixture_file 'file.txt' }
37
+
38
+ it "should remove content from existing file - class method" do
39
+ File.overwrite(replace_file) do
40
+ 'Hello You'
41
+ end
42
+ File.remove_content_from replace_file, :content => 'You'
43
+ File.read(replace_file).should_not match /You/
44
+ end
45
+
46
+ it "should remove content from existing file - instance method #remove_content" do
47
+ File.overwrite(replace_file) do
48
+ 'Hello You'
49
+ end
50
+ File.new(replace_file).remove_content 'You'
51
+ File.read(replace_file).should_not match /You/
52
+ end
53
+ end
54
+
55
+ describe '#remove_from with String/Regexp argument that is content to remove' do
56
+ let(:replace_file) { fixture_file 'file.txt' }
57
+
58
+ it "should remove content from existing file - class method" do
59
+ File.overwrite(replace_file) do
60
+ 'Hello You'
61
+ end
62
+ File.remove_from replace_file, 'You'
63
+ File.read(replace_file).should_not match /You/
64
+ end
65
+
66
+ it "should remove content from existing file - instance method #remove" do
67
+ File.overwrite(replace_file) do
68
+ 'Hello You'
69
+ end
70
+ File.new(replace_file).remove_content 'You'
71
+ File.read(replace_file).should_not match /You/
72
+ end
73
+ end
74
+
75
+ describe '#remove_from with block argument that is content to remove' do
76
+ let(:replace_file) { fixture_file 'file.txt' }
77
+
78
+ it "should work using File object as target" do
79
+ fr = File.new replace_file
80
+ File.overwrite(fr) do
81
+ 'Hello You'
82
+ end
83
+ File.remove_from fr do
84
+ 'You'
85
+ end
86
+ File.read(fr).should_not match /You/
87
+ end
88
+
89
+ it "should remove content from existing file - class method" do
90
+ File.overwrite(replace_file) do
91
+ 'Hello You'
92
+ end
93
+ File.remove_from replace_file do
94
+ 'You'
95
+ end
96
+ File.read(replace_file).should_not match /You/
97
+ end
98
+
99
+ it "should remove content from existing file - instance method #remove" do
100
+ File.overwrite(replace_file) do
101
+ 'Hello You'
102
+ end
103
+ File.new(replace_file).remove do
104
+ 'You'
105
+ end
106
+ File.read(replace_file).should_not match /You/
107
+ end
108
+ end
109
+ end