file_mutate 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ module FileMutate
2
+ module ReplaceContent
3
+ def replace_content options = {}, &block
4
+ File.replace_content_from self.path, options, &block
5
+ end
6
+
7
+ module ClassMethods
8
+ # replaces content found at replacement_expr with content resulting from yielding block
9
+ # File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
10
+ def replace_content_from file_name, options = {}, &block
11
+ replacement_expr = options[:where] || options[:content]
12
+ new_content = options[:with]
13
+
14
+ begin
15
+ replacement_expr = replacement_expr.to_regexp
16
+ rescue
17
+ raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
18
+ end
19
+
20
+ file = get_file file_name
21
+
22
+ # get existing file content
23
+ content = file.read
24
+
25
+ # return nil if no mathing replacement found
26
+ return nil if !(content =~ replacement_expr)
27
+
28
+ new_content ||= yield if block
29
+
30
+ raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
31
+
32
+ # remove content that matches expr, by replacing with empty
33
+ mutated_content = content.gsub replacement_expr, new_content
34
+
35
+ # write mutated content as new file
36
+ file.overwrite mutated_content
37
+
38
+ true # signal success!
39
+ end
40
+ alias_method :replace_content_in, :replace_content_from
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,46 @@
1
+ require 'sugar-high/blank'
2
+ require 'sugar-high/arguments'
3
+ require 'sugar-high/path'
4
+ require 'sugar-high/regexp'
5
+ require 'sugar-high/string'
6
+ require 'sugar-high/file'
7
+ require 'sugar-high/array'
8
+ require 'sweetloader'
9
+
10
+ require 'active_support/inflector'
11
+
12
+ class Module
13
+ def file_mutate name
14
+ if name == :all
15
+ FileMutate.add_all self
16
+ return
17
+ end
18
+ FileMutate.add_mutate_exts self, [:mutate, name]
19
+ end
20
+ end
21
+
22
+ module FileMutate
23
+ autoload_modules :Mutate, :Delete, :AppendContent, :InsertContent
24
+ autoload_modules :OverwriteContent, :RemoveContent, :ReplaceContent
25
+
26
+ def self.mutate_apis
27
+ [:delete, :mutate, :append_content, :insert_content, :overwrite_content, :remove_content, :replace_content]
28
+ end
29
+
30
+ def self.add_all clazz
31
+ add_mutate_exts clazz, mutate_apis
32
+ end
33
+
34
+ def self.add_mutate_exts clazz, *names
35
+ names.flat_uniq!
36
+ unknowns = (names - mutate_apis)
37
+ raise ArgumentError, "Unknown FileMutate APIs: #{unknowns}, must be one of: #{mutate_apis}" if !unknowns.empty?
38
+ names.each do |api|
39
+ ns = "FileMutate::#{api.to_s.camelize}"
40
+ begin
41
+ clazz.send :include, ns.constantize
42
+ clazz.extend "#{ns}::ClassMethods".constantize
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :append_content
3
+
4
+ describe FileMutate do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:replace_file) { fixture_file 'file.txt' }
7
+
8
+ describe '#append with :content option' do
9
+ let(:append_file) { fixture_file 'file.txt' }
10
+
11
+ it 'should append content to existing file - class method' do
12
+ File.overwrite(append_file) do
13
+ 'Hello You'
14
+ end
15
+ File.append append_file, :content => 'Appended'
16
+ content = File.read(append_file)
17
+ content.should match /Hello You/
18
+ content.should match /Appended/
19
+ end
20
+
21
+ it 'should append content to existing file - instance method' do
22
+ File.overwrite(append_file) do
23
+ 'Hello You'
24
+ end
25
+ File.new(append_file).append :content => 'Appended'
26
+ content = File.read(append_file)
27
+ content.should match /Hello You/
28
+ content.should match /Appended/
29
+ end
30
+ end
31
+
32
+ describe '#append with block' do
33
+ let(:append_file) { fixture_file 'file.txt' }
34
+
35
+ it "should append content to existing file using block arg - class method" do
36
+ File.overwrite(append_file) do
37
+ 'Hello You'
38
+ end
39
+ File.append append_file do
40
+ 'Appended'
41
+ end
42
+ content = File.read(replace_file)
43
+ content.should match /Hello You/
44
+ content.should match /Appended/
45
+ end
46
+
47
+ it "should append content to existing file using block arg - instance method" do
48
+ File.overwrite(append_file) do
49
+ 'Hello You'
50
+ end
51
+ File.new(append_file).append do
52
+ 'Appended'
53
+ end
54
+ content = File.read(replace_file)
55
+ content.should match /Hello You/
56
+ content.should match /Appended/
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :delete
3
+
4
+ describe FileMutate do
5
+ let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
6
+
7
+ describe '#delete! (class)' do
8
+ it 'should delete file' do
9
+ File.overwrite(file_to_delete) do
10
+ 'Delete this!'
11
+ end
12
+ File.delete! file_to_delete
13
+ File.exist?(file_to_delete).should be_false
14
+ end
15
+ end
16
+
17
+ describe '#delete_file! (class)' do
18
+ it 'should delete file' do
19
+ File.overwrite(file_to_delete) do
20
+ 'Delete this!'
21
+ end
22
+ File.delete_file! file_to_delete
23
+ File.exist?(file_to_delete).should be_false
24
+ end
25
+ end
26
+
27
+ describe '#delete! (instance)' do
28
+ it 'should delete file' do
29
+ File.overwrite(file_to_delete) do
30
+ 'Delete this!'
31
+ end
32
+ File.new(file_to_delete).delete!
33
+ File.exist?(file_to_delete).should be_false
34
+ end
35
+ end
36
+
37
+ describe '#delete_file! (instance)' do
38
+ it 'should delete file' do
39
+ File.overwrite(file_to_delete) do
40
+ 'Delete this!'
41
+ end
42
+ File.new(file_to_delete).delete_file!
43
+ File.exist?(file_to_delete).should be_false
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :insert_content
3
+
4
+ describe FileMutate do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:class_file) { fixture_file 'class_file.rb'}
7
+ let(:routes_file) { fixture_file 'routes_file.rb' }
8
+ let(:app_file) { fixture_file 'application_file.rb' }
9
+
10
+ describe '#insert_into' do
11
+ let(:insertion_file) { fixture_file 'insertion.txt' }
12
+ before :each do
13
+ File.overwrite(insertion_file) do
14
+ 'Goodbye'
15
+ end
16
+ end
17
+
18
+ after :each do
19
+ File.delete insertion_file if File.file?(insertion_file)
20
+ end
21
+
22
+
23
+ describe '#insert before last' do
24
+ context ':content option and :before_last option' do
25
+ it "should insert Hello before last end statement" do
26
+ File.insert_into class_file, :content => ' # Hello', :before_last => 'end'
27
+ puts File.read(class_file)
28
+ File.read(class_file).should match /end\s+# Hello\s+end/
29
+ File.remove_content_from class_file, :content => ' # Hello'
30
+ end
31
+ end
32
+
33
+ context ':content option and :before_last option and repeat=true' do
34
+ it "should insert Hello before last end statement but don't repeat" do
35
+ File.insert_into class_file, :content => ' # Hello', :before_last => 'end', :no_repeat => true
36
+ puts File.read(class_file)
37
+ File.read(class_file).should match /end\s+# Hello\s+end/
38
+ File.remove_content_from class_file, :content => ' # Hello'
39
+ end
40
+ end
41
+
42
+ context 'block content and :before_last' do
43
+ it "should insert Hello before last end statement" do
44
+ File.insert_into class_file, :before_last => 'end' do
45
+ ' # Hello'
46
+ end
47
+ puts File.read(class_file)
48
+ File.read(class_file).should match /end\s+# Hello\s+end/
49
+ File.remove_content_from class_file, :content => ' # Hello'
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :insert_content
3
+
4
+ describe FileMutate do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:class_file) { fixture_file 'class_file.rb'}
7
+ let(:routes_file) { fixture_file 'routes_file.rb' }
8
+ let(:app_file) { fixture_file 'application_file.rb' }
9
+
10
+ describe '#insert_into' do
11
+ let(:insertion_file) { fixture_file 'insertion.txt' }
12
+
13
+ before :each do
14
+ File.overwrite(insertion_file) do
15
+ 'Goodbye'
16
+ end
17
+ end
18
+
19
+ after :each do
20
+ File.delete insertion_file if File.file?(insertion_file)
21
+ end
22
+
23
+ context 'File object as target' do
24
+ it "should insert Hello before Goodbye" do
25
+ ins_file = File.new(insertion_file)
26
+ ins_file.read_content.should match /Goodbye/
27
+
28
+ ins_file.has_content?('Goodbye').should be_true
29
+
30
+ File.insert_into ins_file, :before => 'Goodbye' do
31
+ 'Hello'
32
+ end
33
+ ins_file.read_content.should match /Hello\s+Goodbye/
34
+ end
35
+
36
+ context 'content as block argument' do
37
+ it "should insert Hello before Goodbye" do
38
+ File.insert_into insertion_file, :before => 'Goodbye' do
39
+ 'Hello'
40
+ end
41
+ File.read(insertion_file).should match /Hello\s+Goodbye/
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#insert instance method' do
47
+ context 'content as block argument' do
48
+ it "should insert Hello before Goodbye" do
49
+ File.new(insertion_file).insert :before => 'Goodbye' do
50
+ 'Hello'
51
+ end
52
+ File.read(insertion_file).should match /Hello\s+Goodbye/
53
+ end
54
+ end
55
+
56
+ context 'content as String argument' do
57
+ it "should insert Hello before Goodbye using a content string arg" do
58
+ File.insert_into insertion_file, "Hello ", :before => 'Goodbye'
59
+ File.read(insertion_file).should match /Hello\s+Goodbye/
60
+ end
61
+ end
62
+
63
+ context 'using a :content option' do
64
+ it "should insert Hello before Goodbye" do
65
+ File.insert_into insertion_file, :content => 'Hello', :before => 'Goodbye'
66
+ File.read(insertion_file).should match /Hello\s+Goodbye/
67
+ end
68
+ end
69
+
70
+ describe 'insert after' do
71
+ context 'Regexp for the :after expression' do
72
+ it "should insert Hello after Goodbye using a :content option" do
73
+ File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
74
+ File.read(insertion_file).should match /Goodbye\s+Hello/
75
+ end
76
+
77
+ it "should work with an escaped Regexp matching" do
78
+ pattern = Regexp.escape('::Application.initialize!')
79
+ File.insert_into app_file, :after => /\w+#{pattern}/ do
80
+ 'hello'
81
+ end
82
+ File.read(app_file).should match /hello/
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'routes file - String path' do
89
+ it "should insert devise routes statement as first route statement" do
90
+ File.insert_into routes_file, :after => 'routes.draw do' do
91
+ 'devise :users'
92
+ end
93
+ # puts File.read(routes_file)
94
+ File.read(routes_file).should match /routes.draw\s+do\s+devise :users/
95
+ File.remove_content_from routes_file, :content => 'devise :users'
96
+ end
97
+ end
98
+
99
+ context 'routes file - File obj' do
100
+ it "should insert devise routes statement as first route statement" do
101
+ File.insert_into File.new(routes_file), :after => 'routes.draw do' do
102
+ 'devise :users'
103
+ end
104
+ # puts File.read(routes_file)
105
+ File.read(routes_file).should match /routes.draw\s+do\s+devise :users/
106
+ File.remove_content_from routes_file, :content => 'devise :users'
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :overwrite_content
3
+
4
+ describe FileMutate do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:non_empty_file) { fixture_file 'non-empty.txt' }
7
+ let(:content_file) { fixture_file 'content_file.txt' }
8
+
9
+ before :each do
10
+ File.overwrite(content_file) do
11
+ 'Goodbye'
12
+ end
13
+ end
14
+
15
+ describe 'File #overwrite class method' do
16
+ describe '#overwrite with block arg' do
17
+ context 'empty file' do
18
+ let (:content) { 'Overwritten!!!' }
19
+
20
+ it 'should overwrite empty file' do
21
+ File.overwrite content_file do
22
+ content
23
+ end
24
+ cf = File.new(content_file)
25
+ cf.has_content?(content).should be_true
26
+ cf.delete!
27
+ end
28
+ end
29
+
30
+ context 'non-empty file' do
31
+ let (:content) { 'Overwritten!!!' }
32
+
33
+ it 'should overwrite file content with new content, erasing the old' do
34
+ File.overwrite content_file do
35
+ content
36
+ end
37
+
38
+ new_content = "New content"
39
+ File.overwrite content_file do
40
+ new_content
41
+ end
42
+
43
+ cf = File.new(content_file)
44
+ cf.has_content?(content).should be_false
45
+ cf.has_content?(new_content).should be_true
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#overwrite! (instance)' do
51
+ let (:content) { 'Overwritten!!!' }
52
+
53
+ describe '#overwrite with block argument' do
54
+ it 'should overwrite empty file' do
55
+ cf = File.new(content_file)
56
+ cf.overwrite do
57
+ content
58
+ end
59
+ cf.has_content?(content).should be_true
60
+ File.delete! content_file
61
+ end
62
+ end
63
+
64
+ describe '#overwrite using :with option arg' do
65
+ it 'should overwrite file content with new content, erasing the old' do
66
+ cf = File.new(content_file)
67
+ cf.overwrite :with => content
68
+
69
+ new_content = "New content"
70
+ cf.overwrite :with => new_content
71
+
72
+ cf.has_content?(content).should be_false
73
+ cf.has_content?(new_content).should be_true
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ File.file_mutate :remove_content
3
+
4
+ describe FileMutate do
5
+ let(:empty_file) { fixture_file 'empty.txt' }
6
+ let(:non_empty_file) { fixture_file 'non-empty.txt'}
7
+ let(:class_file) { fixture_file 'class_file.rb'}
8
+ let(:replace_file) { fixture_file 'file.txt' }
9
+ let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
10
+ let(:routes_file) { fixture_file 'routes_file.rb' }
11
+ let(:app_file) { fixture_file 'application_file.rb' }
12
+
13
+ describe '#remove_content_from with :where option' do
14
+ let(:replace_file) { fixture_file 'file.txt' }
15
+
16
+ it "should remove content from existing file - class method" do
17
+ File.overwrite(replace_file) do
18
+ 'Hello You'
19
+ end
20
+ File.remove_content_from replace_file, :where => 'You'
21
+ File.read(replace_file).should_not match /You/
22
+ end
23
+
24
+ it "should remove content from existing file - instance method #remove_content" do
25
+ File.overwrite(replace_file) do
26
+ 'Hello You'
27
+ end
28
+ File.new(replace_file).remove_content :where => 'You'
29
+ File.read(replace_file).should_not match /You/
30
+ end
31
+
32
+ end
33
+
34
+ describe '#remove_content_from with :content option' do
35
+ let(:replace_file) { fixture_file 'file.txt' }
36
+
37
+ it "should remove content from existing file - class method" do
38
+ File.overwrite(replace_file) do
39
+ 'Hello You'
40
+ end
41
+ File.remove_content_from replace_file, :content => 'You'
42
+ File.read(replace_file).should_not match /You/
43
+ end
44
+
45
+ it "should remove content from existing file - instance method #remove_content" do
46
+ File.overwrite(replace_file) do
47
+ 'Hello You'
48
+ end
49
+ File.new(replace_file).remove_content 'You'
50
+ File.read(replace_file).should_not match /You/
51
+ end
52
+ end
53
+
54
+ describe '#remove_from with String/Regexp argument that is content to remove' do
55
+ let(:replace_file) { fixture_file 'file.txt' }
56
+
57
+ it "should remove content from existing file - class method" do
58
+ File.overwrite(replace_file) do
59
+ 'Hello You'
60
+ end
61
+ File.remove_from replace_file, 'You'
62
+ File.read(replace_file).should_not match /You/
63
+ end
64
+
65
+ it "should remove content from existing file - instance method #remove" do
66
+ File.overwrite(replace_file) do
67
+ 'Hello You'
68
+ end
69
+ File.new(replace_file).remove_content 'You'
70
+ File.read(replace_file).should_not match /You/
71
+ end
72
+ end
73
+
74
+ describe '#remove_from with block argument that is content to remove' do
75
+ let(:replace_file) { fixture_file 'file.txt' }
76
+
77
+ it "should work using File object as target" do
78
+ fr = File.new replace_file
79
+ File.overwrite(fr) do
80
+ 'Hello You'
81
+ end
82
+ File.remove_from fr do
83
+ 'You'
84
+ end
85
+ File.read(fr).should_not match /You/
86
+ end
87
+
88
+ it "should remove content from existing file - class method" do
89
+ File.overwrite(replace_file) do
90
+ 'Hello You'
91
+ end
92
+ File.remove_from replace_file do
93
+ 'You'
94
+ end
95
+ File.read(replace_file).should_not match /You/
96
+ end
97
+
98
+ it "should remove content from existing file - instance method #remove" do
99
+ File.overwrite(replace_file) do
100
+ 'Hello You'
101
+ end
102
+ File.new(replace_file).remove do
103
+ 'You'
104
+ end
105
+ File.read(replace_file).should_not match /You/
106
+ end
107
+ end
108
+ end