Class: MonkeyPatch::Patch
- Inherits:
-
Object
- Object
- MonkeyPatch::Patch
- Defined in:
- lib/monkeypatch.rb
Overview
Abstract definition of a patch.
You cannot create Patch instance yourself, they are spawned from MonkeyPatch class-methods.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
The callstack it was defined in.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Combine patches together.
-
#add_condition(msg, &block) ⇒ Object
Add a condition for patch application, like library version.
-
#initialize(&patch_def) ⇒ Patch
constructor
:nodoc:.
-
#patch_class(klass) ⇒ Object
Patches a class or module instance methods.
-
#patch_instance(obj) ⇒ Object
Patches the instance’s metaclass.
-
#to_a ⇒ Object
Returns [self], used by #&.
Constructor Details
#initialize(&patch_def) ⇒ Patch
:nodoc:
56 57 58 59 60 61 62 63 |
# File 'lib/monkeypatch.rb', line 56 def initialize(&patch_def) #:nodoc: raise ArgumentError, "patch_def not given" unless block_given? @patch_def = patch_def @conditions = [] add_condition("Patch already applied") do |klass| !(klass.respond_to?(:applied_patches) && klass.applied_patches.include?(self)) end end |
Instance Attribute Details
#from ⇒ Object (readonly)
The callstack it was defined in
54 55 56 |
# File 'lib/monkeypatch.rb', line 54 def from @from end |
Instance Method Details
#&(other) ⇒ Object
Combine patches together. Produces a PatchSet instance.
66 67 68 |
# File 'lib/monkeypatch.rb', line 66 def &(other) PatchSet.new([self]) & other end |
#add_condition(msg, &block) ⇒ Object
Add a condition for patch application, like library version
If condition is not matched, msg
is
96 97 98 99 |
# File 'lib/monkeypatch.rb', line 96 def add_condition(msg, &block) raise ArgumentError, "block is missing" unless block_given? @conditions.push [block, msg] end |
#patch_class(klass) ⇒ Object
Patches a class or module instance methods
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/monkeypatch.rb', line 74 def patch_class(klass) raise ArgumentError, "klass is not a Class" unless klass.kind_of?(Class) return false if !check_conditions(klass) check_conflicts!(klass) # Apply apply_patch(klass) return true end |
#patch_instance(obj) ⇒ Object
Patches the instance’s metaclass
88 89 90 91 |
# File 'lib/monkeypatch.rb', line 88 def patch_instance(obj) = (class << obj; self end) patch_class() end |
#to_a ⇒ Object
Returns [self], used by #&
71 |
# File 'lib/monkeypatch.rb', line 71 def to_a; [self] end |