File tree Expand file tree Collapse file tree 3 files changed +48
-2
lines changed
Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -62,3 +62,30 @@ Load a patch defined in YAML and apply it.
6262``` Ruby
6363PatternPatch ::Patch .from_yaml(" patch.yaml" ).apply " file.txt"
6464```
65+
66+ #### Define patch text in external files
67+
68+ Load the contents of a file to use for the insertion/substitution text:
69+
70+ ``` Ruby
71+ PatternPatch ::Patch .new (
72+ regexp: /\z / ,
73+ text_file: " text_to_insert_at_end.txt" ,
74+ mode: :append
75+ )
76+ ```
77+
78+ When loading from a YAML file, the ` text_file ` path is interpreted relative
79+ to the directory of the YAML file, e.g.:
80+
81+ ** patch.yaml:**
82+
83+ ``` YAML
84+ text_file : text_to_insert_at_end.txt
85+ ` ` `
86+
87+ ` ` ` Ruby
88+ PatternPatch::Patch.from_yaml("/path/to/patches/patch.yaml")
89+ ```
90+
91+ This will load the contents of ` /path/to/patches/text_to_insert_at_end.txt ` .
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ class Patch
77 attr_accessor :text
88 attr_accessor :mode
99 attr_accessor :global
10+ attr_reader :text_file
1011
1112 class << self
1213 def from_yaml ( path )
@@ -22,17 +23,35 @@ def from_yaml(path)
2223 hash [ :mode ] = hash [ :mode ] . to_sym
2324 end
2425
26+ if hash [ :text_file ]
27+ hash [ :text_file ] = File . expand_path hash [ :text_file ] , File . dirname ( path )
28+ end
29+
2530 new hash
2631 end
2732 end
2833
2934 def initialize ( options = { } )
35+ raise ArgumentError , "text and text_file are mutually exclusive" if options [ :text ] && options [ :text_file ]
36+
3037 @regexp = options [ :regexp ]
31- @text = options [ :text ]
38+ @text_file = options [ :text_file ]
39+
40+ if @text_file
41+ @text = File . read @text_file
42+ else
43+ @text = options [ :text ]
44+ end
45+
3246 @mode = options [ :mode ] || :append
3347 @global = options [ :global ] . nil? ? false : options [ :global ]
3448 end
3549
50+ def text_file = ( path )
51+ @text_file = path
52+ @text = File . read path if path
53+ end
54+
3655 def apply ( files , options = { } )
3756 offset = options [ :offset ] || 0
3857 files = [ files ] if files . kind_of? String
Original file line number Diff line number Diff line change 11module PatternPatch
2- VERSION = "0.2.1 "
2+ VERSION = "0.3.0 "
33end
You can’t perform that action at this time.
0 commit comments