-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jdee/patch-class
Patch class
- Loading branch information
Showing
6 changed files
with
211 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require "pattern_patch/core_ext" | ||
require "pattern_patch/patch" | ||
require "pattern_patch/utilities" | ||
require "pattern_patch/version" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require "active_support/core_ext/hash" | ||
require "yaml" | ||
|
||
module PatternPatch | ||
class Patch | ||
attr_accessor :regexp | ||
attr_accessor :text | ||
attr_accessor :mode | ||
attr_accessor :global | ||
|
||
class << self | ||
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 | ||
end | ||
|
||
def initialize(options = {}) | ||
@regexp = options[:regexp] | ||
@text = options[:text] | ||
@mode = options[:mode] || :append | ||
@global = options[:global].nil? ? false : options[:global] | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
def inspect | ||
"#<PatternPatch::Patch regexp=#{regexp.inspect} text=#{text.inspect} mode=#{mode.inspect} global=#{global.inspect}>" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module PatternPatch | ||
VERSION = "0.1.2" | ||
VERSION = "0.2.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
describe PatternPatch::Patch do | ||
describe 'initialization' do | ||
it 'initializes parameters from options' do | ||
patch = PatternPatch::Patch.new regexp: //, text: '', mode: :prepend, global: true | ||
expect(patch.regexp).to eq(//) | ||
expect(patch.text).to eq '' | ||
expect(patch.mode).to eq :prepend | ||
expect(patch.global).to be true | ||
end | ||
|
||
it 'initializes to default values when no options passed' do | ||
patch = PatternPatch::Patch.new | ||
expect(patch.regexp).to be_nil | ||
expect(patch.text).to be_nil | ||
expect(patch.mode).to eq :append | ||
expect(patch.global).to be false | ||
end | ||
end | ||
|
||
describe '#inspect' do | ||
it 'includes the value of each field' do | ||
text = PatternPatch::Patch.new.inspect | ||
expect(text).to match(/regexp=/) | ||
expect(text).to match(/text=/) | ||
expect(text).to match(/mode=/) | ||
expect(text).to match(/global=/) | ||
end | ||
end | ||
|
||
describe '#apply' do | ||
it 'passes field values to the Utilities#apply_patch method' do | ||
patch = PatternPatch::Patch.new regexp: /x/, text: 'y' | ||
expect(File).to receive(:read).with('file.txt') { 'x' } | ||
|
||
expect(PatternPatch::Utilities).to receive(:apply_patch).with( | ||
'x', | ||
patch.regexp, | ||
patch.text, | ||
patch.global, | ||
patch.mode, 0 | ||
) { 'xy' } | ||
|
||
expect(File).to receive(:write).with('file.txt', 'xy') | ||
|
||
patch.apply 'file.txt' | ||
end | ||
|
||
it 'passes the offset option if present' do | ||
patch = PatternPatch::Patch.new regexp: /x/, text: 'y' | ||
expect(File).to receive(:read).with('file.txt') { 'x' } | ||
|
||
expect(PatternPatch::Utilities).to receive(:apply_patch).with( | ||
'x', | ||
patch.regexp, | ||
patch.text, | ||
patch.global, | ||
patch.mode, | ||
1 | ||
) { 'x' } | ||
|
||
expect(File).to receive(:write).with('file.txt', 'x') | ||
|
||
patch.apply 'file.txt', offset: 1 | ||
end | ||
end | ||
|
||
describe '#revert' do | ||
it 'passes field values to the Utilities#revert_patch method' do | ||
patch = PatternPatch::Patch.new regexp: /x/, text: 'y' | ||
expect(File).to receive(:read).with('file.txt') { 'xy' } | ||
|
||
expect(PatternPatch::Utilities).to receive(:revert_patch).with( | ||
'xy', | ||
patch.regexp, | ||
patch.text, | ||
patch.global, | ||
patch.mode, 0 | ||
) { 'x' } | ||
|
||
expect(File).to receive(:write).with('file.txt', 'x') | ||
|
||
patch.revert 'file.txt' | ||
end | ||
|
||
it 'passes the offset option if present' do | ||
patch = PatternPatch::Patch.new regexp: /x/, text: 'y' | ||
expect(File).to receive(:read).with('file.txt') { 'x' } | ||
|
||
expect(PatternPatch::Utilities).to receive(:revert_patch).with( | ||
'x', | ||
patch.regexp, | ||
patch.text, | ||
patch.global, | ||
patch.mode, | ||
1 | ||
) { 'x' } | ||
|
||
expect(File).to receive(:write).with('file.txt', 'x') | ||
|
||
patch.revert 'file.txt', offset: 1 | ||
end | ||
end | ||
|
||
describe '::from_yaml' do | ||
it 'loads a patch from a YAML file' do | ||
expect(YAML).to receive(:load_file).with('file.yml') { { regexp: 'x', text: 'y', mode: 'prepend', global: true } } | ||
patch = PatternPatch::Patch.from_yaml 'file.yml' | ||
expect(patch.regexp).to eq(/x/) | ||
expect(patch.text).to eq 'y' | ||
expect(patch.mode).to eq :prepend | ||
expect(patch.global).to be true | ||
end | ||
end | ||
end |