Class: PatternPatch::Patch
- Inherits:
-
Object
- Object
- PatternPatch::Patch
- Defined in:
- lib/pattern_patch/patch.rb
Instance Attribute Summary collapse
-
#global ⇒ Object
Returns the value of attribute global.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#regexp ⇒ Object
Returns the value of attribute regexp.
-
#text ⇒ Object
Returns the value of attribute text.
-
#text_file ⇒ Object
Returns the value of attribute text_file.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(files, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Patch
constructor
A new instance of Patch.
- #inspect ⇒ Object
- #revert(files, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Patch
Returns a new instance of Patch.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pattern_patch/patch.rb', line 34 def initialize( = {}) raise ArgumentError, "text and text_file are mutually exclusive" if [:text] && [:text_file] @regexp = [:regexp] @text_file = [:text_file] if @text_file @text = File.read @text_file else @text = [:text] end @mode = [:mode] || :append @global = [:global].nil? ? false : [:global] end |
Instance Attribute Details
#global ⇒ Object
Returns the value of attribute global.
9 10 11 |
# File 'lib/pattern_patch/patch.rb', line 9 def global @global end |
#mode ⇒ Object
Returns the value of attribute mode.
8 9 10 |
# File 'lib/pattern_patch/patch.rb', line 8 def mode @mode end |
#regexp ⇒ Object
Returns the value of attribute regexp.
6 7 8 |
# File 'lib/pattern_patch/patch.rb', line 6 def regexp @regexp end |
#text ⇒ Object
Returns the value of attribute text.
7 8 9 |
# File 'lib/pattern_patch/patch.rb', line 7 def text @text end |
#text_file ⇒ Object
Returns the value of attribute text_file.
10 11 12 |
# File 'lib/pattern_patch/patch.rb', line 10 def text_file @text_file end |
Class Method Details
.from_yaml(path) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pattern_patch/patch.rb', line 13 def from_yaml(path) hash = YAML.load_file(path).symbolize_keys # Adjust string fields from YAML if hash[:regexp].kind_of? String hash[:regexp] = /#{hash[:regexp]}/ end if hash[:mode].kind_of? String hash[:mode] = hash[:mode].to_sym end if hash[:text_file] hash[:text_file] = File. hash[:text_file], File.dirname(path) end new hash end |
Instance Method Details
#apply(files, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pattern_patch/patch.rb', line 55 def apply(files, = {}) offset = [:offset] || 0 files = [files] if files.kind_of? String files.each do |path| modified = Utilities.apply_patch File.read(path), regexp, text, global, mode, offset File.write path, modified end end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/pattern_patch/patch.rb', line 85 def inspect "#<PatternPatch::Patch regexp=#{regexp.inspect} text=#{text.inspect} mode=#{mode.inspect} global=#{global.inspect}>" end |
#revert(files, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pattern_patch/patch.rb', line 70 def revert(files, = {}) offset = [:offset] || 0 files = [files] if files.kind_of? String files.each do |path| modified = Utilities.revert_patch File.read(path), regexp, text, global, mode, offset File.write path, modified end end |