Skip to content

Commit 2dea522

Browse files
authored
Merge pull request #10 from jdee/erb-improvements
ERb improvements
2 parents 7ae7352 + c8c30dd commit 2dea522

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ PatternPatch::Patch.new(
117117

118118
This is particularly useful with a `text_file` argument.
119119

120+
The `#apply` and `#revert` methods also accept `:safe_level` and `:trim_mode`
121+
options for use with ERb. These can be set at the global level using
122+
`PatternPatch.safe_level` and `PatternPatch.trim_mode`. The `#safe_level`
123+
and '#trim_mode' attributes in the `Methods` module are convenience methods
124+
to set and retrieve these global values.
125+
126+
```Ruby
127+
PatternPatch::Patch.new(
128+
regexp: /x/,
129+
text_file: "template.erb",
130+
mode: :replace
131+
).apply file_path, trim_mode: "<>"
132+
```
133+
120134
#### Regular expressions with modifiers in YAML
121135

122136
The `regexp` field in a YAML file may be specified with or without slashes

lib/pattern_patch.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "forwardable"
12
require "pattern_patch/core_ext"
23
require "pattern_patch/patch"
34
require "pattern_patch/utilities"
@@ -8,19 +9,37 @@
89
#
910
# @author Jimmy Dee (https://github.com/jdee)
1011
module PatternPatch
12+
extend Forwardable
13+
1114
# Generic exception class for PatternPatch exceptions
1215
class Error < RuntimeError; end
1316

1417
# Exception generated by patch method when patch_dir is not set properly
1518
class ConfigurationError < Error; end
1619

1720
module Methods
21+
extend Forwardable
22+
1823
# @!attribute patch_dir
1924
# Set this to conveniently load patches from a common folder with
2025
# the patch method.
2126
# @return [String] Path to a directory for use with patch
2227
attr_accessor :patch_dir
2328

29+
# @!attribute safe_level
30+
# Set the default safe level to use with ERb. This is the same as the value
31+
# of PatternPatch.safe_level.
32+
# @return [Object, nil] The current default safe level for ERb
33+
def_delegator "PatternPatch", :safe_level, :safe_level
34+
def_delegator "PatternPatch", :safe_level=, :safe_level=
35+
36+
# @!attribute trim_mode
37+
# Set the default trim mode to use with ERb. This is the same as the value
38+
# of PatternPatch.trim_mode.
39+
# @return [String, nil] The current default trim mode for ERb
40+
def_delegator "PatternPatch", :trim_mode, :trim_mode
41+
def_delegator "PatternPatch", :trim_mode=, :trim_mode=
42+
2443
# Loads a patch from the patch_dir
2544
# @param name [#to_s] Name of a patch to load from the patch_dir
2645
# @return [Patch] A patch loaded from the patch_dir
@@ -32,5 +51,17 @@ def patch(name)
3251
end
3352
end
3453

54+
class << self
55+
# @!attribute safe_level
56+
# The default safe level to use with ERb. Defaults to nil.
57+
# @return [Object, nil] The current default safe level for ERb
58+
attr_accessor :safe_level
59+
60+
# @!attribute trim_mode
61+
# The default trim mode to use with ERb. Defaults to nil.
62+
# @return [String, nil] The current default trim mode for ERb
63+
attr_accessor :trim_mode
64+
end
65+
3566
extend Methods
3667
end

lib/pattern_patch/patch.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,17 @@ def text_file=(path)
123123
# @param options [Hash] Options for applying the patch.
124124
# @option options [Binding] :binding (nil) A Binding object to use when rendering ERB
125125
# @option options [Integer] :offset (0) Offset in characters
126+
# @option options [Object, nil] :safe_level (PatternPatch.safe_level) A valid value for $SAFE for use with ERb
127+
# @option options [String] :trim_mode (PatternPatch.trim_mode) A valid ERb trim mode
126128
# @raise [ArgumentError] In case of invalid mode (other than :append, :prepend, :replace)
127129
def apply(files, options = {})
128130
offset = options[:offset] || 0
129131
files = [files] if files.kind_of? String
130132

131-
patch_text = ERB.new(text).result options[:binding]
133+
safe_level = options[:safe_level] || PatternPatch.safe_level
134+
trim_mode = options[:trim_mode] || PatternPatch.trim_mode
135+
136+
patch_text = ERB.new(text, safe_level, trim_mode).result options[:binding]
132137

133138
files.each do |path|
134139
modified = Utilities.apply_patch File.read(path),
@@ -150,12 +155,17 @@ def apply(files, options = {})
150155
# @param options [Hash] Options for applying the patch.
151156
# @option options [Binding] :binding (nil) A Binding object to use when rendering ERB
152157
# @option options [Integer] :offset (0) Offset in characters
158+
# @option options [Object, nil] :safe_level (PatternPatch.safe_level) A valid value for $SAFE for use with ERb
159+
# @option options [String] :trim_mode (PatternPatch.trim_mode) A valid ERb trim mode
153160
# @raise [ArgumentError] In case of invalid mode (other than :append or :prepend)
154161
def revert(files, options = {})
155162
offset = options[:offset] || 0
156163
files = [files] if files.kind_of? String
157164

158-
patch_text = ERB.new(text).result options[:binding]
165+
safe_level = options[:safe_level] || PatternPatch.safe_level
166+
trim_mode = options[:trim_mode] || PatternPatch.trim_mode
167+
168+
patch_text = ERB.new(text, safe_level, trim_mode).result options[:binding]
159169

160170
files.each do |path|
161171
modified = Utilities.revert_patch File.read(path),

spec/methods_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
include PatternPatch::Methods
2+
13
describe PatternPatch::Methods do
24
it 'has an attr_accessor :patch_dir' do
35
expect(PatternPatch).to respond_to(:patch_dir)
46
expect(PatternPatch).to respond_to(:patch_dir=)
57
end
68

9+
it 'has an attr_accessor :safe_level' do
10+
expect(PatternPatch).to respond_to(:safe_level)
11+
expect(PatternPatch).to respond_to(:safe_level=)
12+
end
13+
14+
it 'has an attr_accessor :trim_mode' do
15+
expect(PatternPatch).to respond_to(:trim_mode)
16+
expect(PatternPatch).to respond_to(:trim_mode=)
17+
end
18+
719
describe '::patch' do
820
it 'raises if patch_dir is nil' do
921
expect do
@@ -28,4 +40,24 @@
2840
expect(PatternPatch.patch(:foo)).to be_a PatternPatch::Patch
2941
end
3042
end
43+
44+
describe '#safe_level' do
45+
it 'delegates to the PatternPatch module' do
46+
PatternPatch.safe_level = 0
47+
expect(safe_level).to eq 0
48+
49+
self.safe_level = 1
50+
expect(PatternPatch.safe_level).to eq 1
51+
end
52+
end
53+
54+
describe '#trim_mode' do
55+
it 'delegates to the PatternPatch module' do
56+
PatternPatch.trim_mode = "-"
57+
expect(trim_mode).to eq "-"
58+
59+
self.trim_mode = "<>"
60+
expect(PatternPatch.trim_mode).to eq "<>"
61+
end
62+
end
3163
end

0 commit comments

Comments
 (0)