Class: RubyJard::Reflection
- Inherits:
-
Object
- Object
- RubyJard::Reflection
- Defined in:
- lib/ruby_jard/reflection.rb
Overview
User classes may override basic Kernel methods, such as #inspect or #to_s, or even #is_a?. It’s not very wise to call those methods directly, as there maybe side effects. Therefore, this class is to extract unbound methods from Kernel module, and then call them in Object’s context.
Class Method Summary collapse
- .bind_call(owner, method_name, object, *args) ⇒ Object
- .call_class(object) ⇒ Object
- .call_const_defined?(object, const_name) ⇒ Boolean
- .call_const_get(object, const_name) ⇒ Object
- .call_inspect(object) ⇒ Object
- .call_instance_variable_get(object, variable) ⇒ Object
- .call_instance_variable_set(object, variable, value) ⇒ Object
- .call_instance_variables(object) ⇒ Object
- .call_is_a?(object, comparing_class) ⇒ Boolean
- .call_respond_to?(object, method_name) ⇒ Boolean
- .call_to_s(object) ⇒ Object
- .fetch_instance_method(object, method_name) ⇒ Object
- .fetch_method(object, method_name) ⇒ Object
- .instance_bind_call(owner, method_name, object, *args) ⇒ Object
Class Method Details
.bind_call(owner, method_name, object, *args) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ruby_jard/reflection.rb', line 68 def bind_call(owner, method_name, object, *args) @method_cache ||= {} @method_cache[owner] ||= {} @method_cache[owner][method_name] ||= fetch_method(owner, method_name) @method_cache[owner][method_name].bind(object).call(*args) end |
.call_class(object) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/ruby_jard/reflection.rb', line 12 def call_class(object) if call_is_a?(object, Module) bind_call(Kernel, :class, object) else instance_bind_call(Kernel, :class, object) end end |
.call_const_defined?(object, const_name) ⇒ Boolean
64 65 66 |
# File 'lib/ruby_jard/reflection.rb', line 64 def call_const_defined?(object, const_name) bind_call(Kernel, :const_defined?, object, const_name) end |
.call_const_get(object, const_name) ⇒ Object
60 61 62 |
# File 'lib/ruby_jard/reflection.rb', line 60 def call_const_get(object, const_name) bind_call(Kernel, :const_get, object, const_name) end |
.call_inspect(object) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/ruby_jard/reflection.rb', line 40 def call_inspect(object) if call_is_a?(object, Module) bind_call(Kernel, :inspect, object) else instance_bind_call(Kernel, :inspect, object) end end |
.call_instance_variable_get(object, variable) ⇒ Object
32 33 34 |
# File 'lib/ruby_jard/reflection.rb', line 32 def call_instance_variable_get(object, variable) bind_call(Kernel, :instance_variable_get, object, variable) end |
.call_instance_variable_set(object, variable, value) ⇒ Object
36 37 38 |
# File 'lib/ruby_jard/reflection.rb', line 36 def call_instance_variable_set(object, variable, value) bind_call(Kernel, :instance_variable_set, object, variable, value) end |
.call_instance_variables(object) ⇒ Object
28 29 30 |
# File 'lib/ruby_jard/reflection.rb', line 28 def call_instance_variables(object) bind_call(Kernel, :instance_variables, object) end |
.call_is_a?(object, comparing_class) ⇒ Boolean
56 57 58 |
# File 'lib/ruby_jard/reflection.rb', line 56 def call_is_a?(object, comparing_class) bind_call(Kernel, :is_a?, object, comparing_class) end |
.call_respond_to?(object, method_name) ⇒ Boolean
20 21 22 23 24 25 26 |
# File 'lib/ruby_jard/reflection.rb', line 20 def call_respond_to?(object, method_name) if call_is_a?(object, Module) bind_call(Kernel, :respond_to?, object, method_name) else instance_bind_call(Kernel, :respond_to?, object, method_name) end end |
.call_to_s(object) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ruby_jard/reflection.rb', line 48 def call_to_s(object) if call_is_a?(object, Module) bind_call(Kernel, :to_s, object) else instance_bind_call(Kernel, :to_s, object) end end |
.fetch_instance_method(object, method_name) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/ruby_jard/reflection.rb', line 89 def fetch_instance_method(object, method_name) @method_cache ||= {} @method_cache[::Kernel] ||= {} @method_cache[::Kernel][:instance_method] ||= ::Kernel.method(:instance_method).unbind @method_cache[::Kernel][:instance_method].bind(object).call(method_name) end |
.fetch_method(object, method_name) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/ruby_jard/reflection.rb', line 82 def fetch_method(object, method_name) @method_cache ||= {} @method_cache[::Kernel] ||= {} @method_cache[::Kernel][:method] ||= ::Kernel.method(:method).unbind @method_cache[::Kernel][:method].bind(object).call(method_name).unbind end |
.instance_bind_call(owner, method_name, object, *args) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/ruby_jard/reflection.rb', line 75 def instance_bind_call(owner, method_name, object, *args) @instance_method_cache ||= {} @instance_method_cache[owner] ||= {} @instance_method_cache[owner][method_name] ||= fetch_instance_method(owner, method_name) @instance_method_cache[owner][method_name].bind(object).call(*args) end |