sugar-high 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sugar-high/array.rb +29 -2
- data/lib/sugar-high/file.rb +64 -161
- data/lib/sugar-high/file_mutate.rb +193 -0
- data/lib/sugar-high/path.rb +26 -0
- data/lib/sugar-high/string.rb +1 -1
- data/spec/fixtures/class_file.rb +0 -2
- data/spec/fixtures/search_file.txt +1 -0
- data/spec/sugar-high/file/file_mutate_spec.rb +165 -26
- data/spec/sugar-high/file/file_spec.rb +83 -40
- data/sugar-high.gemspec +4 -2
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/lib/sugar-high/array.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'sugar-high/kind_of'
|
2
|
+
require 'sugar-high/path'
|
2
3
|
|
3
4
|
class Array
|
4
5
|
def to_symbols option=nil
|
@@ -7,9 +8,27 @@ class Array
|
|
7
8
|
res.select_labels.map(&:to_s).map(&:to_sym)
|
8
9
|
end
|
9
10
|
|
10
|
-
def to_strings
|
11
|
+
def to_strings
|
11
12
|
self.flatten.select_labels.map(&:to_s)
|
12
13
|
end
|
14
|
+
|
15
|
+
def to_filenames
|
16
|
+
self.to_strings.map(&:underscore)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def to_paths
|
21
|
+
self.map(&:path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def file_join
|
25
|
+
File.join(*self.flatten)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_files
|
29
|
+
self.map{|fp| fp.path.to_file }
|
30
|
+
self.extend FilesArray
|
31
|
+
end
|
13
32
|
|
14
33
|
def none?
|
15
34
|
self.flatten.compact.empty?
|
@@ -17,7 +36,15 @@ class Array
|
|
17
36
|
|
18
37
|
def flat_uniq
|
19
38
|
self.flatten.compact.uniq
|
20
|
-
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module FilesArray
|
43
|
+
def delete_all!
|
44
|
+
self.each do |f|
|
45
|
+
f.delete! if f.kind_of?(File)
|
46
|
+
end
|
47
|
+
end
|
21
48
|
end
|
22
49
|
|
23
50
|
class NilClass
|
data/lib/sugar-high/file.rb
CHANGED
@@ -4,17 +4,7 @@ require 'sugar-high/path'
|
|
4
4
|
require 'sugar-high/regexp'
|
5
5
|
require 'sugar-high/string'
|
6
6
|
|
7
|
-
class File
|
8
|
-
def self.delete! name
|
9
|
-
return nil if !File.exist?(name)
|
10
|
-
File.delete name
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.delete_file! name
|
14
|
-
return nil if !File.file?(name)
|
15
|
-
File.delete name
|
16
|
-
end
|
17
|
-
|
7
|
+
class File
|
18
8
|
def self.blank? file_name
|
19
9
|
raise ArgumentError, "Filename argument must not be blank" if file_name.blank?
|
20
10
|
raise ArgumentError, "There is no file at: #{file_name}" if !File.file?(file_name)
|
@@ -24,174 +14,87 @@ class File
|
|
24
14
|
def blank?
|
25
15
|
File.zero?(self.path)
|
26
16
|
end
|
27
|
-
|
28
|
-
def self.overwrite path, content=nil, &block
|
29
|
-
File.open(path, 'w') do |f|
|
30
|
-
f.puts content ||= yield
|
31
|
-
end
|
32
|
-
end
|
33
17
|
|
34
|
-
def self.
|
35
|
-
File.
|
36
|
-
f.puts content ||= yield
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.remove_from file_name, content=nil, &block
|
41
|
-
content ||= yield
|
42
|
-
replace_content_from file_name, :content => content, :with => '', &block
|
18
|
+
def self.has_content? file_name, content_matcher, &block
|
19
|
+
File.new(file_name).has_content? content_matcher, &block
|
43
20
|
end
|
44
|
-
|
45
|
-
# replaces content found at replacement_expr with content resulting from yielding block
|
46
|
-
# File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
|
47
|
-
def self.replace_content_from file_name, options = {}, &block
|
48
|
-
replacement_expr = options[:where] || options[:content]
|
49
|
-
new_content = options[:with]
|
50
21
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
|
22
|
+
def has_content? content_matcher = nil, &block
|
23
|
+
content_matcher ||= yield
|
24
|
+
begin
|
25
|
+
content_matcher = content_matcher.to_regexp
|
26
|
+
rescue
|
27
|
+
raise ArgumentError, "Content match must be specified as either a String or Regexp"
|
58
28
|
end
|
29
|
+
!(self.read =~ content_matcher).nil?
|
30
|
+
end
|
59
31
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
# return nil if no mathing replacement found
|
64
|
-
return nil if !(content =~ replacement_expr)
|
65
|
-
|
66
|
-
new_content ||= yield if block
|
67
|
-
|
68
|
-
raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
|
69
|
-
|
70
|
-
# remove content that matches expr, by replacing with empty
|
71
|
-
mutated_content = content.gsub replacement_expr, new_content
|
72
|
-
|
73
|
-
# write mutated content as new file
|
74
|
-
File.overwrite file_name, mutated_content
|
75
|
-
|
76
|
-
true # signal success!
|
32
|
+
def read_content options = {}, &block
|
33
|
+
File.read_from self.path, options, &block
|
77
34
|
end
|
35
|
+
alias_method :with_content, :read_content
|
78
36
|
|
37
|
+
class << self
|
79
38
|
|
80
|
-
|
81
|
-
|
82
|
-
|
39
|
+
def read_from file_name, options = {}, &block
|
40
|
+
raise ArgumentError, "File to read from not found or not a file: #{file_name}" if !File.file? file_name
|
41
|
+
content = File.read file_name
|
83
42
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
43
|
+
if options[:before]
|
44
|
+
begin
|
45
|
+
regexp = options[:before].to_regexp
|
46
|
+
index = content.match(regexp).offset_before
|
47
|
+
content = content[0..index]
|
48
|
+
rescue
|
49
|
+
raise ArgumentError, ":before option must be a string or regular expression, was : #{options[:before]}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
if options[:after]
|
54
|
+
begin
|
55
|
+
regexp = options[:after].to_regexp
|
56
|
+
index = content.match(regexp).offset_after
|
57
|
+
content = content[index..-1]
|
58
|
+
rescue
|
59
|
+
raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}"
|
60
|
+
end
|
91
61
|
end
|
92
|
-
|
93
|
-
|
94
|
-
if options[:after]
|
95
|
-
begin
|
96
|
-
regexp = options[:after].to_regexp
|
97
|
-
index = content.match(regexp).offset_after
|
98
|
-
content = content[index..-1]
|
99
|
-
rescue
|
100
|
-
raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}"
|
101
|
-
end
|
62
|
+
yield content if block
|
63
|
+
content
|
102
64
|
end
|
103
|
-
|
104
|
-
|
65
|
+
alias_method :read_content_from, :read_from
|
66
|
+
alias_method :with_content_from, :read_from
|
105
67
|
end
|
68
|
+
end
|
106
69
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
# TODO: Needs spec
|
112
|
-
|
113
|
-
# insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello
|
114
|
-
# insert_into 'my_file.txt', 'Hello', :after => 'Blip'
|
115
|
-
# insert_into 'my_file.txt', :after => 'Blip' do
|
116
|
-
# 'Hello'
|
117
|
-
# end
|
70
|
+
class String
|
71
|
+
def as_filename
|
72
|
+
self.underscore
|
73
|
+
end
|
118
74
|
|
119
|
-
def
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
file = File.new(file_name)
|
124
|
-
return nil if !File.exist?(file)
|
125
|
-
|
126
|
-
# already inserted?
|
127
|
-
return nil if content.blank? || (file.read =~ /#{content}/)
|
128
|
-
|
129
|
-
place, marker = if options[:before]
|
130
|
-
[ :before, options[:before] ]
|
131
|
-
elsif options[:before_last]
|
132
|
-
[ :before_last, options[:before_last] ]
|
133
|
-
else
|
134
|
-
[ :after, options[:after] ]
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
marker = Insert.get_marker marker
|
139
|
-
|
140
|
-
return nil if !(File.new(file.path).read =~ /#{marker}/)
|
141
|
-
|
142
|
-
Mutate.mutate_file file.path, marker, place do
|
143
|
-
content
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
module Insert
|
148
|
-
def self.get_marker marker
|
149
|
-
marker = case marker
|
150
|
-
when String
|
151
|
-
Regexp.escape(marker)
|
152
|
-
when Regexp
|
153
|
-
marker.source
|
154
|
-
end
|
155
|
-
end
|
75
|
+
def valid_file_command?
|
76
|
+
self.to_sym.valid_file_command?
|
77
|
+
end
|
78
|
+
end
|
156
79
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
80
|
+
class Symbol
|
81
|
+
def as_filename
|
82
|
+
self.to_s.underscore
|
83
|
+
end
|
84
|
+
|
85
|
+
def valid_file_command?
|
86
|
+
[:read, :remove, :delete].include? self
|
87
|
+
end
|
88
|
+
end
|
169
89
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
content = File.read(file)
|
176
|
-
content = content.insert_before_last yield, marker
|
177
|
-
File.open(file, 'wb') { |file| file.write(content) }
|
178
|
-
return
|
179
|
-
end
|
180
|
-
|
181
|
-
replace_in_file file, /(#{marker})/mi do |match|
|
182
|
-
place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def self.replace_in_file(path, regexp, *args, &block)
|
187
|
-
content = File.read(path).gsub(regexp, *args, &block)
|
188
|
-
File.open(path, 'wb') { |file| file.write(content) }
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
90
|
+
class NilClass
|
91
|
+
def valid_file_command?
|
92
|
+
false
|
93
|
+
end
|
94
|
+
end
|
192
95
|
|
193
96
|
class Array
|
194
97
|
def file_names ext = '*'
|
195
|
-
self.map{|a| a.gsub( /(.*)\//, '').gsub(/\.#{Regexp.escape(ext)}/, '')}
|
98
|
+
self.map{|a| a.gsub( /(.*)\//, '').gsub(/\.#{Regexp.escape(ext.to_s)}/, '')}
|
196
99
|
end
|
197
100
|
end
|
@@ -0,0 +1,193 @@
|
|
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
|
+
|
8
|
+
class File
|
9
|
+
class << self
|
10
|
+
def delete! name
|
11
|
+
return nil if !File.exist?(name)
|
12
|
+
File.delete name
|
13
|
+
end
|
14
|
+
alias_method :delete_file!, :delete!
|
15
|
+
end
|
16
|
+
|
17
|
+
def delete!
|
18
|
+
File.delete(self.path)
|
19
|
+
end
|
20
|
+
alias_method :delete_file!, :delete!
|
21
|
+
|
22
|
+
|
23
|
+
def overwrite content=nil, &block
|
24
|
+
File.overwrite self.path, content, &block
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.overwrite file, content=nil, &block
|
28
|
+
filepath = case file
|
29
|
+
when PathString, String
|
30
|
+
file
|
31
|
+
when File
|
32
|
+
file.path
|
33
|
+
else
|
34
|
+
raise ArgumentError, "Expected first argument to be a File instance or a path indicating file to overwrite"
|
35
|
+
end
|
36
|
+
File.open(filepath, 'w') do |f|
|
37
|
+
f.puts content ||= yield
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def append content=nil, &block
|
42
|
+
File.append self.path, content, &block
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.append path, content=nil, &block
|
46
|
+
File.open(path, 'a') do |f|
|
47
|
+
f.puts content ||= yield
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def remove_content options=nil, &block
|
52
|
+
opt_str = case options
|
53
|
+
when String
|
54
|
+
options
|
55
|
+
when Hash
|
56
|
+
content = options[:content] || options[:where]
|
57
|
+
raise ArgumentError, "Bad :content value in Hash" if !content || content.strip.empty?
|
58
|
+
content.strip
|
59
|
+
else
|
60
|
+
raise ArgumentError, "non-block argument must be either String or Hash with a :content option" if !block
|
61
|
+
end
|
62
|
+
content = block ? yield : opt_str
|
63
|
+
File.remove_content_from self.path, :content => content, :with => '', &block
|
64
|
+
end
|
65
|
+
alias_method :remove, :remove_content
|
66
|
+
|
67
|
+
def self.remove_from file_name, content=nil, &block
|
68
|
+
content ||= yield
|
69
|
+
replace_content_from file_name, :content => content, :with => '', &block
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.remove_content_from file_name, options = {}, &block
|
73
|
+
replace_content_from file_name, options.merge(:with => ''), &block
|
74
|
+
end
|
75
|
+
|
76
|
+
def replace_content options = {}, &block
|
77
|
+
File.replace_content_from self.path, options, &block
|
78
|
+
end
|
79
|
+
|
80
|
+
# replaces content found at replacement_expr with content resulting from yielding block
|
81
|
+
# File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
|
82
|
+
def self.replace_content_from file_name, options = {}, &block
|
83
|
+
replacement_expr = options[:where] || options[:content]
|
84
|
+
new_content = options[:with]
|
85
|
+
|
86
|
+
begin
|
87
|
+
replacement_expr = replacement_expr.to_regexp
|
88
|
+
rescue
|
89
|
+
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
|
90
|
+
end
|
91
|
+
|
92
|
+
# get existing file content
|
93
|
+
content = File.read file_name
|
94
|
+
|
95
|
+
# return nil if no mathing replacement found
|
96
|
+
return nil if !(content =~ replacement_expr)
|
97
|
+
|
98
|
+
new_content ||= yield if block
|
99
|
+
|
100
|
+
raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
|
101
|
+
|
102
|
+
# remove content that matches expr, by replacing with empty
|
103
|
+
mutated_content = content.gsub replacement_expr, new_content
|
104
|
+
|
105
|
+
# write mutated content as new file
|
106
|
+
File.overwrite file_name, mutated_content
|
107
|
+
|
108
|
+
true # signal success!
|
109
|
+
end
|
110
|
+
|
111
|
+
def insert *args, &block
|
112
|
+
File.insert_into self.path, *args, &block
|
113
|
+
end
|
114
|
+
|
115
|
+
# insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello
|
116
|
+
# insert_into 'my_file.txt', 'Hello', :after => 'Blip'
|
117
|
+
# insert_into 'my_file.txt', :after => 'Blip' do
|
118
|
+
# 'Hello'
|
119
|
+
# end
|
120
|
+
def self.insert_into file_name, *args, &block
|
121
|
+
options = last_option args
|
122
|
+
content = Insert.content options, *args, &block
|
123
|
+
|
124
|
+
file = File.new(file_name)
|
125
|
+
return nil if !File.exist?(file)
|
126
|
+
|
127
|
+
# already inserted?
|
128
|
+
return nil if content.blank?
|
129
|
+
return nil if !options[:repeat] && (file.read =~ /#{Regexp.escape(content.to_s)}/)
|
130
|
+
|
131
|
+
place, marker = if options[:before]
|
132
|
+
[ :before, options[:before] ]
|
133
|
+
elsif options[:before_last]
|
134
|
+
[ :before_last, options[:before_last] ]
|
135
|
+
else
|
136
|
+
[ :after, options[:after] ]
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
marker = Insert.get_marker marker
|
141
|
+
|
142
|
+
return nil if !(File.new(file.path).read =~ /#{Regexp.escape(marker.to_s)}/)
|
143
|
+
|
144
|
+
Mutate.mutate_file file.path, marker, place do
|
145
|
+
content
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
module Insert
|
150
|
+
def self.get_marker marker
|
151
|
+
marker = case marker
|
152
|
+
when String
|
153
|
+
Regexp.escape(marker)
|
154
|
+
when Regexp
|
155
|
+
marker.source
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.content options = {}, *args, &block
|
160
|
+
case args.first
|
161
|
+
when String
|
162
|
+
args.first
|
163
|
+
when Hash
|
164
|
+
options[:content] || (yield if block)
|
165
|
+
else
|
166
|
+
return yield if block
|
167
|
+
raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
module Mutate
|
173
|
+
def self.mutate_file file, marker, place, &block
|
174
|
+
raise ArgumentError, "You must define a replacement marker for a :before, :before_last or :after key" if !marker
|
175
|
+
|
176
|
+
if place == :before_last
|
177
|
+
content = File.read(file)
|
178
|
+
content = content.insert_before_last yield, marker
|
179
|
+
File.open(file, 'wb') { |file| file.write(content) }
|
180
|
+
return
|
181
|
+
end
|
182
|
+
|
183
|
+
replace_in_file file, /(#{Regexp.escape(marker.to_s)})/mi do |match|
|
184
|
+
place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def self.replace_in_file(path, regexp, *args, &block)
|
189
|
+
content = File.read(path).gsub(regexp, *args, &block)
|
190
|
+
File.open(path, 'wb') { |file| file.write(content) }
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
data/lib/sugar-high/path.rb
CHANGED
@@ -5,21 +5,47 @@ class String
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module PathString
|
8
|
+
def to_file option=nil
|
9
|
+
raise ArgumentError, "File doesn't exist" if option == :raise && !File.directory?(self)
|
10
|
+
File.new(self) if File.file? self
|
11
|
+
end
|
12
|
+
alias_method :file, :to_file
|
13
|
+
alias_method :new_file, :to_file
|
14
|
+
|
15
|
+
def to_dir option=nil
|
16
|
+
raise ArgumentError, "Dir doesn't exist" if option == :raise && !File.directory?(self)
|
17
|
+
Dir.new(self) if File.directory? self
|
18
|
+
end
|
19
|
+
alias_method :new_dir, :to_dir
|
20
|
+
alias_method :dir, :to_dir
|
21
|
+
|
22
|
+
def to_symlink new_path #, option=nil
|
23
|
+
# raise ArgumentError, "New link location doesn't exist" if option == :raise && !File.exist?(new_path)
|
24
|
+
File.symlink(self, new_path)
|
25
|
+
end
|
26
|
+
alias_method :new_symlink, :to_symlink
|
27
|
+
alias_method :symlink, :to_symlink
|
28
|
+
|
8
29
|
def exists?
|
9
30
|
File.exist? self
|
10
31
|
end
|
32
|
+
alias_method :there?, :exists?
|
11
33
|
|
12
34
|
def file?
|
13
35
|
File.file? self
|
14
36
|
end
|
37
|
+
alias_method :is_file?, :file?
|
15
38
|
|
16
39
|
def dir?
|
17
40
|
File.directory? self
|
18
41
|
end
|
42
|
+
alias_method :is_dir?, :dir?
|
43
|
+
alias_method :directory?, :dir
|
19
44
|
|
20
45
|
def symlink?
|
21
46
|
File.symlink? self
|
22
47
|
end
|
48
|
+
alias_method :is_symlink?, :symlink?
|
23
49
|
|
24
50
|
def up lv
|
25
51
|
('../' * lv) + self
|
data/lib/sugar-high/string.rb
CHANGED
@@ -11,7 +11,7 @@ class String
|
|
11
11
|
raise ArgumentException, "last argument is the marker and must be a String, Symbol or even Hash with a :marker option pointing to the marker (String or Symbol)"
|
12
12
|
end
|
13
13
|
|
14
|
-
marker = Regexp.escape(marker.reverse)
|
14
|
+
marker = Regexp.escape(marker.to_s.reverse)
|
15
15
|
nl = Regexp.escape("\n")
|
16
16
|
# puts self
|
17
17
|
# puts "marker: #{marker}"
|
data/spec/fixtures/class_file.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
Find this line right here!
|
@@ -1,25 +1,85 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'sugar-high/
|
2
|
+
require 'sugar-high/file_mutate'
|
3
3
|
|
4
4
|
describe "SugarHigh::File" do
|
5
5
|
let(:empty_file) { fixture_file 'empty.txt' }
|
6
6
|
let(:non_empty_file) { fixture_file 'non-empty.txt'}
|
7
7
|
let(:class_file) { fixture_file 'class_file.rb'}
|
8
8
|
let(:replace_file) { fixture_file 'file.txt' }
|
9
|
+
let(:file_to_delete) { fixture_file 'file_to_delete.txt' }
|
10
|
+
|
11
|
+
after do
|
12
|
+
File.overwrite class_file do
|
13
|
+
%q{class Abc
|
14
|
+
def begin
|
15
|
+
end
|
16
|
+
end}
|
17
|
+
end
|
18
|
+
end
|
9
19
|
|
10
20
|
before :each do
|
11
21
|
File.delete replace_file if File.file?(replace_file)
|
12
22
|
end
|
13
23
|
|
24
|
+
describe '#delete! (class)' do
|
25
|
+
it 'should delete file' do
|
26
|
+
File.overwrite(file_to_delete) do
|
27
|
+
'Delete this!'
|
28
|
+
end
|
29
|
+
File.delete! file_to_delete
|
30
|
+
File.exist?(file_to_delete).should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#delete_file! (class)' do
|
35
|
+
it 'should delete file' do
|
36
|
+
File.overwrite(file_to_delete) do
|
37
|
+
'Delete this!'
|
38
|
+
end
|
39
|
+
File.delete_file! file_to_delete
|
40
|
+
File.exist?(file_to_delete).should be_false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#delete! (instance)' do
|
45
|
+
it 'should delete file' do
|
46
|
+
File.overwrite(file_to_delete) do
|
47
|
+
'Delete this!'
|
48
|
+
end
|
49
|
+
File.new(file_to_delete).delete!
|
50
|
+
File.exist?(file_to_delete).should be_false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#delete_file! (instance)' do
|
55
|
+
it 'should delete file' do
|
56
|
+
File.overwrite(file_to_delete) do
|
57
|
+
'Delete this!'
|
58
|
+
end
|
59
|
+
File.new(file_to_delete).delete_file!
|
60
|
+
File.exist?(file_to_delete).should be_false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
14
64
|
describe '#append with :content option' do
|
15
65
|
let(:append_file) { fixture_file 'file.txt' }
|
16
66
|
|
17
|
-
it
|
67
|
+
it 'should append content to existing file - class method' do
|
18
68
|
File.overwrite(append_file) do
|
19
69
|
'Hello You'
|
20
70
|
end
|
21
71
|
File.append append_file, :content => 'Appended'
|
22
|
-
content = File.read(
|
72
|
+
content = File.read(append_file)
|
73
|
+
content.should match /Hello You/
|
74
|
+
content.should match /Appended/
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should append content to existing file - instance method' do
|
78
|
+
File.overwrite(append_file) do
|
79
|
+
'Hello You'
|
80
|
+
end
|
81
|
+
File.new(append_file).append :content => 'Appended'
|
82
|
+
content = File.read(append_file)
|
23
83
|
content.should match /Hello You/
|
24
84
|
content.should match /Appended/
|
25
85
|
end
|
@@ -28,7 +88,7 @@ describe "SugarHigh::File" do
|
|
28
88
|
describe '#append with block' do
|
29
89
|
let(:append_file) { fixture_file 'file.txt' }
|
30
90
|
|
31
|
-
it "should
|
91
|
+
it "should append content to existing file using block arg - class method" do
|
32
92
|
File.overwrite(append_file) do
|
33
93
|
'Hello You'
|
34
94
|
end
|
@@ -39,61 +99,106 @@ describe "SugarHigh::File" do
|
|
39
99
|
content.should match /Hello You/
|
40
100
|
content.should match /Appended/
|
41
101
|
end
|
42
|
-
end
|
43
|
-
|
44
102
|
|
103
|
+
it "should append content to existing file using block arg - instance method" do
|
104
|
+
File.overwrite(append_file) do
|
105
|
+
'Hello You'
|
106
|
+
end
|
107
|
+
File.new(append_file).append do
|
108
|
+
'Appended'
|
109
|
+
end
|
110
|
+
content = File.read(replace_file)
|
111
|
+
content.should match /Hello You/
|
112
|
+
content.should match /Appended/
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
45
117
|
describe '#replace_content_from' do
|
46
118
|
let(:replace_file) { fixture_file 'file.txt' }
|
47
119
|
|
48
|
-
it "should
|
120
|
+
it "should replace content from existing file - class method" do
|
49
121
|
File.overwrite(replace_file) do
|
50
122
|
'Hello You'
|
51
123
|
end
|
52
124
|
File.replace_content_from replace_file, :where => 'You', :with => 'Me'
|
53
125
|
File.read(replace_file).should_not match /You/
|
54
126
|
end
|
55
|
-
end
|
56
127
|
|
57
|
-
|
58
|
-
|
128
|
+
it 'should remove content from existing file - instance method #replace_content' do
|
129
|
+
File.overwrite(replace_file) do
|
130
|
+
'Hello You'
|
131
|
+
end
|
132
|
+
File.new(replace_file).replace_content :where => 'You', :with => 'Me'
|
133
|
+
File.read(replace_file).should_not match /You/
|
134
|
+
end
|
135
|
+
end
|
59
136
|
|
60
|
-
|
137
|
+
describe '#remove_content_from with :where option' do
|
138
|
+
let(:replace_file) { fixture_file 'file.txt' }
|
139
|
+
|
140
|
+
it "should remove content from existing file - class method" do
|
61
141
|
File.overwrite(replace_file) do
|
62
142
|
'Hello You'
|
63
143
|
end
|
64
144
|
File.remove_content_from replace_file, :where => 'You'
|
65
145
|
File.read(replace_file).should_not match /You/
|
66
146
|
end
|
67
|
-
end
|
68
147
|
|
148
|
+
it "should remove content from existing file - instance method #remove_content" do
|
149
|
+
File.overwrite(replace_file) do
|
150
|
+
'Hello You'
|
151
|
+
end
|
152
|
+
File.new(replace_file).remove_content :where => 'You'
|
153
|
+
File.read(replace_file).should_not match /You/
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
69
158
|
describe '#remove_content_from with :content option' do
|
70
159
|
let(:replace_file) { fixture_file 'file.txt' }
|
71
160
|
|
72
|
-
it "should remove content from existing file" do
|
161
|
+
it "should remove content from existing file - class method" do
|
73
162
|
File.overwrite(replace_file) do
|
74
163
|
'Hello You'
|
75
164
|
end
|
76
165
|
File.remove_content_from replace_file, :content => 'You'
|
77
166
|
File.read(replace_file).should_not match /You/
|
78
167
|
end
|
79
|
-
end
|
80
168
|
|
169
|
+
it "should remove content from existing file - instance method #remove_content" do
|
170
|
+
File.overwrite(replace_file) do
|
171
|
+
'Hello You'
|
172
|
+
end
|
173
|
+
File.new(replace_file).remove_content 'You'
|
174
|
+
File.read(replace_file).should_not match /You/
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
81
178
|
describe '#remove_from with String/Regexp argument that is content to remove' do
|
82
179
|
let(:replace_file) { fixture_file 'file.txt' }
|
83
180
|
|
84
|
-
it "should remove content from existing file" do
|
181
|
+
it "should remove content from existing file - class method" do
|
85
182
|
File.overwrite(replace_file) do
|
86
183
|
'Hello You'
|
87
184
|
end
|
88
185
|
File.remove_from replace_file, 'You'
|
89
186
|
File.read(replace_file).should_not match /You/
|
90
187
|
end
|
188
|
+
|
189
|
+
it "should remove content from existing file - instance method #remove" do
|
190
|
+
File.overwrite(replace_file) do
|
191
|
+
'Hello You'
|
192
|
+
end
|
193
|
+
File.new(replace_file).remove_content 'You'
|
194
|
+
File.read(replace_file).should_not match /You/
|
195
|
+
end
|
91
196
|
end
|
92
|
-
|
197
|
+
|
93
198
|
describe '#remove_from with block argument that is content to remove' do
|
94
199
|
let(:replace_file) { fixture_file 'file.txt' }
|
95
200
|
|
96
|
-
it "should remove content from existing file" do
|
201
|
+
it "should remove content from existing file - class method" do
|
97
202
|
File.overwrite(replace_file) do
|
98
203
|
'Hello You'
|
99
204
|
end
|
@@ -102,22 +207,32 @@ describe "SugarHigh::File" do
|
|
102
207
|
end
|
103
208
|
File.read(replace_file).should_not match /You/
|
104
209
|
end
|
105
|
-
end
|
106
|
-
|
107
210
|
|
211
|
+
it "should remove content from existing file - instance method #remove" do
|
212
|
+
File.overwrite(replace_file) do
|
213
|
+
'Hello You'
|
214
|
+
end
|
215
|
+
File.new(replace_file).remove do
|
216
|
+
'You'
|
217
|
+
end
|
218
|
+
File.read(replace_file).should_not match /You/
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
|
108
223
|
describe '#insert_into' do
|
109
224
|
let(:insertion_file) { fixture_file 'insertion.txt' }
|
110
|
-
|
225
|
+
|
111
226
|
before :each do
|
112
227
|
File.overwrite(insertion_file) do
|
113
228
|
'Goodbye'
|
114
229
|
end
|
115
230
|
end
|
116
|
-
|
231
|
+
|
117
232
|
after :each do
|
118
233
|
File.delete insertion_file if File.file?(insertion_file)
|
119
234
|
end
|
120
|
-
|
235
|
+
|
121
236
|
it "should insert Hello before Goodbye using a block as content" do
|
122
237
|
File.insert_into insertion_file, :before => 'Goodbye' do
|
123
238
|
'Hello'
|
@@ -125,23 +240,31 @@ describe "SugarHigh::File" do
|
|
125
240
|
File.read(insertion_file).should match /Hello\s+Goodbye/
|
126
241
|
end
|
127
242
|
|
243
|
+
it "should insert Hello before Goodbye using a block as content - instance method #insert" do
|
244
|
+
File.new(insertion_file).insert :before => 'Goodbye' do
|
245
|
+
'Hello'
|
246
|
+
end
|
247
|
+
File.read(insertion_file).should match /Hello\s+Goodbye/
|
248
|
+
end
|
249
|
+
|
250
|
+
|
128
251
|
it "should insert Hello before Goodbye using a content string arg" do
|
129
252
|
File.insert_into insertion_file, "Hello ", :before => 'Goodbye'
|
130
253
|
File.read(insertion_file).should match /Hello\s+Goodbye/
|
131
254
|
end
|
132
|
-
|
255
|
+
|
133
256
|
it "should insert Hello before Goodbye using a :content option" do
|
134
257
|
File.insert_into insertion_file, :content => 'Hello', :before => 'Goodbye'
|
135
258
|
File.read(insertion_file).should match /Hello\s+Goodbye/
|
136
259
|
end
|
137
|
-
|
260
|
+
|
138
261
|
it "should insert Hello before Goodbye using a block as content to insert" do
|
139
262
|
File.insert_into insertion_file, :before => 'Goodbye' do
|
140
263
|
'Hello'
|
141
264
|
end
|
142
265
|
File.read(insertion_file).should match /Hello\s+Goodbye/
|
143
266
|
end
|
144
|
-
|
267
|
+
|
145
268
|
it "should insert Hello after Goodbye using a :with option and a Regexp for the after expression" do
|
146
269
|
File.insert_into insertion_file, :content => ' Hello', :after => /Goodbye/
|
147
270
|
File.read(insertion_file).should match /Goodbye\s+Hello/
|
@@ -153,5 +276,21 @@ describe "SugarHigh::File" do
|
|
153
276
|
File.read(class_file).should match /end\s+# Hello\s+end/
|
154
277
|
File.remove_content_from class_file, :content => ' # Hello'
|
155
278
|
end
|
156
|
-
|
279
|
+
|
280
|
+
it "should insert Hello before last end statement but don't repeat" do
|
281
|
+
File.insert_into class_file, :content => ' # Hello', :before_last => 'end', :repeat => true
|
282
|
+
puts File.read(class_file)
|
283
|
+
File.read(class_file).should match /end\s+# Hello\s+end/
|
284
|
+
File.remove_content_from class_file, :content => ' # Hello'
|
285
|
+
end
|
286
|
+
|
287
|
+
it "should insert Hello before last end statement - block" do
|
288
|
+
File.insert_into class_file, :before_last => 'end' do
|
289
|
+
' # Hello'
|
290
|
+
end
|
291
|
+
puts File.read(class_file)
|
292
|
+
File.read(class_file).should match /end\s+# Hello\s+end/
|
293
|
+
File.remove_content_from class_file, :content => ' # Hello'
|
294
|
+
end
|
295
|
+
end
|
157
296
|
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'sugar-high/file'
|
3
|
+
require 'sugar-high/file_mutate'
|
3
4
|
|
4
5
|
describe "SugarHigh" do
|
5
6
|
describe "File" do
|
6
7
|
let(:empty_file) { fixture_file 'empty.txt' }
|
7
8
|
let(:non_empty_file) { fixture_file 'non-empty.txt'}
|
8
9
|
let(:replace_file) { fixture_file 'file.txt' }
|
10
|
+
let(:search_file) { fixture_file 'search_file.txt' }
|
9
11
|
|
10
12
|
before :each do
|
11
13
|
File.delete replace_file if File.file?(replace_file)
|
12
14
|
end
|
13
15
|
|
14
|
-
describe '#self.blank' do
|
16
|
+
describe '#self.blank?' do
|
15
17
|
it "should return true for an empty file" do
|
16
18
|
File.blank?(empty_file).should be_true
|
17
19
|
end
|
@@ -21,7 +23,7 @@ describe "SugarHigh" do
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
describe '#blank' do
|
26
|
+
describe '#blank?' do
|
25
27
|
it "should return true for an empty file" do
|
26
28
|
File.new(empty_file).blank?.should be_true
|
27
29
|
end
|
@@ -30,54 +32,95 @@ describe "SugarHigh" do
|
|
30
32
|
File.new(non_empty_file).blank?.should_not be_true
|
31
33
|
end
|
32
34
|
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe '#file_names' do
|
36
|
-
let(:replace_file) { fixture_file 'file.txt' }
|
37
|
-
|
38
|
-
before :each do
|
39
|
-
File.delete replace_file if File.file?(replace_file)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should return all file names of an array of paths to files" do
|
43
|
-
expr = fixtures_dir + '/*.txt'
|
44
|
-
Dir.glob(expr).file_names('txt').should == ['empty', 'non-empty']
|
45
|
-
end
|
46
|
-
end
|
47
35
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
36
|
+
describe '#has_content?' do
|
37
|
+
it "should find content in file using String argument" do
|
38
|
+
File.overwrite(search_file) do
|
39
|
+
'Find this line right here!'
|
40
|
+
end
|
41
|
+
File.has_content?(search_file, 'line right').should be_true
|
42
|
+
File.has_content?(search_file, 'line left').should be_false
|
55
43
|
end
|
56
|
-
end
|
57
44
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
45
|
+
it "should find content in file using Regexp argument" do
|
46
|
+
File.overwrite(search_file) do
|
47
|
+
'Find this line right here!'
|
48
|
+
end
|
49
|
+
File.has_content?(search_file, /line right/).should be_true
|
50
|
+
File.has_content?(search_file, /line left/).should be_false
|
51
|
+
end
|
62
52
|
end
|
53
|
+
|
54
|
+
describe '#read_from' do
|
63
55
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
56
|
+
it "should read all the content into a block" do
|
57
|
+
File.read_from non_empty_file do |content|
|
58
|
+
content.should match /blip/
|
59
|
+
content.should match /blup/
|
60
|
+
end
|
61
|
+
end
|
69
62
|
|
70
|
-
|
71
|
-
|
63
|
+
it "should read all the content before a given marker" do
|
64
|
+
content = File.read_from non_empty_file, :before => 'blap'
|
72
65
|
content.should match /blip/
|
73
|
-
content.should_not match /blap/
|
66
|
+
content.should_not match /blap/
|
74
67
|
end
|
75
|
-
end
|
76
68
|
|
77
|
-
|
78
|
-
|
69
|
+
it "should read all the content after a given marker" do
|
70
|
+
content = File.read_from non_empty_file, :after => 'blap'
|
79
71
|
content.should match /blup/
|
80
|
-
content.should_not match /blap/
|
72
|
+
content.should_not match /blap/
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should read all the content before a given marker into a block" do
|
76
|
+
File.read_from non_empty_file, :before => 'blap' do |content|
|
77
|
+
content.should match /blip/
|
78
|
+
content.should_not match /blap/
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should read all the content after a given marker into a block" do
|
83
|
+
File.read_from non_empty_file, :after => 'blap' do |content|
|
84
|
+
content.should match /blup/
|
85
|
+
content.should_not match /blap/
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#read_content (instance)' do
|
91
|
+
let(:non_empty_file) { fixture_file 'non-empty.txt' }
|
92
|
+
|
93
|
+
it "should read all the content into a block" do
|
94
|
+
File.new(non_empty_file).read_content do |content|
|
95
|
+
content.should match /blip/
|
96
|
+
content.should match /blup/
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#read_from (class)' do
|
102
|
+
let(:non_empty_file) { fixture_file 'non-empty.txt' }
|
103
|
+
|
104
|
+
it "should read all the content into a block" do
|
105
|
+
File.read_from(non_empty_file) do |content|
|
106
|
+
content.should match /blip/
|
107
|
+
content.should match /blup/
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "Array" do
|
114
|
+
describe '#file_names' do
|
115
|
+
let(:replace_file) { fixture_file 'file.txt' }
|
116
|
+
|
117
|
+
before :each do
|
118
|
+
File.delete replace_file if File.file?(replace_file)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should return all file names of an array of paths to files" do
|
122
|
+
expr = fixtures_dir + '/*.txt'
|
123
|
+
Dir.glob(expr).file_names('txt').should include('empty', 'non-empty')
|
81
124
|
end
|
82
125
|
end
|
83
126
|
end
|
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.
|
8
|
+
s.version = "0.3.4"
|
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-01-
|
12
|
+
s.date = %q{2011-01-18}
|
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 = [
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"lib/sugar-high/array.rb",
|
30
30
|
"lib/sugar-high/blank.rb",
|
31
31
|
"lib/sugar-high/file.rb",
|
32
|
+
"lib/sugar-high/file_mutate.rb",
|
32
33
|
"lib/sugar-high/hash.rb",
|
33
34
|
"lib/sugar-high/includes.rb",
|
34
35
|
"lib/sugar-high/kind_of.rb",
|
@@ -44,6 +45,7 @@ Gem::Specification.new do |s|
|
|
44
45
|
"spec/fixtures/class_file.rb",
|
45
46
|
"spec/fixtures/empty.txt",
|
46
47
|
"spec/fixtures/non-empty.txt",
|
48
|
+
"spec/fixtures/search_file.txt",
|
47
49
|
"spec/spec_helper.rb",
|
48
50
|
"spec/sugar-high/alias_spec.rb",
|
49
51
|
"spec/sugar-high/arguments_spec.rb",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 4
|
9
|
+
version: 0.3.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kristian Mandrup
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-18 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/sugar-high/array.rb
|
85
85
|
- lib/sugar-high/blank.rb
|
86
86
|
- lib/sugar-high/file.rb
|
87
|
+
- lib/sugar-high/file_mutate.rb
|
87
88
|
- lib/sugar-high/hash.rb
|
88
89
|
- lib/sugar-high/includes.rb
|
89
90
|
- lib/sugar-high/kind_of.rb
|
@@ -99,6 +100,7 @@ files:
|
|
99
100
|
- spec/fixtures/class_file.rb
|
100
101
|
- spec/fixtures/empty.txt
|
101
102
|
- spec/fixtures/non-empty.txt
|
103
|
+
- spec/fixtures/search_file.txt
|
102
104
|
- spec/spec_helper.rb
|
103
105
|
- spec/sugar-high/alias_spec.rb
|
104
106
|
- spec/sugar-high/arguments_spec.rb
|