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