riel 1.0.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/riel/hash.rb +3 -0
- data/lib/riel/io.rb +13 -0
- data/lib/riel/pathname.rb +8 -0
- data/lib/riel/string.rb +65 -21
- data/test/riel/io_test.rb +17 -2
- data/test/riel/string_test.rb +5 -1
- metadata +47 -47
data/lib/riel/hash.rb
CHANGED
data/lib/riel/io.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
#!/usr/bin/ruby -w
|
2
2
|
# -*- ruby -*-
|
3
3
|
|
4
|
+
require 'riel/file'
|
5
|
+
|
6
|
+
|
4
7
|
class IO
|
5
8
|
|
6
9
|
$-w = false
|
@@ -17,4 +20,14 @@ class IO
|
|
17
20
|
|
18
21
|
$-w = true
|
19
22
|
|
23
|
+
def self.writelines(name, lines)
|
24
|
+
fpn = File._to_pathname(name)
|
25
|
+
|
26
|
+
fpn.open(File::WRONLY | File::TRUNC | File::CREAT) do |f|
|
27
|
+
f.puts lines
|
28
|
+
end
|
29
|
+
|
30
|
+
fpn
|
31
|
+
end
|
32
|
+
|
20
33
|
end
|
data/lib/riel/pathname.rb
CHANGED
@@ -13,4 +13,12 @@ class Pathname
|
|
13
13
|
basename.to_s - extname.to_s
|
14
14
|
end
|
15
15
|
|
16
|
+
# Returns an array of the path split into its components.
|
17
|
+
# for example: "/usr/bin/ls" => "usr", "bin", "ls"
|
18
|
+
def split_path
|
19
|
+
elements = Array.new
|
20
|
+
each_filename { |fn| elements << fn.to_s }
|
21
|
+
elements
|
22
|
+
end
|
23
|
+
|
16
24
|
end
|
data/lib/riel/string.rb
CHANGED
@@ -3,13 +3,27 @@
|
|
3
3
|
|
4
4
|
require 'riel/text'
|
5
5
|
|
6
|
+
#
|
7
|
+
# = string.rb
|
8
|
+
#
|
9
|
+
# RIEL extensions to the Ruby String class.
|
10
|
+
#
|
11
|
+
# Author:: Jeff Pace <jpace@incava.org>
|
12
|
+
# Documentation:: Author
|
13
|
+
#
|
14
|
+
|
6
15
|
class String
|
7
16
|
|
17
|
+
#
|
18
|
+
# Returns whether the string ends with the given substring.
|
19
|
+
#
|
8
20
|
def ends_with(substr)
|
9
21
|
return rindex(substr) == (length - substr.length)
|
10
22
|
end
|
11
23
|
|
12
|
-
#
|
24
|
+
#
|
25
|
+
# Returns the string, as a number if it is one, and nil otherwise,
|
26
|
+
#
|
13
27
|
def num
|
14
28
|
begin
|
15
29
|
Integer(self)
|
@@ -18,31 +32,59 @@ class String
|
|
18
32
|
end
|
19
33
|
end
|
20
34
|
|
21
|
-
#
|
35
|
+
#
|
36
|
+
# Returns a string based on this instance, with the first occurrance of
|
22
37
|
# +other+ removed. +other+ may be a string or regular expression.
|
38
|
+
#
|
23
39
|
def -(other)
|
24
40
|
sub(other, '')
|
25
41
|
end
|
26
42
|
|
43
|
+
#
|
44
|
+
# Represents infinity, for the +to_ranges+ function.
|
45
|
+
#
|
27
46
|
Infinity = (1.0 / 0)
|
28
47
|
|
48
|
+
# :stopdoc:
|
29
49
|
# from (maybe to this)
|
30
50
|
RANGE_REGEXP = %r{^ \s* (\d*) (?: \s* (\-|\.\.) \s* (\d*) )? \s* $ }x
|
51
|
+
# :startdoc:
|
31
52
|
|
32
|
-
#
|
33
|
-
#
|
53
|
+
#
|
54
|
+
# A class method for +to_ranges+. Deprecated in favor of the instance method.
|
55
|
+
#
|
34
56
|
def self.to_ranges(str, args = Hash.new)
|
57
|
+
str.to_ranges(args)
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Splits num into array of ranges, limiting itself to +args[:min]+ and +args[:max]+,
|
62
|
+
# if specified. The +args[:collapse]+, if true, results in sequential values put
|
63
|
+
# into the same range.
|
64
|
+
#
|
65
|
+
# Examples:
|
66
|
+
#
|
67
|
+
# "1,2".to_ranges # [ 1 .. 1, 2 .. 2 ]
|
68
|
+
# "1,2".to_ranges :collapse => true # [ 1 .. 2 ]
|
69
|
+
# "1-4".to_ranges # [ 1 .. 4 ]
|
70
|
+
# "1-3,5-6,10,11,12,".to_ranges :collapse => true # [ 1 .. 3, 5 .. 6, 10 .. 12 ]
|
71
|
+
# "1-3,5-6,10,11,12,".to_ranges # [ 1 .. 3, 5 .. 6, 10 .. 10, 11 .. 11, 12 .. 12 ]
|
72
|
+
# "-4".to_ranges # [ -String::Infinity .. 4 ]
|
73
|
+
# "4-".to_ranges # [ 4 .. String::Infinity ]
|
74
|
+
# "1-".to_ranges :min => 0, :max => 8 # [ 1 .. 8 ]
|
75
|
+
#
|
76
|
+
def to_ranges(args = Hash.new)
|
35
77
|
min = args[:min] || -Infinity
|
36
78
|
max = args[:max] || Infinity
|
37
79
|
collapse = args[:collapse]
|
38
80
|
|
39
81
|
ranges = Array.new
|
40
|
-
|
82
|
+
self.split(%r{\s*,\s*}).each do |section|
|
41
83
|
md = section.match(RANGE_REGEXP)
|
42
84
|
next unless md
|
43
85
|
|
44
|
-
from = _matchdata_to_number(md, 1, min)
|
45
|
-
to = _has_matchdata?(md, 2) ? _matchdata_to_number(md, 3, max) : from
|
86
|
+
from = String._matchdata_to_number(md, 1, min)
|
87
|
+
to = String._has_matchdata?(md, 2) ? String._matchdata_to_number(md, 3, max) : from
|
46
88
|
|
47
89
|
prevrange = ranges[-1]
|
48
90
|
|
@@ -56,26 +98,28 @@ class String
|
|
56
98
|
ranges
|
57
99
|
end
|
58
100
|
|
59
|
-
|
60
|
-
md && md[idx] && !md[idx].empty?
|
61
|
-
end
|
62
|
-
|
63
|
-
def self._matchdata_to_number(md, idx, default)
|
64
|
-
_has_matchdata?(md, idx) ? md[idx].to_i : default
|
65
|
-
end
|
66
|
-
|
67
|
-
def to_ranges
|
68
|
-
String.to_ranges(self)
|
69
|
-
end
|
70
|
-
|
101
|
+
# :stopdoc:
|
71
102
|
HIGHLIGHTER = ::Text::ANSIHighlighter.new
|
72
|
-
|
73
|
-
|
103
|
+
# :startdoc:
|
104
|
+
|
105
|
+
#
|
106
|
+
# Returns a highlighted (colored) version of the string, applying the regular
|
74
107
|
# expressions in the array, which are paired with the desired color.
|
108
|
+
#
|
75
109
|
def highlight(re, color)
|
76
110
|
gsub(re) do |match|
|
77
111
|
HIGHLIGHTER.color(color, match)
|
78
112
|
end
|
79
113
|
end
|
80
114
|
|
115
|
+
# :stopdoc:
|
116
|
+
def self._has_matchdata?(md, idx)
|
117
|
+
md && md[idx] && !md[idx].empty?
|
118
|
+
end
|
119
|
+
|
120
|
+
def self._matchdata_to_number(md, idx, default)
|
121
|
+
_has_matchdata?(md, idx) ? md[idx].to_i : default
|
122
|
+
end
|
123
|
+
# :startdoc:
|
124
|
+
|
81
125
|
end
|
data/test/riel/io_test.rb
CHANGED
@@ -2,11 +2,12 @@
|
|
2
2
|
# -*- ruby -*-
|
3
3
|
|
4
4
|
require 'rubyunit'
|
5
|
-
require 'riel/
|
5
|
+
require 'riel/io'
|
6
|
+
|
6
7
|
|
7
8
|
class IOTestCase < RUNIT::TestCase
|
8
9
|
|
9
|
-
def
|
10
|
+
def xtest_readlines
|
10
11
|
orig = $/
|
11
12
|
|
12
13
|
$/ = nil
|
@@ -19,4 +20,18 @@ class IOTestCase < RUNIT::TestCase
|
|
19
20
|
$/ = orig
|
20
21
|
end
|
21
22
|
|
23
|
+
def test_writelines
|
24
|
+
name = "/tmp/testfile_writelines." + $$.to_s
|
25
|
+
writelines = Array.new
|
26
|
+
writelines << "this is line one"
|
27
|
+
writelines << "line two is this"
|
28
|
+
|
29
|
+
puts "writelines: <<<#{writelines}>>>"
|
30
|
+
|
31
|
+
IO.writelines(name, writelines)
|
32
|
+
|
33
|
+
readlines = IO.readlines name
|
34
|
+
assert_equal writelines.collect { |line| line + "\n" }, readlines
|
35
|
+
end
|
36
|
+
|
22
37
|
end
|
data/test/riel/string_test.rb
CHANGED
@@ -11,6 +11,10 @@ class StringToRangeTestCase < RUNIT::TestCase
|
|
11
11
|
String.to_ranges(str, args).each_with_index do |rg, idx|
|
12
12
|
assert_equal exp[idx], rg
|
13
13
|
end
|
14
|
+
|
15
|
+
str.to_ranges(args).each_with_index do |rg, idx|
|
16
|
+
assert_equal exp[idx], rg
|
17
|
+
end
|
14
18
|
end
|
15
19
|
|
16
20
|
def test_string_to_range
|
@@ -22,7 +26,7 @@ class StringToRangeTestCase < RUNIT::TestCase
|
|
22
26
|
|
23
27
|
do_string_to_range_test([ 1 .. 4 ], "1-4")
|
24
28
|
do_string_to_range_test([ -String::Infinity .. 4 ], "-4")
|
25
|
-
do_string_to_range_test([
|
29
|
+
do_string_to_range_test([ 4 .. String::Infinity ], "4-")
|
26
30
|
|
27
31
|
do_string_to_range_test([ -8 .. 4 ], "-4", :min => -8)
|
28
32
|
do_string_to_range_test([ 0 .. 4 ], "-4", :min => 0)
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 1.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeff Pace
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-04-30 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -29,51 +29,51 @@ extra_rdoc_files:
|
|
29
29
|
- README
|
30
30
|
files:
|
31
31
|
- lib/riel.rb
|
32
|
-
- lib/riel/
|
33
|
-
- lib/riel/date.rb
|
34
|
-
- lib/riel/text.rb
|
32
|
+
- lib/riel/filetype.rb
|
35
33
|
- lib/riel/rcfile.rb
|
36
|
-
- lib/riel/dir.rb
|
37
34
|
- lib/riel/matchdata.rb
|
35
|
+
- lib/riel/enumerable.rb
|
36
|
+
- lib/riel/size_converter.rb
|
37
|
+
- lib/riel/pathname.rb
|
38
|
+
- lib/riel/array.rb
|
38
39
|
- lib/riel/io.rb
|
39
|
-
- lib/riel/
|
40
|
+
- lib/riel/regexp.rb
|
41
|
+
- lib/riel/ansicolor.rb
|
42
|
+
- lib/riel/date.rb
|
40
43
|
- lib/riel/hash.rb
|
41
|
-
- lib/riel/array.rb
|
42
44
|
- lib/riel/optproc.rb
|
43
|
-
- lib/riel/
|
44
|
-
- lib/riel/
|
45
|
+
- lib/riel/command.rb
|
46
|
+
- lib/riel/tempfile.rb
|
45
47
|
- lib/riel/file.rb
|
48
|
+
- lib/riel/dir.rb
|
49
|
+
- lib/riel/text.rb
|
50
|
+
- lib/riel/env.rb
|
51
|
+
- lib/riel/log.rb
|
52
|
+
- lib/riel/timer.rb
|
46
53
|
- lib/riel/string.rb
|
47
54
|
- lib/riel/setdiff.rb
|
48
|
-
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
-
|
52
|
-
- lib/riel/pathname.rb
|
53
|
-
- lib/riel/size_converter.rb
|
54
|
-
- lib/riel/command.rb
|
55
|
+
- test/riel/setdiff_test.rb
|
56
|
+
- test/riel/command_test.rb
|
57
|
+
- test/riel/log_test.rb
|
58
|
+
- test/riel/array_test.rb
|
55
59
|
- test/riel/optproc_test.rb
|
56
|
-
- test/riel/
|
60
|
+
- test/riel/tempfile_test.rb
|
57
61
|
- test/riel/timer_test.rb
|
58
|
-
- test/riel/
|
62
|
+
- test/riel/regexp_test.rb
|
63
|
+
- test/riel/dir_test.rb
|
64
|
+
- test/riel/date_test.rb
|
65
|
+
- test/riel/size_converter_test.rb
|
59
66
|
- test/riel/env_test.rb
|
60
67
|
- test/riel/file_test.rb
|
61
|
-
- test/riel/size_converter_test.rb
|
62
|
-
- test/riel/io_test.rb
|
63
|
-
- test/riel/rcfile_test.rb
|
64
|
-
- test/riel/log_test.rb
|
65
|
-
- test/riel/tempfile_test.rb
|
66
68
|
- test/riel/filetype_test.rb
|
67
|
-
- test/riel/
|
68
|
-
- test/riel/
|
69
|
+
- test/riel/text_test.rb
|
70
|
+
- test/riel/string_test.rb
|
69
71
|
- test/riel/enumerable_test.rb
|
70
|
-
- test/riel/dir_test.rb
|
71
72
|
- test/riel/matchdata_test.rb
|
72
73
|
- test/riel/pathname_test.rb
|
73
|
-
- test/riel/
|
74
|
-
- test/riel/
|
75
|
-
- test/riel/
|
76
|
-
- test/riel/date_test.rb
|
74
|
+
- test/riel/hash_test.rb
|
75
|
+
- test/riel/rcfile_test.rb
|
76
|
+
- test/riel/io_test.rb
|
77
77
|
- README
|
78
78
|
has_rdoc: true
|
79
79
|
homepage: http://www.incava.org/projects/riel
|
@@ -110,25 +110,25 @@ signing_key:
|
|
110
110
|
specification_version: 3
|
111
111
|
summary: Set of extensions to core Ruby code.
|
112
112
|
test_files:
|
113
|
+
- test/riel/setdiff_test.rb
|
114
|
+
- test/riel/command_test.rb
|
115
|
+
- test/riel/log_test.rb
|
116
|
+
- test/riel/array_test.rb
|
113
117
|
- test/riel/optproc_test.rb
|
114
|
-
- test/riel/
|
118
|
+
- test/riel/tempfile_test.rb
|
115
119
|
- test/riel/timer_test.rb
|
116
|
-
- test/riel/
|
120
|
+
- test/riel/regexp_test.rb
|
121
|
+
- test/riel/dir_test.rb
|
122
|
+
- test/riel/date_test.rb
|
123
|
+
- test/riel/size_converter_test.rb
|
117
124
|
- test/riel/env_test.rb
|
118
125
|
- test/riel/file_test.rb
|
119
|
-
- test/riel/size_converter_test.rb
|
120
|
-
- test/riel/io_test.rb
|
121
|
-
- test/riel/rcfile_test.rb
|
122
|
-
- test/riel/log_test.rb
|
123
|
-
- test/riel/tempfile_test.rb
|
124
126
|
- test/riel/filetype_test.rb
|
125
|
-
- test/riel/
|
126
|
-
- test/riel/
|
127
|
+
- test/riel/text_test.rb
|
128
|
+
- test/riel/string_test.rb
|
127
129
|
- test/riel/enumerable_test.rb
|
128
|
-
- test/riel/dir_test.rb
|
129
130
|
- test/riel/matchdata_test.rb
|
130
131
|
- test/riel/pathname_test.rb
|
131
|
-
- test/riel/
|
132
|
-
- test/riel/
|
133
|
-
- test/riel/
|
134
|
-
- test/riel/date_test.rb
|
132
|
+
- test/riel/hash_test.rb
|
133
|
+
- test/riel/rcfile_test.rb
|
134
|
+
- test/riel/io_test.rb
|