Class: PatternPatch::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern_patch/patch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Patch

Returns a new instance of Patch.



29
30
31
32
33
34
# File 'lib/pattern_patch/patch.rb', line 29

def initialize(options = {})
  @regexp = options[:regexp]
  @text = options[:text]
  @mode = options[:mode] || :append
  @global = options[:global].nil? ? false : options[:global]
end

Instance Attribute Details

#globalObject

Returns the value of attribute global.



9
10
11
# File 'lib/pattern_patch/patch.rb', line 9

def global
  @global
end

#modeObject

Returns the value of attribute mode.



8
9
10
# File 'lib/pattern_patch/patch.rb', line 8

def mode
  @mode
end

#regexpObject

Returns the value of attribute regexp.



6
7
8
# File 'lib/pattern_patch/patch.rb', line 6

def regexp
  @regexp
end

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/pattern_patch/patch.rb', line 7

def text
  @text
end

Class Method Details

.from_yaml(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pattern_patch/patch.rb', line 12

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

  new hash
end

Instance Method Details

#apply(files, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pattern_patch/patch.rb', line 36

def apply(files, options = {})
  offset = options[: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

#inspectObject



66
67
68
# File 'lib/pattern_patch/patch.rb', line 66

def inspect
  "#<PatternPatch::Patch regexp=#{regexp.inspect} text=#{text.inspect} mode=#{mode.inspect} global=#{global.inspect}>"
end

#revert(files, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pattern_patch/patch.rb', line 51

def revert(files, options = {})
  offset = options[: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