zimbatm-monkeypatch 0.1.1 → 0.1.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.
- data/README.rdoc +1 -1
- data/lib/monkeypatch.rb +19 -5
- data/task/gem.rake +1 -1
- data/test/test_monkeypatch.rb +16 -0
- metadata +1 -1
data/README.rdoc
CHANGED
data/lib/monkeypatch.rb
CHANGED
@@ -7,7 +7,7 @@ Once you have a patch, look at Patch and it's children to see how to use it.
|
|
7
7
|
=end
|
8
8
|
module MonkeyPatch
|
9
9
|
# MonkeyPatch's version as a string
|
10
|
-
VERSION = '0.1.
|
10
|
+
VERSION = '0.1.2'
|
11
11
|
# May be raised on check_conflicts
|
12
12
|
class ConflictError < StandardError; end
|
13
13
|
|
@@ -56,6 +56,10 @@ module MonkeyPatch
|
|
56
56
|
def initialize(&patch_def) #:nodoc:
|
57
57
|
raise ArgumentError, "patch_def not given" unless block_given?
|
58
58
|
@patch_def = patch_def
|
59
|
+
@conditions = []
|
60
|
+
add_condition("Patch already applied") do |klass|
|
61
|
+
!(klass.respond_to?(:applied_patches) && klass.applied_patches.include?(self))
|
62
|
+
end
|
59
63
|
end
|
60
64
|
|
61
65
|
# Combine patches together. Produces a PatchSet instance.
|
@@ -86,6 +90,14 @@ module MonkeyPatch
|
|
86
90
|
patch_class(meta)
|
87
91
|
end
|
88
92
|
|
93
|
+
# Add a condition for patch application, like library version
|
94
|
+
#
|
95
|
+
# If condition is not matched, +msg+ is
|
96
|
+
def add_condition(msg, &block)
|
97
|
+
raise ArgumentError, "block is missing" unless block_given?
|
98
|
+
@conditions.push [block, msg]
|
99
|
+
end
|
100
|
+
|
89
101
|
protected
|
90
102
|
|
91
103
|
# Condition are checks that raise nothing
|
@@ -94,11 +106,13 @@ module MonkeyPatch
|
|
94
106
|
#
|
95
107
|
# Returns true if all conditions are matched
|
96
108
|
def check_conditions(klass) #:nodoc:
|
97
|
-
|
98
|
-
|
99
|
-
|
109
|
+
!@conditions.find do |tuple|
|
110
|
+
if tuple[0].call(klass); false
|
111
|
+
else
|
112
|
+
log tuple[1]
|
113
|
+
true
|
114
|
+
end
|
100
115
|
end
|
101
|
-
true
|
102
116
|
end
|
103
117
|
|
104
118
|
# Re-implement in childs. Make sure super is called
|
data/task/gem.rake
CHANGED
data/test/test_monkeypatch.rb
CHANGED
@@ -135,5 +135,21 @@ class TestMonkeypatch < Test::Unit::TestCase
|
|
135
135
|
assert_equal("exists", @c.new.new_method )
|
136
136
|
assert_equal("replaced", @c.new.existing_method )
|
137
137
|
end
|
138
|
+
|
139
|
+
def test_add_failing_condition
|
140
|
+
@p_add.add_condition("Failing condition") do |klass|
|
141
|
+
false
|
142
|
+
end
|
143
|
+
assert !@p_add.patch_class(@c)
|
144
|
+
assert !@c.respond_to?(:applied_patches)
|
145
|
+
assert_equal(["Failing condition"], @logs)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_working_condition
|
149
|
+
@p_add.add_condition("Working condition") do true end
|
150
|
+
assert @p_add.patch_class(@c)
|
151
|
+
assert @c.respond_to?(:applied_patches)
|
152
|
+
assert @logs.empty?
|
153
|
+
end
|
138
154
|
|
139
155
|
end
|