Module: MonkeyBars

Includes:
Patch, Validation
Defined in:
lib/monkey_bars.rb,
lib/monkey_bars/patch.rb,
lib/monkey_bars/errors.rb,
lib/monkey_bars/version.rb,
lib/monkey_bars/validation.rb

Defined Under Namespace

Modules: Patch, SuperSuper, Validation Classes: MismatchedClassMethodArityError, MismatchedInstanceMethodArityError, NewClassMethodAlreadyExistsError, NewConstantAlreadyExistsError, NewInstanceMethodAlreadyExistsError, NoPatchableClassMethodFoundError, NoPatchableInstanceMethodFoundError, NoPatchableMonkeyFoundError, NoPatchableVersionCheckError, NoPatchableVersionFoundError, PatchAlreadyPerformedError, PatchConstantNotFoundError, PatchableClassMethodIsNotPrivateError, PatchableClassMethodIsNotProtectedError, PatchableClassMethodIsPrivateError, PatchableClassMethodIsProtectedError, PatchableInstanceMethodIsNotPrivateError, PatchableInstanceMethodIsNotProtectedError, PatchableInstanceMethodIsPrivateError, PatchableInstanceMethodIsProtectedError

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(monkey_patcher) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/monkey_bars.rb', line 12

def self.extended(monkey_patcher)
  monkey_patcher.instance_variable_set(:@monkey_patcher_name, monkey_patcher.name)
  monkey_patcher.instance_variable_set(:@patch_class_methods, [])
  monkey_patcher.instance_variable_set(:@new_class_methods, [])
  monkey_patcher.instance_variable_set(:@patch_instance_methods, [])
  monkey_patcher.instance_variable_set(:@new_instance_methods, [])
  monkey_patcher.instance_variable_set(:@patch_constants, [])
  monkey_patcher.instance_variable_set(:@new_constants, [])
end

Instance Method Details

#new_class_methods(&block) ⇒ Object



79
80
81
# File 'lib/monkey_bars.rb', line 79

def new_class_methods(&block)
  @new_class_methods << block
end

#new_constants(&block) ⇒ Object



99
100
101
# File 'lib/monkey_bars.rb', line 99

def new_constants(&block)
  @new_constants << block
end

#new_instance_methods(&block) ⇒ Object



91
92
93
# File 'lib/monkey_bars.rb', line 91

def new_instance_methods(&block)
  @new_instance_methods << block
end

#patch(monkey, version:, version_check:, &block) ⇒ Object Also known as: 🐵



22
23
24
25
26
27
28
29
30
# File 'lib/monkey_bars.rb', line 22

def patch(monkey, version:, version_check:, &block)
  prepare_for_patching(
    monkey,
    version: version,
    version_check: version_check,
    patch_immediately: true,
    &block
  )
end

#patch!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/monkey_bars.rb', line 43

def patch!
  if @patch_performed
    raise(PatchAlreadyPerformedError.new(@monkey_patcher_name))
  end

  if @patch_class_methods.empty? &&
      @new_class_methods.empty? &&
      @patch_instance_methods.empty? &&
      @new_instance_methods.empty? &&
      @patch_constants.empty? &&
      @new_constants.empty?
    warn "[#{self}] No methods or constants defined. Maybe you're not calling `#patch!` last?"
  end

  ensure_monkey_exists
  ensure_patchable_version
  ensure_patch_instance_methods_are_valid
  ensure_new_instance_methods_are_valid
  ensure_patch_class_methods_are_valid
  ensure_new_class_methods_are_valid
  ensure_patch_constants_are_valid
  ensure_new_constants_are_valid

  perform_patch

  @patch_performed = true
end

#patch_class_methods(ignore_arity_errors: false, include_super_super: false, &block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/monkey_bars.rb', line 71

def patch_class_methods(ignore_arity_errors: false, include_super_super: false, &block)
  @patch_class_methods << {
    block: block,
    ignore_arity_errors: ignore_arity_errors,
    include_super_super: include_super_super
  }
end

#patch_constants(&block) ⇒ Object



95
96
97
# File 'lib/monkey_bars.rb', line 95

def patch_constants(&block)
  @patch_constants << block
end

#patch_instance_methods(ignore_arity_errors: false, include_super_super: false, &block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/monkey_bars.rb', line 83

def patch_instance_methods(ignore_arity_errors: false, include_super_super: false, &block)
  @patch_instance_methods << {
    block: block,
    ignore_arity_errors: ignore_arity_errors,
    include_super_super: include_super_super
  }
end

#post_patch(&block) ⇒ Object



103
104
105
# File 'lib/monkey_bars.rb', line 103

def post_patch(&block)
  @post_patch_block = block
end

#prepare_for_patching(monkey, version:, version_check:, patch_immediately: false, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/monkey_bars.rb', line 33

def prepare_for_patching(monkey, version:, version_check:, patch_immediately: false, &block)
  @monkey = monkey
  @version = version
  @version_check_block = version_check

  yield if block_given?

  patch! if patch_immediately
end