duct_tape 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +35 -0
- data/LICENSE +25 -0
- data/README.md +223 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/duct_tape.gemspec +106 -0
- data/ext/mkrf_conf.rb +36 -0
- data/git_hooks/env_vars.sh +287 -0
- data/git_hooks/post-commit +43 -0
- data/git_hooks/post-merge +8 -0
- data/git_hooks/pre-commit +273 -0
- data/install_git_hooks +17 -0
- data/lib/algorithms/containers/heap.rb +15 -0
- data/lib/algorithms/containers/priority_queue.rb +11 -0
- data/lib/algorithms/containers.rb +1 -0
- data/lib/duct_tape/autoassociative_array.rb +110 -0
- data/lib/duct_tape.rb +10 -0
- data/lib/ext/array.rb +327 -0
- data/lib/ext/boolean.rb +3 -0
- data/lib/ext/datetime.rb +7 -0
- data/lib/ext/dir.rb +59 -0
- data/lib/ext/file.rb +40 -0
- data/lib/ext/hash.rb +83 -0
- data/lib/ext/kernel.rb +593 -0
- data/lib/ext/numeric.rb +74 -0
- data/lib/ext/object.rb +17 -0
- data/lib/ext/pathname.rb +114 -0
- data/lib/ext/range.rb +7 -0
- data/lib/ext/regexp.rb +12 -0
- data/lib/ext/string.rb +54 -0
- data/lib/ext/symbol.rb +8 -0
- data/lib/ext/time.rb +32 -0
- data/lib/ext/uri.rb +16 -0
- data/spec/algorithms/containers/heap_spec.rb +19 -0
- data/spec/algorithms/containers/priority_queue_spec.rb +19 -0
- data/spec/duct_tape/autoassociative_array_spec.rb +139 -0
- data/spec/ext/array_spec.rb +407 -0
- data/spec/ext/boolean_spec.rb +19 -0
- data/spec/ext/datetime_spec.rb +10 -0
- data/spec/ext/dir_spec.rb +46 -0
- data/spec/ext/file_spec.rb +10 -0
- data/spec/ext/hash_spec.rb +73 -0
- data/spec/ext/kernel_spec.rb +64 -0
- data/spec/ext/numeric_spec.rb +61 -0
- data/spec/ext/object_spec.rb +19 -0
- data/spec/ext/pathname_spec.rb +13 -0
- data/spec/ext/range_spec.rb +10 -0
- data/spec/ext/regexp_spec.rb +10 -0
- data/spec/ext/string_spec.rb +73 -0
- data/spec/ext/symbol_spec.rb +10 -0
- data/spec/ext/time_spec.rb +19 -0
- data/spec/ext/uri_spec.rb +28 -0
- data/spec/spec_helper.rb +7 -0
- metadata +183 -0
data/lib/ext/pathname.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
class Pathname
|
4
|
+
# Cross-platform way of finding an executable in the $PATH.
|
5
|
+
#
|
6
|
+
# Pathname.which('ruby') #=> /usr/bin/ruby
|
7
|
+
def self.which(cmd)
|
8
|
+
paths = ENV['PATH'].split(File::PATH_SEPARATOR).uniq
|
9
|
+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
|
10
|
+
names = exts.map { |ext| "#{cmd}#{ext}" }
|
11
|
+
return do_search(paths, *names) { |f| f.executable? }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.library(lib)
|
15
|
+
paths = [
|
16
|
+
'/usr/local/lib64',
|
17
|
+
'/usr/local/lib',
|
18
|
+
'/usr/local/libdata',
|
19
|
+
'/opt/local/lib',
|
20
|
+
'/usr/lib64',
|
21
|
+
'/usr/lib',
|
22
|
+
'/usr/X11/lib',
|
23
|
+
'/usr/share',
|
24
|
+
].uniq
|
25
|
+
names = [lib, "#{lib}.so", "lib#{lib}", "lib#{lib}.so"]
|
26
|
+
|
27
|
+
if detect_os[:platform] == "windows"
|
28
|
+
names = [
|
29
|
+
lib, "#{lib}.dll", "#{lib}.dll.a",
|
30
|
+
"lib#{lib}", "lib#{lib}.dll", "lib#{lib}.dll.a"
|
31
|
+
]
|
32
|
+
paths = [
|
33
|
+
calling_method_dirname,
|
34
|
+
Dir.pwd,
|
35
|
+
File.join(ENV["SystemRoot"], "system"),
|
36
|
+
File.join(ENV["SystemRoot"], "system32"),
|
37
|
+
ENV["SystemRoot"],
|
38
|
+
].compact.uniq
|
39
|
+
paths |= ENV['PATH'].split(File::PATH_SEPARATOR).uniq
|
40
|
+
end
|
41
|
+
|
42
|
+
return do_search(paths, *names) { |f| f.readable? && f.file? }
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.header(hdr)
|
46
|
+
paths = [
|
47
|
+
"/usr/local/include",
|
48
|
+
"/opt/include",
|
49
|
+
"/usr/include",
|
50
|
+
RbConfig::CONFIG["includedir"],
|
51
|
+
RbConfig::CONFIG["oldincludedir"],
|
52
|
+
].compact.uniq
|
53
|
+
names = [hdr, hdr + ".h", hdr + ".hpp"]
|
54
|
+
return do_search(paths, *names) { |f| f.readable? && f.file? }
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.join(*paths)
|
58
|
+
new(File.join(*paths))
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_file(*args, &block)
|
62
|
+
File.open(to_s, *args, &block)
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_dir(*args, &block)
|
66
|
+
Dir.new(to_s, *args, &block)
|
67
|
+
end
|
68
|
+
|
69
|
+
=begin
|
70
|
+
def to_object(*args, &block)
|
71
|
+
if socket?
|
72
|
+
if defined?(::UNIXSocket)
|
73
|
+
::UNIXSocket.new(path)
|
74
|
+
else
|
75
|
+
:socket
|
76
|
+
end
|
77
|
+
elsif symlink?
|
78
|
+
link = Pathname.new(File.readlink(name))
|
79
|
+
link = Pathname.join(File.dirname(to_s), link) if link !~ /\A#{SEPARATOR}/
|
80
|
+
# {:symlink => classify_file(link)}
|
81
|
+
link.to_object(*args, &block)
|
82
|
+
elsif directory?
|
83
|
+
(readable?) ? Dir.new(to_s, *args, &block) : :unreadable_directory
|
84
|
+
elsif file?
|
85
|
+
(readable?) ? File.open(to_s, *args, &block) : :unreadable_file
|
86
|
+
elsif blockdev?
|
87
|
+
:block_device
|
88
|
+
elsif chardev?
|
89
|
+
:char_device
|
90
|
+
end
|
91
|
+
rescue Errno::ECONNREFUSED
|
92
|
+
:unreadable_socket
|
93
|
+
rescue Errno::EPROTOTYPE
|
94
|
+
:socket
|
95
|
+
rescue Errno::EACCES
|
96
|
+
:unreadable_item
|
97
|
+
end
|
98
|
+
=end
|
99
|
+
alias_method :exists?, :exist?
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def self.do_search(paths, *try_names, &block)
|
104
|
+
try_names.flatten!
|
105
|
+
paths.each do |path|
|
106
|
+
pn = Pathname.new(path)
|
107
|
+
try_names.each do |name|
|
108
|
+
file = pn + name
|
109
|
+
return file if yield(file)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
end
|
data/lib/ext/range.rb
ADDED
data/lib/ext/regexp.rb
ADDED
data/lib/ext/string.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
class String
|
2
|
+
def uncolorize
|
3
|
+
self.gsub(/\e\[0[;0-9]*m/, "")
|
4
|
+
end
|
5
|
+
|
6
|
+
def colorized?
|
7
|
+
self =~ /\e\[0[;0-9]*m/ && true || false
|
8
|
+
end
|
9
|
+
|
10
|
+
def word_wrap(width=(tty_width || 80).to_i)
|
11
|
+
ret = dup
|
12
|
+
ret.word_wrap!(width)
|
13
|
+
ret
|
14
|
+
end
|
15
|
+
|
16
|
+
def word_wrap!(width=(tty_width || 80).to_i)
|
17
|
+
self.gsub!(/(.{1,#{width}})( +|$\n?)|(.{1,#{width}})/, "\\1\\3\n")
|
18
|
+
end
|
19
|
+
|
20
|
+
def chunk(max_length)
|
21
|
+
type_assert(max_length, Integer)
|
22
|
+
chars.each_slice(max_length).to_a.map! { |ary| ary.join("") }
|
23
|
+
end
|
24
|
+
|
25
|
+
def %(arg)
|
26
|
+
if ::RUBY_VERSION <= "1.8.7" && arg.is_a?(Hash)
|
27
|
+
return gsub(/%\{(\w+)\}/) do |s|
|
28
|
+
if arg.has_key?($1.to_sym)
|
29
|
+
arg[$1.to_sym]
|
30
|
+
else
|
31
|
+
raise KeyError.new("key{#{$1}} not found")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
elsif arg.is_a?(Array)
|
35
|
+
return Kernel.sprintf(self, *arg)
|
36
|
+
else
|
37
|
+
return Kernel.sprintf(self, arg)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Transform self to a Date
|
42
|
+
def to_date
|
43
|
+
Date.parse(self)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Transform self to a Time
|
47
|
+
def to_time
|
48
|
+
Time.parse(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_const
|
52
|
+
split("::").inject(::Object) { |k,c| k.const_get(c) }
|
53
|
+
end
|
54
|
+
end
|
data/lib/ext/symbol.rb
ADDED
data/lib/ext/time.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Time
|
2
|
+
def self.duration(num, usecs=false)
|
3
|
+
type_assert(num, Numeric)
|
4
|
+
secs = num.to_i
|
5
|
+
mins = secs / 60
|
6
|
+
hours = mins / 60
|
7
|
+
days = hours / 24
|
8
|
+
|
9
|
+
secs = num if usecs
|
10
|
+
|
11
|
+
day_str = (days == 1 ? "day" : "days")
|
12
|
+
hour_str = (hours % 24 == 1 ? "hour" : "hours")
|
13
|
+
min_str = (mins % 60 == 1 ? "minute" : "minutes")
|
14
|
+
sec_str = (secs % 60 == 1 ? "second" : "seconds")
|
15
|
+
|
16
|
+
str_ary = []
|
17
|
+
str_ary << "#{days} #{day_str}" if days > 0
|
18
|
+
str_ary << "#{hours % 24} #{hour_str}" if hours > 0
|
19
|
+
str_ary << "#{mins % 60} #{min_str}" if mins > 0
|
20
|
+
str_ary << "#{secs % 60} #{sec_str}" if secs > 0
|
21
|
+
if str_ary.size > 2
|
22
|
+
[str_ary[0..-2].join(", "), str_ary[-1]].join(", and ")
|
23
|
+
else
|
24
|
+
str_ary.join(" and ")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Transform a Time to a DateTime
|
29
|
+
def to_datetime
|
30
|
+
DateTime.parse(self.to_s)
|
31
|
+
end
|
32
|
+
end
|
data/lib/ext/uri.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
class URI::Generic
|
5
|
+
def relative_path?
|
6
|
+
relative? && !path.empty? && Pathname.new(path).relative?
|
7
|
+
end
|
8
|
+
|
9
|
+
def absolute_path?
|
10
|
+
relative? && Pathname.new(path).absolute?
|
11
|
+
end
|
12
|
+
|
13
|
+
def relative_scheme?
|
14
|
+
scheme.nil? && (!host.nil? || !path.empty?)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")
|
2
|
+
|
3
|
+
#
|
4
|
+
# Containers::Heap#to_a
|
5
|
+
#
|
6
|
+
describe Containers::Heap, "#to_a" do
|
7
|
+
it "remains unchanged" do
|
8
|
+
# TODO
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Containers::Heap#each
|
14
|
+
#
|
15
|
+
describe Containers::Heap, "#each" do
|
16
|
+
it "remains unchanged" do
|
17
|
+
# TODO
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "spec_helper.rb")
|
2
|
+
|
3
|
+
#
|
4
|
+
# Containers::PriorityQueue#to_a
|
5
|
+
#
|
6
|
+
describe Containers::PriorityQueue, "#to_a" do
|
7
|
+
it "remains unchanged" do
|
8
|
+
# TODO
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Containers::PriorityQueue#each
|
14
|
+
#
|
15
|
+
describe Containers::PriorityQueue, "#each" do
|
16
|
+
it "remains unchanged" do
|
17
|
+
# TODO
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
|
2
|
+
|
3
|
+
#
|
4
|
+
# Containers::AutoassociativeArray#initialize
|
5
|
+
#
|
6
|
+
describe Containers::AutoassociativeArray, "#initialize" do
|
7
|
+
it "remains unchanged" do
|
8
|
+
# TODO
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Containers::AutoassociativeArray#insert
|
14
|
+
#
|
15
|
+
describe Containers::AutoassociativeArray, "#insert" do
|
16
|
+
it "remains unchanged" do
|
17
|
+
# TODO
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Containers::AutoassociativeArray#<<
|
23
|
+
#
|
24
|
+
describe Containers::AutoassociativeArray, "#<<" do
|
25
|
+
it "remains unchanged" do
|
26
|
+
# TODO
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Containers::AutoassociativeArray#partial_match
|
32
|
+
#
|
33
|
+
describe Containers::AutoassociativeArray, "#partial_match" do
|
34
|
+
it "remains unchanged" do
|
35
|
+
# TODO
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns the matches with the most query matches" do
|
39
|
+
aa = Containers::AutoassociativeArray.new
|
40
|
+
aa << [1,2,3] << [2,3,4] << [3,4,5]
|
41
|
+
aa.partial_match(1,4,5).should eq([3,4,5])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns all of the matches with the most query matches" do
|
45
|
+
aa = Containers::AutoassociativeArray.new
|
46
|
+
aa << [1,2,3] << [2,3,4] << [3,4,5]
|
47
|
+
aa.partial_match(1,5).should eq([[1,2,3], [3,4,5]])
|
48
|
+
aa.partial_match(1,3,5).should eq([[1,2,3], [3,4,5]])
|
49
|
+
aa.partial_match(2,4).should eq([2,3,4])
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns the matches with the most query matches with the most matches" do
|
53
|
+
aa = Containers::AutoassociativeArray.new
|
54
|
+
aa << [1,2,3] << [2,3,4] << [3,4,5]
|
55
|
+
aa.partial_match(1,4).should eq([[2,3,4], [3,4,5]])
|
56
|
+
aa.partial_match(2,5).should eq([[1,2,3], [2,3,4]])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Containers::AutoassociativeArray#[]
|
62
|
+
#
|
63
|
+
describe Containers::AutoassociativeArray, "#[]" do
|
64
|
+
it "remains unchanged" do
|
65
|
+
# TODO
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Containers::AutoassociativeArray#by_column
|
71
|
+
#
|
72
|
+
describe Containers::AutoassociativeArray, "#by_column" do
|
73
|
+
it "remains unchanged" do
|
74
|
+
# TODO
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# Containers::AutoassociativeArray#empty?
|
80
|
+
#
|
81
|
+
describe Containers::AutoassociativeArray, "#empty?" do
|
82
|
+
it "remains unchanged" do
|
83
|
+
# TODO
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Containers::AutoassociativeArray#length
|
89
|
+
#
|
90
|
+
describe Containers::AutoassociativeArray, "#length" do
|
91
|
+
it "remains unchanged" do
|
92
|
+
# TODO
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Containers::AutoassociativeArray#size
|
98
|
+
#
|
99
|
+
describe Containers::AutoassociativeArray, "#size" do
|
100
|
+
it "remains unchanged" do
|
101
|
+
# TODO
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# Containers::AutoassociativeArray#clear
|
107
|
+
#
|
108
|
+
describe Containers::AutoassociativeArray, "#clear" do
|
109
|
+
it "remains unchanged" do
|
110
|
+
# TODO
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# Containers::AutoassociativeArray#inspect
|
116
|
+
#
|
117
|
+
describe Containers::AutoassociativeArray, "#inspect" do
|
118
|
+
it "remains unchanged" do
|
119
|
+
# TODO
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
#
|
124
|
+
# Containers::AutoassociativeArray#to_s
|
125
|
+
#
|
126
|
+
describe Containers::AutoassociativeArray, "#to_s" do
|
127
|
+
it "remains unchanged" do
|
128
|
+
# TODO
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
#
|
133
|
+
# Containers::AutoassociativeArray#dup
|
134
|
+
#
|
135
|
+
describe Containers::AutoassociativeArray, "#dup" do
|
136
|
+
it "remains unchanged" do
|
137
|
+
# TODO
|
138
|
+
end
|
139
|
+
end
|