Skip to content

Commit 1f8a96a

Browse files
committedMar 3, 2014
Changed :transform to :map
1 parent d2975b7 commit 1f8a96a

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed
 

‎lib/erb_parser.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ module ErbParser
99
def self.parse(str, options = {})
1010
result = ParsedErb.new TreetopRunner.run(str)
1111

12-
if options[:transform]
12+
if options[:transform] or options[:map]
1313
result.tokens.map do |elem|
1414
case elem
1515
when String
1616
elem
1717
when ErbTag
18-
options[:transform].call(elem)
18+
(options[:transform] || options[:map]).call(elem)
1919
end
2020
end.join
2121
else

‎readme.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,26 @@ Pass in an ERB template as a string:
3535

3636
The above usage sets `result` to an array of tokens.
3737

38-
## The `:transform` option
38+
## The `:map` option
3939

4040
Often, you'll want to parse an ERB template, apply some transformation function to all
41-
the ERB tags, and get back a new string. That's what the `:transform` option is for:
41+
the ERB tags, and get back a new string. That's what the `:map` option is for:
4242

4343
result = ErbParser.parse(
4444
'This is a<% TSET %>',
45-
:transform => lambda { |t| t.ruby_code.reverse.downcase }
45+
:map => lambda { |t| t.ruby_code.reverse.downcase }
4646
)
4747

4848
# 'This is a test '
4949
puts result
5050

51-
As in the above example, the `:transform` option must be a proc. The proc will be passed
51+
As in the above example, the `:map` option must be a proc. The proc will be passed
5252
an `ErbTag` as its only parameter. It must return a string. The returned string will be
5353
inserted into the output where the ERB tag used to be.
5454

55+
`:map` was called `:transform` in version 0.0.0. `:transform` is still available but
56+
deprecated.
57+
5558
## XML/HTML usage synopsis
5659

5760
Alternatively, you may pass in an XML/HTML document or fragment and have the ERB tags

‎test/api_test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def test_complex_document
5757

5858
def test_transform_option
5959
str = 'foo<% bar %>baz'
60+
result = ErbParser.parse str, :map => lambda { |tag| tag.ruby_code.upcase.reverse }
61+
assert_equal 'foo RAB baz', result
6062
result = ErbParser.parse str, :transform => lambda { |tag| tag.ruby_code.upcase.reverse }
6163
assert_equal 'foo RAB baz', result
6264
end

0 commit comments

Comments
 (0)