Class: MonkeyPatch::Patch

Inherits:
Object
  • Object
show all
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

MethodPatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&patch_def) ⇒ Patch

:nodoc:

Raises:

  • (ArgumentError)


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

#fromObject (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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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)
  meta = (class << obj; self end)
  patch_class(meta)
end

#to_aObject

Returns [self], used by #&



71
# File 'lib/monkeypatch.rb', line 71

def to_a; [self] end