Class: Graft::HookPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/graft/hook_point.rb

Defined Under Namespace

Modules: Chain, Prepend

Constant Summary collapse

DEFAULT_STRATEGY =
Module.respond_to?(:prepend) ? :prepend : :chain

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_point, strategy = DEFAULT_STRATEGY) ⇒ HookPoint

Returns a new instance of HookPoint.



67
68
69
70
71
72
# File 'lib/graft/hook_point.rb', line 67

def initialize(hook_point, strategy = DEFAULT_STRATEGY)
  @klass_name, @method_kind, @method_name = HookPoint.parse(hook_point)
  @strategy = strategy

  extend HookPoint.strategy_module(strategy)
end

Instance Attribute Details

#klass_nameObject (readonly)

Returns the value of attribute klass_name.



65
66
67
# File 'lib/graft/hook_point.rb', line 65

def klass_name
  @klass_name
end

#method_kindObject (readonly)

Returns the value of attribute method_kind.



65
66
67
# File 'lib/graft/hook_point.rb', line 65

def method_kind
  @method_kind
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



65
66
67
# File 'lib/graft/hook_point.rb', line 65

def method_name
  @method_name
end

Class Method Details

.const_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/graft/hook_point.rb', line 35

def const_exist?(name)
  resolve_const(name) && true
rescue NameError, ArgumentError
  false
end

.parse(hook_point) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/graft/hook_point.rb', line 24

def parse(hook_point)
  klass_name, separator, method_name = hook_point.split(/(\#|\.)/, 2)

  raise ArgumentError, hook_point if klass_name.nil? || separator.nil? || method_name.nil?
  raise ArgumentError, hook_point unless [".", "#"].include?(separator)

  method_kind = (separator == ".") ? :klass_method : :instance_method

  [klass_name.to_sym, method_kind, method_name.to_sym]
end

.resolve_const(name) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/graft/hook_point.rb', line 41

def resolve_const(name)
  raise ArgumentError, "const not found: #{name}" if name.nil? || name.empty?

  name.to_s.split("::").inject(Object) { |a, e| a.const_get(e, false) }
end

.resolve_module(name) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/graft/hook_point.rb', line 47

def resolve_module(name)
  const = resolve_const(name)

  raise ArgumentError, "not a Module: #{name}" unless const.is_a?(Module)

  const
end

.strategy_module(strategy) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/graft/hook_point.rb', line 55

def strategy_module(strategy)
  case strategy
  when :prepend then Prepend
  when :chain then Chain
  else
    raise HookPointError, "unknown strategy: #{strategy.inspect}"
  end
end

Instance Method Details

#disable(key) ⇒ Object

rubocop:disable Lint/UselessMethodDefinition



154
155
156
# File 'lib/graft/hook_point.rb', line 154

def disable(key) # rubocop:disable Lint/UselessMethodDefinition
  super
end

#disabled?(key) ⇒ Boolean

rubocop:disable Lint/UselessMethodDefinition

Returns:

  • (Boolean)


158
159
160
# File 'lib/graft/hook_point.rb', line 158

def disabled?(key) # rubocop:disable Lint/UselessMethodDefinition
  super
end

#enable(key) ⇒ Object

rubocop:disable Lint/UselessMethodDefinition



150
151
152
# File 'lib/graft/hook_point.rb', line 150

def enable(key) # rubocop:disable Lint/UselessMethodDefinition
  super
end

#exist?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/graft/hook_point.rb', line 78

def exist?
  return false unless HookPoint.const_exist?(@klass_name)

  if klass_method?
    (
      klass.singleton_class.public_instance_methods(false) +
      klass.singleton_class.protected_instance_methods(false) +
      klass.singleton_class.private_instance_methods(false)
    ).include?(@method_name)
  elsif instance_method?
    (
      klass.public_instance_methods(false) +
      klass.protected_instance_methods(false) +
      klass.private_instance_methods(false)
    ).include?(@method_name)
  else
    raise HookPointError, "#{self} unknown hook point kind"
  end
end

#install(key, &block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/graft/hook_point.rb', line 134

def install(key, &block)
  return unless exist?

  return if installed?(key)

  super
end

#installed?(key) ⇒ Boolean

rubocop:disable Lint/UselessMethodDefinition

Returns:

  • (Boolean)


130
131
132
# File 'lib/graft/hook_point.rb', line 130

def installed?(key) # rubocop:disable Lint/UselessMethodDefinition
  super
end

#instance_method?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/graft/hook_point.rb', line 106

def instance_method?
  @method_kind == :instance_method
end

#klassObject



98
99
100
# File 'lib/graft/hook_point.rb', line 98

def klass
  HookPoint.resolve_module(@klass_name)
end

#klass_method?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/graft/hook_point.rb', line 102

def klass_method?
  @method_kind == :klass_method
end

#private_method?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
# File 'lib/graft/hook_point.rb', line 110

def private_method?
  if klass_method?
    klass.private_methods.include?(@method_name)
  elsif instance_method?
    klass.private_instance_methods.include?(@method_name)
  else
    raise HookPointError, "#{self} unknown hook point kind"
  end
end

#protected_method?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/graft/hook_point.rb', line 120

def protected_method?
  if klass_method?
    klass.protected_methods.include?(@method_name)
  elsif instance_method?
    klass.protected_instance_methods.include?(@method_name)
  else
    raise HookPointError, "#{self} unknown hook point kind"
  end
end

#to_sObject



74
75
76
# File 'lib/graft/hook_point.rb', line 74

def to_s
  @to_s ||= "#{@klass_name}#{(@method_kind == :instance_method) ? "#" : "."}#{@method_name}"
end

#uninstall(key) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/graft/hook_point.rb', line 142

def uninstall(key)
  return unless exist?

  return unless installed?(key)

  super
end