pattern_patch 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45106665f7237be519a7a8ec49474ebe62305569e456c830275acfb447241ea0
4
- data.tar.gz: bfa912c8f32295c8b1744cf0468e15b2e3138ce8bf0caedcbea0fbc0e1e7b7a7
3
+ metadata.gz: 020a46e316bbe314edeee3920555b57699d37ca1668d10f1d3e7c7b68325c523
4
+ data.tar.gz: 8cd9d162362f0a168f3950cce7b3a67083638fb55f629c89fbbc36ba89d73c6a
5
5
  SHA512:
6
- metadata.gz: e022aec8c7b46d499f2c0a37af3c21cfe2ecf0bcb5859a0a48946d06a13a0bce897851925108c0f1f3bfe72df878f431ca3bfb5967c3c571f7c2798d3f953e2b
7
- data.tar.gz: 6cb41a5b13b0d2a8b9bdd1857959d7ab74150fe2f1d2e9f8536fcec6a16fcef89e726f1904573098a98ef989c6c257e3e6ee48cf14fd0914c8c0a32f8b2f75bd
6
+ metadata.gz: bb2bdeb2fd23ff22dcd79741e37e449f10b808174d6401b74fbae472f97a53d3589db604c272f7ed34f98799f7ee3382009059ef3f18a9879296e7c55b811314
7
+ data.tar.gz: e65d3f77114df163c67fd263f9471b320f6656541ab5b13173405887926755f1a8ae9dbce44030368f9974d8d337309ce0a7366abca636a4c82c3d8095f9647e
@@ -3,7 +3,7 @@ class String
3
3
  # data from matches. Modifies the receiver. The receiver
4
4
  # need not match the matches.regexp.
5
5
  #
6
- # :matches: A MatchData object returned by Regexp#match
6
+ # [matches] [MatchData] A MatchData object returned by Regexp#match
7
7
  def apply_matches!(matches)
8
8
  search_position = 0
9
9
  while (m = /\\(\d+)/.match(self, search_position))
@@ -19,7 +19,7 @@ class String
19
19
  # in self replaced by appropriate data from matches. The receiver
20
20
  # need not match the matches.regexp.
21
21
  #
22
- # :matches: A MatchData object returned by Regexp#match
22
+ # [matches] [MatchData] A MatchData object returned by Regexp#match
23
23
  def apply_matches(matches)
24
24
  string = clone
25
25
  string.apply_matches! matches
@@ -3,14 +3,37 @@ require "erb"
3
3
  require "yaml"
4
4
 
5
5
  module PatternPatch
6
+ # The PatternPatch::Patch class defines a patch as an operation that
7
+ # may be applied to any file. Often the operation may also be reverted.
6
8
  class Patch
9
+ # Regexp defining one or more matching regions in a file.
7
10
  attr_accessor :regexp
11
+
12
+ # String with text to use in the patch operation. May contain ERB.
8
13
  attr_accessor :text
14
+
15
+ # Symbol specifying the patch mode: :append (default), :prepend or :replace
9
16
  attr_accessor :mode
17
+
18
+ # Setting this to true will apply the patch to all matches in the file.
19
+ # By default (when false), the patch is only applied to the first match.
10
20
  attr_accessor :global
21
+
22
+ # If set to a file path, the contents of that file will be used to populate
23
+ # the text attribute. Setting this after construction modifies the text
24
+ # field.
11
25
  attr_reader :text_file
12
26
 
13
27
  class << self
28
+ # Load a Patch from a YAML file. The following special processing applies:
29
+ # The mode field is converted to a symbol. The text_file field will be
30
+ # interpreted relative to the YAML file. A Regexp will be constructed from
31
+ # the regexp field using Regexp.new unless it is a String containing a
32
+ # Regexp literal using slash delimiters, e.g. /x/i. This format may be
33
+ # used to specify a Regexp with modifiers in YAML. Raises if the file
34
+ # cannot be loaded.
35
+ #
36
+ # [patch] [String] Path to a YAML file containing a patch definition.
14
37
  def from_yaml(path)
15
38
  hash = YAML.load_file(path).symbolize_keys
16
39
 
@@ -47,6 +70,11 @@ module PatternPatch
47
70
  end
48
71
  end
49
72
 
73
+ # Construct a new Patch from the options. The following fields are mapped
74
+ # to the corresponding attributes: :regexp, :text, :text_file, :mode,
75
+ # :global. Raises ArgumentError if both :text and :text_file are specified.
76
+ #
77
+ # [options] [Hash] Parameters used to construct the Patch
50
78
  def initialize(options = {})
51
79
  raise ArgumentError, "text and text_file are mutually exclusive" if options[:text] && options[:text_file]
52
80
 
@@ -68,15 +96,18 @@ module PatternPatch
68
96
  @text = File.read path if path
69
97
  end
70
98
 
99
+ # Applies the patch to one or more files. ERB is processed in the text
100
+ # field, whether it comes from a text_file or not. Pass a Binding to
101
+ # ERB using the :binding option. Pass the :offset option to specify a
102
+ # starting offset, in characters, from the beginning of the file.
103
+ #
104
+ # [files] [Array or String] One or more file paths to which to apply the patch.
105
+ # [options] [Hash] Options for applying the patch.
71
106
  def apply(files, options = {})
72
107
  offset = options[:offset] || 0
73
108
  files = [files] if files.kind_of? String
74
109
 
75
- if options[:binding]
76
- patch_text = ERB.new(text).result(options[:binding])
77
- else
78
- patch_text = text
79
- end
110
+ patch_text = ERB.new(text).result options[:binding]
80
111
 
81
112
  files.each do |path|
82
113
  modified = Utilities.apply_patch File.read(path),
@@ -89,15 +120,18 @@ module PatternPatch
89
120
  end
90
121
  end
91
122
 
123
+ # Reverse the effect of a patch on one or more files. ERB is processed in the text
124
+ # field, whether it comes from a text_file or not. Pass a Binding to
125
+ # ERB using the :binding option. Pass the :offset option to specify a
126
+ # starting offset, in characters, from the beginning of the file.
127
+ #
128
+ # [files] [Array or String] One or more file paths to which to apply the patch.
129
+ # [options] [Hash] Options for applying the patch.
92
130
  def revert(files, options = {})
93
131
  offset = options[:offset] || 0
94
132
  files = [files] if files.kind_of? String
95
133
 
96
- if options[:binding]
97
- patch_text = ERB.new(text).result(options[:binding])
98
- else
99
- patch_text = text
100
- end
134
+ patch_text = ERB.new(text).result options[:binding]
101
135
 
102
136
  files.each do |path|
103
137
  modified = Utilities.revert_patch File.read(path),
@@ -1,3 +1,3 @@
1
1
  module PatternPatch
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pattern_patch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Dee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2017-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport