sugar-high 0.4.0 → 0.4.3
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.
- data/VERSION +1 -1
- data/lib/sugar-high/array.rb +58 -7
- data/lib/sugar-high/class_ext.rb +11 -0
- data/lib/sugar-high/enumerable.rb +67 -0
- data/lib/sugar-high/file.rb +24 -76
- data/lib/sugar-high/file_ext.rb +65 -0
- data/lib/sugar-high/file_mutate.rb +45 -192
- data/lib/sugar-high/file_mutate/append_content.rb +17 -0
- data/lib/sugar-high/file_mutate/delete.rb +29 -0
- data/lib/sugar-high/file_mutate/insert_content.rb +62 -0
- data/lib/sugar-high/file_mutate/mutate.rb +58 -0
- data/lib/sugar-high/file_mutate/overwrite_content.rb +17 -0
- data/lib/sugar-high/file_mutate/remove_content.rb +33 -0
- data/lib/sugar-high/file_mutate/replace_content.rb +45 -0
- data/lib/sugar-high/kind_of.rb +8 -43
- data/lib/sugar-high/string.rb +5 -0
- data/spec/fixtures/application_file.rb +1 -0
- data/spec/fixtures/class_file.rb +15 -0
- data/spec/fixtures/content_file.txt +1 -0
- data/spec/fixtures/file.txt +1 -0
- data/spec/fixtures/routes_file.rb +16 -0
- data/spec/sugar-high/array_spec.rb +44 -3
- data/spec/sugar-high/file/file_dsl_spec.rb +4 -0
- data/spec/sugar-high/file_mutate/append_content_spec.rb +60 -0
- data/spec/sugar-high/file_mutate/delete_spec.rb +47 -0
- data/spec/sugar-high/file_mutate/insert_before_last_spec.rb +56 -0
- data/spec/sugar-high/file_mutate/insert_content_spec.rb +111 -0
- data/spec/sugar-high/file_mutate/overwrite_content_spec.rb +80 -0
- data/spec/sugar-high/file_mutate/remove_content_spec.rb +109 -0
- data/spec/sugar-high/file_mutate/replace_content_spec.rb +33 -0
- data/spec/sugar-high/{file/file_spec.rb → file_spec.rb} +1 -0
- data/sugar-high.gemspec +26 -27
- metadata +27 -28
- data/spec/sugar-high/file/file_mutate_spec.rb +0 -325
@@ -0,0 +1,17 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module AppendContent
|
4
|
+
def append content=nil, &block
|
5
|
+
File.append self.path, content, &block
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def append path, content=nil, &block
|
10
|
+
File.open(path, 'a') do |f|
|
11
|
+
f.puts content ||= yield
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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
|
+
module SugarHigh
|
9
|
+
module FileMutate
|
10
|
+
module Delete
|
11
|
+
module ClassMethods
|
12
|
+
def delete! name
|
13
|
+
return nil if !File.exist?(name)
|
14
|
+
File.delete name
|
15
|
+
end
|
16
|
+
alias_method :delete_file!, :delete!
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete!
|
20
|
+
File.delete(self.path)
|
21
|
+
end
|
22
|
+
alias_method :delete_file!, :delete!
|
23
|
+
|
24
|
+
def mutate marker, place, &block
|
25
|
+
File.mutate_file self.path, marker, place, &block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module InsertContent
|
4
|
+
def insert *args, &block
|
5
|
+
File.insert_into self.path, *args, &block
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# insert_into 'my_file.txt', :after => 'Blip', :content => 'Hello
|
10
|
+
# insert_into 'my_file.txt', 'Hello', :after => 'Blip'
|
11
|
+
# insert_into 'my_file.txt', :after => 'Blip' do
|
12
|
+
# 'Hello'
|
13
|
+
# end
|
14
|
+
def insert_into file_name, *args, &block
|
15
|
+
options = last_option args
|
16
|
+
content = insertion_content options, *args, &block
|
17
|
+
|
18
|
+
# no content to insert?
|
19
|
+
return nil if content.blank?
|
20
|
+
|
21
|
+
file = begin
|
22
|
+
get_file(file_name)
|
23
|
+
rescue
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# already inserted?
|
28
|
+
return nil if !options[:repeat] && file.has_content?(content)
|
29
|
+
|
30
|
+
# find where to insert
|
31
|
+
place, marker = if options[:before]
|
32
|
+
[ :before, options[:before] ]
|
33
|
+
elsif options[:before_last]
|
34
|
+
[ :before_last, options[:before_last] ]
|
35
|
+
else
|
36
|
+
[ :after, options[:after] ]
|
37
|
+
end
|
38
|
+
|
39
|
+
return nil if !file.has_content?(marker)
|
40
|
+
|
41
|
+
# do mutation
|
42
|
+
res = file.mutate marker, place do
|
43
|
+
content
|
44
|
+
end
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
def insertion_content options = {}, *args, &block
|
49
|
+
case args.first
|
50
|
+
when String
|
51
|
+
args.first
|
52
|
+
when Hash
|
53
|
+
options[:content] || (yield if block)
|
54
|
+
else
|
55
|
+
return yield if block
|
56
|
+
raise ArgumentError, "You must supply content to insert, either as a String before the options hash, a :content option or a block"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module Mutate
|
4
|
+
module ClassMethods
|
5
|
+
def mutate_file file, marker, place, &block
|
6
|
+
raise ArgumentError, "You must define a replacement marker for a :before, :before_last or :after key" if !marker
|
7
|
+
|
8
|
+
file = File.get_file(file)
|
9
|
+
|
10
|
+
if place == :before_last
|
11
|
+
content = file.read
|
12
|
+
content = content.insert_before_last yield, marker
|
13
|
+
file.overwrite content
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
marker_found = file.has_content? marker
|
18
|
+
return nil if !marker_found
|
19
|
+
|
20
|
+
replace_in_file file, /(#{marker})/mi do |match|
|
21
|
+
place == :after ? "#{match}\n #{yield}" : "#{yield}\n #{match}"
|
22
|
+
end
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def replace_in_file(file, regexp, *args, &block)
|
27
|
+
file = File.get_file(file)
|
28
|
+
content = file.read.gsub(regexp, *args, &block)
|
29
|
+
file.overwrite content
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def get_file file_name
|
36
|
+
case file_name
|
37
|
+
when PathString, String
|
38
|
+
File.new file_name
|
39
|
+
when File
|
40
|
+
file_name
|
41
|
+
else
|
42
|
+
raise ArgumentError, "Could not be converted to a File object: #{file_name}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_filepath file
|
47
|
+
case file
|
48
|
+
when PathString, String
|
49
|
+
file
|
50
|
+
when File
|
51
|
+
file.path
|
52
|
+
else
|
53
|
+
raise ArgumentError, "Could not be converted to a file path: #{file_name}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module OverwriteContent
|
4
|
+
def overwrite content=nil, &block
|
5
|
+
File.overwrite self.path, content, &block
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def overwrite file, content=nil, &block
|
10
|
+
File.open(get_filepath(file).path, 'w') do |f|
|
11
|
+
f.puts content ||= yield
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module RemoveContent
|
4
|
+
def remove_content options=nil, &block
|
5
|
+
opt_str = case options
|
6
|
+
when String
|
7
|
+
options
|
8
|
+
when Hash
|
9
|
+
content = options[:content] || options[:where]
|
10
|
+
raise ArgumentError, "Bad :content value in Hash" if !content || content.strip.empty?
|
11
|
+
content.strip
|
12
|
+
else
|
13
|
+
raise ArgumentError, "non-block argument must be either String or Hash with a :content option" if !block
|
14
|
+
end
|
15
|
+
content = block ? yield : opt_str
|
16
|
+
|
17
|
+
File.remove_content_from self.path, :content => content, :with => '', &block
|
18
|
+
end
|
19
|
+
alias_method :remove, :remove_content
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def remove_from file_name, content=nil, &block
|
23
|
+
content ||= yield
|
24
|
+
replace_content_from file_name, :content => content, :with => '', &block
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_content_from file_name, options = {}, &block
|
28
|
+
replace_content_from file_name, options.merge(:with => ''), &block
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module SugarHigh
|
2
|
+
module FileMutate
|
3
|
+
module ReplaceContent
|
4
|
+
def replace_content options = {}, &block
|
5
|
+
File.replace_content_from self.path, options, &block
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# replaces content found at replacement_expr with content resulting from yielding block
|
10
|
+
# File.replace_content_from 'myfile.txt', where => /HelloWorld/, with => 'GoodBye'
|
11
|
+
def replace_content_from file_name, options = {}, &block
|
12
|
+
replacement_expr = options[:where] || options[:content]
|
13
|
+
new_content = options[:with]
|
14
|
+
|
15
|
+
begin
|
16
|
+
replacement_expr = replacement_expr.to_regexp
|
17
|
+
rescue
|
18
|
+
raise ArgumentError, "Content to be replaced must be specified as either a String or Regexp in a :where or :content option"
|
19
|
+
end
|
20
|
+
|
21
|
+
file = get_file file_name
|
22
|
+
|
23
|
+
# get existing file content
|
24
|
+
content = file.read
|
25
|
+
|
26
|
+
# return nil if no mathing replacement found
|
27
|
+
return nil if !(content =~ replacement_expr)
|
28
|
+
|
29
|
+
new_content ||= yield if block
|
30
|
+
|
31
|
+
raise ArgumentError, "Content to be replaced with must be specified as a :with option or as a block" if !new_content
|
32
|
+
|
33
|
+
# remove content that matches expr, by replacing with empty
|
34
|
+
mutated_content = content.gsub replacement_expr, new_content
|
35
|
+
|
36
|
+
# write mutated content as new file
|
37
|
+
file.overwrite mutated_content
|
38
|
+
|
39
|
+
true # signal success!
|
40
|
+
end
|
41
|
+
alias_method :replace_content_in, :replace_content_from
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/sugar-high/kind_of.rb
CHANGED
@@ -12,6 +12,13 @@ class Object
|
|
12
12
|
false
|
13
13
|
end
|
14
14
|
|
15
|
+
def not_any_kind_of? *kinds
|
16
|
+
kinds.all_kinds.each do |kind|
|
17
|
+
return false if self.kind_of? kind
|
18
|
+
end
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
15
22
|
def kind_of_label?
|
16
23
|
self.any_kind_of? String, Symbol
|
17
24
|
end
|
@@ -20,49 +27,7 @@ class Object
|
|
20
27
|
self.any_kind_of? Symbols, Symbol
|
21
28
|
end
|
22
29
|
end
|
23
|
-
|
24
|
-
module Enumerable
|
25
|
-
def only_kinds_of? *kinds
|
26
|
-
all?{|a| a.any_kind_of? *kinds }
|
27
|
-
end
|
28
|
-
|
29
|
-
def only_labels?
|
30
|
-
all?{|a| a.kind_of_label? }
|
31
|
-
end
|
32
|
-
|
33
|
-
def select_kinds_of *kinds
|
34
|
-
select{|a| a.any_kind_of? *kinds }
|
35
|
-
end
|
36
|
-
|
37
|
-
def select_labels
|
38
|
-
select{|a| a.kind_of_label? }
|
39
|
-
end
|
40
|
-
|
41
|
-
def select_symbols
|
42
|
-
select{|a| a.kind_of_symbol? }
|
43
|
-
end
|
44
|
-
|
45
|
-
def select_strings
|
46
|
-
select_only :string
|
47
|
-
end
|
48
|
-
|
49
|
-
def select_only type
|
50
|
-
const = type.kind_of_label? ? "#{type.to_s.camelize}".constantize : type
|
51
|
-
select{|a| a.kind_of? const}
|
52
|
-
end
|
53
|
-
|
54
|
-
def all_kinds
|
55
|
-
map do |a|
|
56
|
-
case a
|
57
|
-
when Kinds
|
58
|
-
a.kinds
|
59
|
-
else
|
60
|
-
a if a.kind_of?(Module)
|
61
|
-
end
|
62
|
-
end.compact.uniq.flatten
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
30
|
+
|
66
31
|
class Kinds
|
67
32
|
attr_accessor :kinds
|
68
33
|
|
data/lib/sugar-high/string.rb
CHANGED
data/spec/fixtures/class_file.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
{:with=>"New content"}
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello Me
|
@@ -3,19 +3,27 @@ require 'sugar-high/array'
|
|
3
3
|
|
4
4
|
describe "SugarHigh" do
|
5
5
|
describe "Array ext" do
|
6
|
+
|
6
7
|
describe '#to_symbols' do
|
7
8
|
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
8
9
|
[1, 'blip', [3, "hello"]].to_symbols.should == [:blip, :hello]
|
9
10
|
end
|
10
11
|
|
11
12
|
it "should translate nested array of numbers and strings into symbols only array, including numbers" do
|
12
|
-
[[1, 'blip', [3, "hello"]]].
|
13
|
+
[[1, 'blip', [3, "hello"]]].to_symbols_num.should == [:_1, :blip, :_3, :hello]
|
13
14
|
end
|
14
15
|
|
15
|
-
it "should translate nested array of numbers and strings into
|
16
|
-
[['blip', [
|
16
|
+
it "should translate nested array of numbers and strings into symbols only array, including numbers" do
|
17
|
+
[[1, 'blip', [1, "hello"]]].to_symbols_uniq.should include :blip, :hello
|
17
18
|
end
|
18
19
|
end
|
20
|
+
|
21
|
+
describe '#to_symbols!' do
|
22
|
+
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
23
|
+
x = [1, 'hello', 'blip', [3, "hello"]].to_symbols!
|
24
|
+
x.should include :hello, :blip
|
25
|
+
end
|
26
|
+
end
|
19
27
|
|
20
28
|
describe '#to_strings' do
|
21
29
|
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
@@ -23,6 +31,13 @@ describe "SugarHigh" do
|
|
23
31
|
end
|
24
32
|
end
|
25
33
|
|
34
|
+
describe '#to_strings!' do
|
35
|
+
it "should translate nested array of numbers and strings into symbols only array, excluding numbers" do
|
36
|
+
x = [1, 'blip', [3, "hello"]].to_strings!
|
37
|
+
x.should == ['blip', 'hello']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
26
41
|
describe '#flat_uniq' do
|
27
42
|
it "should flatten array, remove nils and make unique" do
|
28
43
|
[1, 'blip', ['blip', nil, 'c'], nil].flat_uniq.should == [1, 'blip', 'c']
|
@@ -33,6 +48,32 @@ describe "SugarHigh" do
|
|
33
48
|
end
|
34
49
|
end
|
35
50
|
|
51
|
+
describe '#flat_uniq!' do
|
52
|
+
it "should flatten array, remove nils and make unique" do
|
53
|
+
x = [1, 'blip', ['blip', nil, 'c'], nil]
|
54
|
+
x.flat_uniq!
|
55
|
+
x.should == [1, 'blip', 'c']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#sum' do
|
60
|
+
it "should add elements in array" do
|
61
|
+
[1, 2, 3].sum.should == 6
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#mean' do
|
66
|
+
it "should find mean of elements in array" do
|
67
|
+
[1, 2, 3].mean.should == 2
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#extract' do
|
72
|
+
it "should call method on each element in array" do
|
73
|
+
["a", "ab", "abc"].extract(:size).mean.should == 2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
36
77
|
describe '#none?' do
|
37
78
|
it "should be none if no real values in array" do
|
38
79
|
[nil, nil].none?.should be_true
|