Module: AddedMethods
- Extended by:
- AddedMethods
- Included in:
- AddedMethods
- Defined in:
- lib/added_methods.rb,
lib/added_methods/version.rb,
lib/added_methods/added_method.rb
Overview
–
#
A component of added_methods, the added method watcher. #
#
Copyright © 2007-2013 Jens Wille #
#
Authors: #
Jens Wille <[email protected]> #
#
added_methods is free software; you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as published by # the Free Software Foundation; either version 3 of the License, or (at your # option) any later version. #
#
added_methods is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public # License for more details. #
#
You should have received a copy of the GNU Affero General Public License # along with added_methods. If not, see <www.gnu.org/licenses/>. #
#
++
Defined Under Namespace
Modules: Version Classes: AddedMethod
Constant Summary collapse
Instance Method Summary collapse
- #all_methods ⇒ Object
- #callback(*args, &inner_block) ⇒ Object
- #callbacks ⇒ Object
- #define_callback(name, regexp = //, klasses = [], &outer_block) ⇒ Object
- #find(conditions = {}) ⇒ Object
- #find_by_class(*classes) ⇒ Object
- #find_by_name(*names) ⇒ Object
- #find_one_by_name_or_class(name_or_class, conditions = {}) ⇒ Object (also: #[])
- #init(regexp = nil, klasses = [], &block) ⇒ Object
- #install_callbacks(bases = [Object, Class, Module, Kernel]) ⇒ Object
- #remove_callback(name) ⇒ Object
- #replace_callback(name, regexp = nil, klasses = [], &outer_block) ⇒ Object
Instance Method Details
#all_methods ⇒ Object
113 114 115 116 |
# File 'lib/added_methods.rb', line 113 def all_methods init_all_methods ALL_METHODS end |
#callback(*args, &inner_block) ⇒ Object
58 59 60 61 |
# File 'lib/added_methods.rb', line 58 def callback(*args, &inner_block) callback_args = [identify_added_method(*args << caller), caller, inner_block] callbacks.each { |name, callback| callback[*callback_args] } end |
#callbacks ⇒ Object
53 54 55 56 |
# File 'lib/added_methods.rb', line 53 def callbacks init_callbacks CALLBACKS end |
#define_callback(name, regexp = //, klasses = [], &outer_block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/added_methods.rb', line 63 def define_callback(name, regexp = //, klasses = [], &outer_block) raise TypeError, "wrong argument type #{name.class} (expected Symbol)" unless name.is_a?(Symbol) raise "callback with name #{name} already exists" if callbacks.assoc(name) raise TypeError, "wrong argument type #{regexp.class} (expected Regexp)" unless regexp.is_a?(Regexp) raise TypeError, "wrong argument type #{klasses.class} (expected container object)" unless klasses.respond_to?(:empty?) && klasses.respond_to?(:include?) callbacks << [name, lambda { |am, callstack, inner_block| method, klass = am.name, am.klass return if %w[method_added singleton_method_added].include?(method) return unless klasses.empty? || klasses.include?(klass.to_s) return unless method =~ regexp if outer_block || inner_block outer_block[am] if outer_block inner_block[am] if inner_block else msg = "[#{am.base}] Adding #{'singleton ' if am.singleton?}method #{klass}##{method}" msg << if irb?(callstack) " in (irb:#{IRB.conf[:MAIN_CONTEXT].instance_variable_get(:@line_no)})" else " at #{where(callstack)}" end puts msg end }] end |
#find(conditions = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/added_methods.rb', line 118 def find(conditions = {}) conditions = conditions.dup class_condition = conditions.delete(:class) file_condition = conditions.delete(:file) results = [] all_methods.each { |klass, files| if class_condition next unless class_condition.is_a?(Array) ? class_condition.include?(klass) : klass == class_condition end files.each { |file, entries| if file_condition next unless file_condition.is_a?(Regexp) ? file =~ file_condition : file == file_condition end entries.each { |am| results << am if conditions.all? { |key, value| case value when Array, Range then value.include?(am[key]) when Regexp then value =~ am[key].to_s else value == am[key] end } } } } results end |
#find_by_class(*classes) ⇒ Object
151 152 153 154 |
# File 'lib/added_methods.rb', line 151 def find_by_class(*classes) conditions = classes.last.is_a?(Hash) ? classes.pop : {} find(conditions.merge(:class => classes)) end |
#find_by_name(*names) ⇒ Object
156 157 158 159 |
# File 'lib/added_methods.rb', line 156 def find_by_name(*names) conditions = names.last.is_a?(Hash) ? names.pop : {} find(conditions.merge(:name => names.map { |m| m.to_s })) end |
#find_one_by_name_or_class(name_or_class, conditions = {}) ⇒ Object Also known as: []
161 162 163 164 165 166 |
# File 'lib/added_methods.rb', line 161 def find_one_by_name_or_class(name_or_class, conditions = {}) (name_or_class.is_a?(Class) ? find_by_class(name_or_class) : find_by_name(name_or_class) ).last end |
#init(regexp = nil, klasses = [], &block) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/added_methods.rb', line 45 def init(regexp = nil, klasses = [], &block) init_script_lines patch_readline_history define_callback(:__init, regexp, klasses, &block) if regexp install_callbacks end |
#install_callbacks(bases = [Object, Class, Module, Kernel]) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/added_methods.rb', line 104 def install_callbacks(bases = [Object, Class, Module, Kernel]) bases.each { |base| [base, singleton_class(base)].each { |b| b.send(:define_method, :method_added) { |id| AddedMethods.callback(b, self, id, false) } b.send(:define_method, :singleton_method_added) { |id| AddedMethods.callback(b, self, id, true) } } } end |
#remove_callback(name) ⇒ Object
95 96 97 |
# File 'lib/added_methods.rb', line 95 def remove_callback(name) callbacks.delete_if { |n, _| n == name } end |
#replace_callback(name, regexp = nil, klasses = [], &outer_block) ⇒ Object
99 100 101 102 |
# File 'lib/added_methods.rb', line 99 def replace_callback(name, regexp = nil, klasses = [], &outer_block) remove_callback(name) define_callback(name, regexp, klasses, &outer_block) end |