Class: Graft::HookPoint
- Inherits:
-
Object
- Object
- Graft::HookPoint
- Defined in:
- lib/graft/hook_point.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_STRATEGY =
Module.respond_to?(:prepend) ? :prepend : :chain
Instance Attribute Summary collapse
-
#klass_name ⇒ Object
readonly
Returns the value of attribute klass_name.
-
#method_kind ⇒ Object
readonly
Returns the value of attribute method_kind.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Class Method Summary collapse
- .const_exist?(name) ⇒ Boolean
- .parse(hook_point) ⇒ Object
- .resolve_const(name) ⇒ Object
- .resolve_module(name) ⇒ Object
- .strategy_module(strategy) ⇒ Object
Instance Method Summary collapse
-
#disable(key) ⇒ Object
rubocop:disable Lint/UselessMethodDefinition.
-
#disabled?(key) ⇒ Boolean
rubocop:disable Lint/UselessMethodDefinition.
-
#enable(key) ⇒ Object
rubocop:disable Lint/UselessMethodDefinition.
- #exist? ⇒ Boolean
-
#initialize(hook_point, strategy = DEFAULT_STRATEGY) ⇒ HookPoint
constructor
A new instance of HookPoint.
- #install(key, &block) ⇒ Object
-
#installed?(key) ⇒ Boolean
rubocop:disable Lint/UselessMethodDefinition.
- #instance_method? ⇒ Boolean
- #klass ⇒ Object
- #klass_method? ⇒ Boolean
- #private_method? ⇒ Boolean
- #protected_method? ⇒ Boolean
- #to_s ⇒ Object
- #uninstall(key) ⇒ Object
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_name ⇒ Object (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_kind ⇒ Object (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_name ⇒ Object (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
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
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
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
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
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
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
130 131 132 |
# File 'lib/graft/hook_point.rb', line 130 def installed?(key) # rubocop:disable Lint/UselessMethodDefinition super end |
#instance_method? ⇒ Boolean
106 107 108 |
# File 'lib/graft/hook_point.rb', line 106 def instance_method? @method_kind == :instance_method end |
#klass ⇒ Object
98 99 100 |
# File 'lib/graft/hook_point.rb', line 98 def klass HookPoint.resolve_module(@klass_name) end |
#klass_method? ⇒ Boolean
102 103 104 |
# File 'lib/graft/hook_point.rb', line 102 def klass_method? @method_kind == :klass_method end |
#private_method? ⇒ 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
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_s ⇒ Object
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 |