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.

Raises:

  • (ArgumentError)


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

def initialize(options = {})
  raise ArgumentError, "text and text_file are mutually exclusive" if options[:text] && options[:text_file]

  @regexp = options[:regexp]
  @text_file = options[:text_file]

  if @text_file
    @text = File.read @text_file
  else
    @text = options[:text]
  end

  @mode = options[:mode] || :append
  @global = options[:global].nil? ? false : options[:global]
end

Instance Attribute Details

#globalObject

Returns the value of attribute global.



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

def global
  @global
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#regexpObject

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

#textObject

Returns the value of attribute text.



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

def text
  @text
end

#text_fileObject

Returns the value of attribute text_file.



11
12
13
# File 'lib/pattern_patch/patch.rb', line 11

def text_file
  @text_file
end

Class Method Details

.from_yaml(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pattern_patch/patch.rb', line 14

def from_yaml(path)
  hash = YAML.load_file(path).symbolize_keys

  # Adjust string fields from YAML

  if hash[:regexp].kind_of? String
    regexp_string = hash[:regexp]
    if (matches = %r{^/(.+)/([imx]*)$}.match regexp_string)
      flags = 0
      if matches[2] =~ /i/
        flags |= Regexp::IGNORECASE
      end
      if matches[2] =~ /x/
        flags |= Regexp::EXTENDED
      end
      if matches[2] =~ /m/
        flags |= Regexp::MULTILINE
      end
      hash[:regexp] = Regexp.new matches[1], flags
      puts "Regexp from YAML: #{hash[:regexp].inspect}"
    else
      hash[:regexp] = /#{regexp_string}/
    end
  end

  if hash[:mode].kind_of? String
    hash[:mode] = hash[:mode].to_sym
  end

  if hash[:text_file]
    hash[:text_file] = File.expand_path hash[:text_file], File.dirname(path)
  end

  new hash
end

Instance Method Details

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pattern_patch/patch.rb', line 72

def apply(files, options = {})
  offset = options[:offset] || 0
  files = [files] if files.kind_of? String

  if options[:binding]
    patch_text = ERB.new(text).result(options[:binding])
  else
    patch_text = text
  end

  files.each do |path|
    modified = Utilities.apply_patch File.read(path),
                                     regexp,
                                     patch_text,
                                     global,
                                     mode,
                                     offset
    File.write path, modified
  end
end

#inspectObject



114
115
116
# File 'lib/pattern_patch/patch.rb', line 114

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

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pattern_patch/patch.rb', line 93

def revert(files, options = {})
  offset = options[:offset] || 0
  files = [files] if files.kind_of? String

  if options[:binding]
    patch_text = ERB.new(text).result(options[:binding])
  else
    patch_text = text
  end

  files.each do |path|
    modified = Utilities.revert_patch File.read(path),
                                      regexp,
                                      patch_text,
                                      global,
                                      mode,
                                      offset
    File.write path, modified
  end
end