zimbatm-monkeypatch 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -39,6 +39,6 @@ Once you have a patch object, look at the MonkeyPatch::Patch definition to know
39
39
 
40
40
  ==== TODO
41
41
 
42
- * Add programmable patching conditions
42
+ * Rework the documentation
43
43
  * Add reason string
44
44
  * Add 'monkeywarn' that warns when a monkeypatch is applied
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.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
- if klass.respond_to?(:applied_patches) && klass.applied_patches.include?(self)
98
- log "WARN: Patch already applied"
99
- return false
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
@@ -23,7 +23,7 @@ task :gem => "gem:spec"
23
23
 
24
24
  namespace :gem do
25
25
 
26
- spec_name = "ruby-monkeypatch.gemspec"
26
+ spec_name = "monkeypatch.gemspec"
27
27
  desc "Updates the #{spec_name} file if VERSION has changed"
28
28
  task :spec do
29
29
  if !File.exist?(spec_name) ||
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zimbatm-monkeypatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zimbatm