guerilla_patch 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/guerilla_patch.gemspec +31 -0
- data/lib/guerilla_patch.rb +25 -0
- data/lib/guerilla_patch/version.rb +3 -0
- data/spec/guerilla_patch_spec.rb +36 -0
- metadata +110 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jimmy Cuadra
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# GuerillaPatch
|
2
|
+
|
3
|
+
**GuerillaPatch** is a Ruby gem to ease the transition to Ruby 2.0 when redefining behavior in existing objects (monkey patching). Ruby 2.0 introduces a new feature called *refinements* which allows you to create monkey patches that only exist in a scope where the refinement is explicitly included, preventing your changes from affected code that has not opted in.
|
4
|
+
|
5
|
+
Using GuerillaPatch, you can use a common interface for writing a monkey patch that will use refinements if available, working seemlessly for both Ruby 1.9 and Ruby 2.0.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'guerilla_patch'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself with:
|
18
|
+
|
19
|
+
$ gem install guerilla_patch
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To create a new monkey patch, use `GuerillaPatch.patch`. The first argument is the object you are patching, and the second argument is the name to use for the refinement if run under Ruby 2.0. Pass a block with the methods you want to add to the object, or any other modifications you want to make.
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
GuerillaPatch.patch(Fixnum, "TimeExtensions") do
|
27
|
+
def minutes
|
28
|
+
self * 60
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Then, in Ruby 1.9:
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
2.minutes # => 120
|
37
|
+
```
|
38
|
+
|
39
|
+
And in Ruby 2.0:
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
class MyApp
|
43
|
+
using TimeExtensions
|
44
|
+
|
45
|
+
def minutes_to_seconds(m)
|
46
|
+
m.minutes
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
MyApp.new.minutes_to_seconds(2) # => 120
|
51
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'guerilla_patch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "guerilla_patch"
|
8
|
+
gem.version = GuerillaPatch::VERSION
|
9
|
+
gem.authors = ["Jimmy Cuadra"]
|
10
|
+
gem.email = ["jimmy@jimmycuadra.com"]
|
11
|
+
gem.description = %q{Monkey patch objects using Ruby 2.0 refinements, if available.}
|
12
|
+
gem.summary = <<-SUMMARY
|
13
|
+
GuerillaPatch is a Ruby gem to ease the transition to Ruby 2.0 when redefining
|
14
|
+
behavior in existing objects (monkey patching). Ruby 2.0 introduces a new
|
15
|
+
feature called refinements which allows you to create monkey patches that
|
16
|
+
only exist in a scope where the refinement is explicitly included, preventing
|
17
|
+
your changes from affected code that has not opted in. Using GuerillaPatch,
|
18
|
+
you can use a common interface for writing a monkey patch that will use
|
19
|
+
refinements if available, working seemlessly for both Ruby 1.9 and Ruby 2.0.
|
20
|
+
SUMMARY
|
21
|
+
gem.homepage = "https://github.com/jimmycuadra/guerilla_patch"
|
22
|
+
|
23
|
+
gem.files = `git ls-files`.split($/)
|
24
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
25
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
26
|
+
gem.require_paths = ["lib"]
|
27
|
+
|
28
|
+
gem.add_development_dependency("rspec")
|
29
|
+
gem.add_development_dependency("pry")
|
30
|
+
gem.add_development_dependency("pry-nav")
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "guerilla_patch/version"
|
2
|
+
|
3
|
+
module GuerillaPatch
|
4
|
+
class << self
|
5
|
+
def patch(obj, name, &block)
|
6
|
+
if Object.respond_to?(:refine, true)
|
7
|
+
refine_obj(obj, name, &block)
|
8
|
+
else
|
9
|
+
obj.class_exec(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
obj
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def refine_obj(obj, name, &block)
|
18
|
+
m = Module.new do
|
19
|
+
refine(obj, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
Object.const_set(name, m)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "pry"
|
2
|
+
require "guerilla_patch"
|
3
|
+
|
4
|
+
describe GuerillaPatch do
|
5
|
+
describe ".patch" do
|
6
|
+
before do
|
7
|
+
described_class.patch(Fixnum, "TimeExtensions") do
|
8
|
+
def minutes
|
9
|
+
self * 60
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if Object.respond_to?(:refine, true)
|
15
|
+
it "does not permanently modify the object" do
|
16
|
+
expect { 2.minutes }.to raise_error(NoMethodError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "affects classes using the refinement" do
|
20
|
+
c = Class.new do
|
21
|
+
using TimeExtensions
|
22
|
+
|
23
|
+
def test_refinement
|
24
|
+
2.minutes
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
expect(c.new.test_refinement).to eql(120)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
it "permanently modifies the object" do
|
32
|
+
expect(2.minutes).to eql(120)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guerilla_patch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jimmy Cuadra
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry-nav
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Monkey patch objects using Ruby 2.0 refinements, if available.
|
63
|
+
email:
|
64
|
+
- jimmy@jimmycuadra.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- guerilla_patch.gemspec
|
75
|
+
- lib/guerilla_patch.rb
|
76
|
+
- lib/guerilla_patch/version.rb
|
77
|
+
- spec/guerilla_patch_spec.rb
|
78
|
+
homepage: https://github.com/jimmycuadra/guerilla_patch
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.8.23
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: GuerillaPatch is a Ruby gem to ease the transition to Ruby 2.0 when redefining
|
102
|
+
behavior in existing objects (monkey patching). Ruby 2.0 introduces a new feature
|
103
|
+
called refinements which allows you to create monkey patches that only exist in
|
104
|
+
a scope where the refinement is explicitly included, preventing your changes from
|
105
|
+
affected code that has not opted in. Using GuerillaPatch, you can use a common interface
|
106
|
+
for writing a monkey patch that will use refinements if available, working seemlessly
|
107
|
+
for both Ruby 1.9 and Ruby 2.0.
|
108
|
+
test_files:
|
109
|
+
- spec/guerilla_patch_spec.rb
|
110
|
+
has_rdoc:
|