rufus-dollar 1.0.1 → 1.0.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.
- data/CHANGELOG.txt +4 -0
- data/README.txt +26 -21
- data/lib/rufus-dollar.rb +3 -0
- data/lib/rufus/dollar.rb +46 -71
- data/test/dollar_test.rb +43 -47
- data/test/nested_test.rb +14 -15
- data/test/test.rb +2 -2
- data/test/test_base.rb +12 -5
- metadata +4 -3
data/CHANGELOG.txt
CHANGED
data/README.txt
CHANGED
@@ -6,7 +6,7 @@ A one-method library for substituting ${stuff} in text strings.
|
|
6
6
|
|
7
7
|
== getting it
|
8
8
|
|
9
|
-
|
9
|
+
sudo gem install rufus-dollar
|
10
10
|
|
11
11
|
or at
|
12
12
|
|
@@ -15,23 +15,23 @@ http://rubyforge.org/frs/?group_id=4812
|
|
15
15
|
|
16
16
|
== usage
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
18
|
+
require 'rubygems'
|
19
|
+
require 'rufus/dollar'
|
20
|
+
|
21
|
+
h = {
|
22
|
+
'name' => 'Fred Brooks',
|
23
|
+
'title' => 'Silver Bullet',
|
24
|
+
'left' => 'na',
|
25
|
+
'right' => 'me',
|
26
|
+
}
|
27
|
+
|
28
|
+
puts Rufus::dsub "${name} wrote '${title}'", h
|
29
|
+
# => "Fred Brooks wrote 'Silver Bullet'"
|
30
|
+
|
31
|
+
# dollar notations are nestable
|
32
|
+
|
33
|
+
puts Rufus::dsub "${${left}${right}}", h
|
34
|
+
# => "${name}" => "Fred Brooks"
|
35
35
|
|
36
36
|
|
37
37
|
== dependencies
|
@@ -43,7 +43,7 @@ None.
|
|
43
43
|
|
44
44
|
On the rufus-ruby list[http://groups.google.com/group/rufus-ruby] :
|
45
45
|
|
46
|
-
|
46
|
+
http://groups.google.com/group/rufus-ruby
|
47
47
|
|
48
48
|
|
49
49
|
== issue tracker
|
@@ -53,9 +53,9 @@ http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
|
|
53
53
|
|
54
54
|
== source
|
55
55
|
|
56
|
-
http://
|
56
|
+
http://github.com/jmettraux/rufus-dollar
|
57
57
|
|
58
|
-
|
58
|
+
git clone git://github.com/jmettraux/rufus-dollar.git
|
59
59
|
|
60
60
|
|
61
61
|
== author
|
@@ -64,6 +64,11 @@ John Mettraux, jmettraux@gmail.com
|
|
64
64
|
http://jmettraux.wordpress.com
|
65
65
|
|
66
66
|
|
67
|
+
== the rest of Rufus
|
68
|
+
|
69
|
+
http://rufus.rubyforge.org
|
70
|
+
|
71
|
+
|
67
72
|
== license
|
68
73
|
|
69
74
|
MIT
|
data/lib/rufus-dollar.rb
ADDED
data/lib/rufus/dollar.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
2
|
#--
|
3
|
-
# Copyright (c) 2006-
|
3
|
+
# Copyright (c) 2006-2009, John Mettraux, jmettraux@gmail.com
|
4
4
|
#
|
5
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
# of this software and associated documentation files (the "Software"), to deal
|
@@ -8,10 +8,10 @@
|
|
8
8
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
9
|
# copies of the Software, and to permit persons to whom the Software is
|
10
10
|
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# The above copyright notice and this permission notice shall be included in
|
13
13
|
# all copies or substantial portions of the Software.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
16
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
17
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -31,90 +31,65 @@
|
|
31
31
|
|
32
32
|
module Rufus
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
text = text.to_s
|
52
|
-
|
53
|
-
#puts "### text is >#{text}<"
|
54
|
-
#puts "### dict is of class #{dict.class.name}"
|
55
|
-
|
56
|
-
j = text.index("}", offset || 0)
|
34
|
+
#
|
35
|
+
# Performs 'dollar substitution' on a piece of text with a given
|
36
|
+
# dictionary.
|
37
|
+
#
|
38
|
+
# require 'rubygems'
|
39
|
+
# require 'rufus/dollar'
|
40
|
+
#
|
41
|
+
# h = {
|
42
|
+
# "name" => "Fred Brooke",
|
43
|
+
# "title" => "Silver Bullet"
|
44
|
+
# }
|
45
|
+
#
|
46
|
+
# puts Rufus::dsub "${name} wrote '${title}'", h
|
47
|
+
# # => "Fred Brooke wrote 'Silver Bullet'"
|
48
|
+
#
|
49
|
+
def self.dsub (text, dict, offset=nil)
|
57
50
|
|
58
|
-
|
51
|
+
text = text.to_s
|
59
52
|
|
60
|
-
|
53
|
+
j = text.index('}', offset || 0)
|
61
54
|
|
62
|
-
|
63
|
-
ii = t.rindex("\\${")
|
55
|
+
return text unless j
|
64
56
|
|
65
|
-
|
66
|
-
iii = nil if offset
|
57
|
+
t = text[0, j]
|
67
58
|
|
68
|
-
|
69
|
-
|
70
|
-
#puts "ii is #{ii}"
|
71
|
-
#puts "iii is #{iii}"
|
59
|
+
i = t.rindex('${')
|
60
|
+
ii = t.rindex("\\${")
|
72
61
|
|
73
|
-
|
74
|
-
|
62
|
+
iii = t.rindex('{')
|
63
|
+
iii = nil if offset
|
75
64
|
|
76
|
-
|
77
|
-
|
78
|
-
# found "\${"
|
79
|
-
|
80
|
-
key = text[i+2..j-1]
|
65
|
+
return text unless i
|
66
|
+
return dsub(text, dict, j+1) if (iii) and (iii-1 > i)
|
81
67
|
|
82
|
-
|
68
|
+
return unescape(text) if (i) and (i != 0) and (ii == i-1)
|
69
|
+
#
|
70
|
+
# found "\${"
|
83
71
|
|
84
|
-
|
72
|
+
key = text[i+2..j-1]
|
85
73
|
|
86
|
-
|
74
|
+
value = dict[key]
|
87
75
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
else
|
94
|
-
""
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
#puts "### value 1 is '#{value}'"
|
99
|
-
|
100
|
-
#puts "### pre is >#{text[0..i-1]}<"
|
101
|
-
#puts "### post is >#{text[j+1..-1]}<"
|
76
|
+
value = if value
|
77
|
+
value.to_s
|
78
|
+
else
|
79
|
+
dict.has_key?(key) ? 'false' : ''
|
80
|
+
end
|
102
81
|
|
103
|
-
|
104
|
-
text[0..i-1]
|
105
|
-
else
|
106
|
-
""
|
107
|
-
end
|
82
|
+
pre = (i > 0) ? text[0..i-1] : ''
|
108
83
|
|
109
|
-
|
110
|
-
|
84
|
+
dsub("#{pre}#{value}#{text[j+1..-1]}", dict)
|
85
|
+
end
|
111
86
|
|
112
|
-
|
87
|
+
private
|
113
88
|
|
114
|
-
|
89
|
+
def self.unescape (text)
|
115
90
|
|
116
|
-
|
117
|
-
|
91
|
+
text.gsub("\\\\\\$\\{", "\\${")
|
92
|
+
end
|
118
93
|
|
119
94
|
end
|
120
95
|
|
data/test/dollar_test.rb
CHANGED
@@ -8,77 +8,73 @@
|
|
8
8
|
#
|
9
9
|
|
10
10
|
require 'test/unit'
|
11
|
-
require '
|
12
|
-
require 'test_base'
|
11
|
+
require File.dirname(__FILE__) + '/test_base'
|
13
12
|
|
14
|
-
#
|
15
|
-
# testing the 'dollar notation'
|
16
|
-
#
|
17
13
|
|
18
14
|
class DollarTest < Test::Unit::TestCase
|
19
|
-
|
15
|
+
include TestBase
|
20
16
|
|
21
|
-
|
22
|
-
|
17
|
+
#def setup
|
18
|
+
#end
|
23
19
|
|
24
|
-
|
25
|
-
|
20
|
+
#def teardown
|
21
|
+
#end
|
26
22
|
|
27
|
-
|
23
|
+
def test_0
|
28
24
|
|
29
|
-
|
25
|
+
dict = {}
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
dict['renard'] = 'goupil'
|
28
|
+
dict['cane'] = 'oie'
|
29
|
+
dict['oie blanche'] = 'poule'
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
dotest "le petit renard", dict, "le petit renard"
|
32
|
+
dotest "le petit {renard}", dict, "le petit {renard}"
|
33
|
+
dotest "le petit ${renard}", dict, "le petit goupil"
|
34
|
+
dotest "le petit ${renard} noir", dict, "le petit goupil noir"
|
39
35
|
|
40
|
-
|
36
|
+
dotest "la grande ${${cane} blanche}", dict, "la grande poule"
|
41
37
|
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
dotest "le ${renard} et la ${cane}", dict, "le goupil et la oie"
|
39
|
+
#
|
40
|
+
# excuse my french...
|
45
41
|
|
46
|
-
|
42
|
+
dotest "le \\${renard} encore", dict, "le \\${renard} encore"
|
47
43
|
|
48
|
-
|
44
|
+
dotest "", dict, ""
|
49
45
|
|
50
|
-
|
46
|
+
dotest("""
|
51
47
|
""", dict, """
|
52
48
|
""")
|
53
|
-
|
49
|
+
dotest("""
|
54
50
|
""", dict, """
|
55
51
|
""")
|
56
|
-
|
52
|
+
end
|
57
53
|
|
58
|
-
|
54
|
+
def test_1
|
59
55
|
|
60
|
-
|
61
|
-
|
56
|
+
dict = {}
|
57
|
+
dict['x'] = 'y'
|
62
58
|
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
dotest "${x}", dict, "y"
|
60
|
+
dotest "\\${x}", dict, "\\${x}"
|
61
|
+
end
|
66
62
|
|
67
|
-
|
63
|
+
def test_2
|
68
64
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
65
|
+
dict = {}
|
66
|
+
dict['A'] = 'a'
|
67
|
+
dict['B'] = 'b'
|
68
|
+
dict['ab'] = 'ok'
|
73
69
|
|
74
|
-
|
75
|
-
|
70
|
+
dotest "${${A}${B}}", dict, "ok"
|
71
|
+
end
|
76
72
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
#def test_3
|
74
|
+
# assert_equal OpenWFE.unescape("toto and ${toto}"), "toto and ${toto}"
|
75
|
+
# assert_equal OpenWFE.unescape("toto & \${toto}"), "toto & ${toto}"
|
76
|
+
# assert_equal "toto & \\${toto}", "toto & ${toto}"
|
77
|
+
# #assert_equal OpenWFE.unescape('toto & \${toto}'), "toto & ${toto}"
|
78
|
+
#end
|
83
79
|
|
84
80
|
end
|
data/test/nested_test.rb
CHANGED
@@ -7,25 +7,24 @@
|
|
7
7
|
# Mon Feb 18 23:32:12 JST 2008
|
8
8
|
#
|
9
9
|
|
10
|
-
require '
|
11
|
-
|
12
|
-
require 'test_base'
|
10
|
+
require File.dirname(__FILE__) + '/test_base'
|
11
|
+
|
13
12
|
|
14
13
|
class NestedTest < Test::Unit::TestCase
|
15
|
-
|
14
|
+
include TestBase
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
#def setup
|
17
|
+
#end
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
#def teardown
|
20
|
+
#end
|
22
21
|
|
23
|
-
|
22
|
+
def test_0
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
dotest " ${a${b}e} ", {}, " "
|
25
|
+
dotest " ${a{b}e} ", {}, " "
|
26
|
+
dotest "${a{b}e}", {}, ""
|
27
|
+
dotest " \\${a{b}e} ", {}, " \\${a{b}e} "
|
28
|
+
dotest "{a${b}c}", { "b" => 2 }, "{a2c}"
|
29
|
+
end
|
31
30
|
end
|
data/test/test.rb
CHANGED
data/test/test_base.rb
CHANGED
@@ -7,14 +7,21 @@
|
|
7
7
|
# Mon Feb 18 23:30:37 JST 2008
|
8
8
|
#
|
9
9
|
|
10
|
+
$:.unshift File.dirname(__FILE__) + '/../lib/'
|
11
|
+
|
12
|
+
require 'test/unit'
|
13
|
+
require 'rufus/dollar'
|
14
|
+
|
15
|
+
|
10
16
|
module TestBase
|
11
17
|
|
12
|
-
|
18
|
+
protected
|
13
19
|
|
14
|
-
|
20
|
+
def dotest (text, dict, target)
|
15
21
|
|
16
|
-
|
22
|
+
result = Rufus.dsub(text, dict)
|
17
23
|
|
18
|
-
|
19
|
-
|
24
|
+
assert_equal target, result
|
25
|
+
end
|
20
26
|
end
|
27
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufus-dollar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Mettraux
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-04 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,6 +26,7 @@ extra_rdoc_files:
|
|
26
26
|
files:
|
27
27
|
- lib/rufus
|
28
28
|
- lib/rufus/dollar.rb
|
29
|
+
- lib/rufus-dollar.rb
|
29
30
|
- test/dollar_test.rb
|
30
31
|
- test/nested_test.rb
|
31
32
|
- test/test.rb
|
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
requirements: []
|
56
57
|
|
57
58
|
rubyforge_project:
|
58
|
-
rubygems_version:
|
59
|
+
rubygems_version: 1.3.1
|
59
60
|
signing_key:
|
60
61
|
specification_version: 2
|
61
62
|
summary: ${xxx} substitutions
|