Module: SuperCallbacks::ClassAndInstanceMethods
- Defined in:
- lib/super_callbacks.rb
Instance Method Summary collapse
- #after(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
- #after!(method_name, *remaining_args) ⇒ Object
- #before(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
- #before!(method_name, *remaining_args) ⇒ Object
Instance Method Details
#after(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/super_callbacks.rb', line 83 def after(method_name, callback_method_name = nil, = {}, &callback_proc) callback_method_name_or_proc = callback_proc || callback_method_name unless [Symbol, String, Proc].include? callback_method_name_or_proc.class raise ArgumentError, "Only `Symbol`, `String` or `Proc` allowed for `method_name`, but is #{callback_method_name_or_proc.class}" end invalid_option_keys = .keys - VALID_OPTION_KEYS unless invalid_option_keys.empty? raise ArgumentError, "Invalid `options` keys: #{invalid_option_keys}. Valid are only: #{VALID_OPTION_KEYS}" end if [:if] && ![Symbol, String, Proc].include?([:if].class) raise ArgumentError, "Only `Symbol`, `String` or `Proc` allowed for `options[:if]`, but is #{[:if].class}" end self.after_callbacks ||= {} self.after_callbacks[method_name.to_sym] ||= [] self.after_callbacks[method_name.to_sym] << [callback_method_name_or_proc, [:if]] _callbacks_prepended_module_instance = callbacks_prepended_module_instance # dont redefine, to save cpu cycles unless _callbacks_prepended_module_instance.method_defined? method_name _callbacks_prepended_module_instance.send(:define_method, method_name) do |*args| run_before_callbacks(method_name, *args) super_value = super(*args) run_after_callbacks(method_name, *args) super_value end end end |
#after!(method_name, *remaining_args) ⇒ Object
47 48 49 50 |
# File 'lib/super_callbacks.rb', line 47 def after!(method_name, *remaining_args) raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name before(method_name, *remaining_args) end |
#before(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/super_callbacks.rb', line 52 def before(method_name, callback_method_name = nil, = {}, &callback_proc) callback_method_name_or_proc = callback_proc || callback_method_name unless [Symbol, String, Proc].any? { |klass| callback_method_name_or_proc.is_a? klass } raise ArgumentError, "Only `Symbol`, `String` or `Proc` allowed for `method_name`, but is #{callback_method_name_or_proc.class}" end invalid_option_keys = .keys - VALID_OPTION_KEYS unless invalid_option_keys.empty? raise ArgumentError, "Invalid `options` keys: #{invalid_option_keys}. Valid are only: #{VALID_OPTION_KEYS}" end if [:if] && !([Symbol, String, Proc].any? { |klass| callback_method_name_or_proc.is_a? klass }) raise ArgumentError, "Only `Symbol`, `String` or `Proc` allowed for `options[:if]`, but is #{[:if].class}" end self.before_callbacks ||= {} self.before_callbacks[method_name.to_sym] ||= [] self.before_callbacks[method_name.to_sym] << [callback_method_name_or_proc, [:if]] _callbacks_prepended_module_instance = callbacks_prepended_module_instance # dont redefine, to save cpu cycles unless _callbacks_prepended_module_instance.method_defined? method_name _callbacks_prepended_module_instance.send(:define_method, method_name) do |*args| run_before_callbacks(method_name, *args) super_value = super(*args) run_after_callbacks(method_name, *args) super_value end end end |
#before!(method_name, *remaining_args) ⇒ Object
42 43 44 45 |
# File 'lib/super_callbacks.rb', line 42 def before!(method_name, *remaining_args) raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name before(method_name, *remaining_args) end |