Module: SuperCallbacks::ClassAndInstanceMethods
- Defined in:
- lib/super_callbacks/class_and_instance_methods.rb
Overview
The methods defined here will be available “class” and “instance” for any class where SuperCallbacks is included
Instance Method Summary collapse
- #after(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
- #after!(method_name, *remaining_args, &callback_proc) ⇒ Object
- #before(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
- #before!(method_name, *remaining_args, &callback_proc) ⇒ Object
- #instance_variable_before_change(instance_variable) ⇒ Object
- #instance_variable_changed?(instance_variable) ⇒ Boolean
- #instance_variables_before_change ⇒ Object
Instance Method Details
#after(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 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 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 70 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]] super_callbacks_prepended = self.super_callbacks_prepended # dont redefine, to save cpu cycles unless super_callbacks_prepended.instance_methods(false).include? method_name super_callbacks_prepended.send(:define_method, method_name) do |*args| return super(*args) if self.class.super_callbacks_prepended != super_callbacks_prepended begin # refactored to use Thread.current for thread-safetiness Thread.current[:super_callbacks_all_instance_variables_before_change] ||= {} Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] ||= [] all_instance_variables_before_change = instance_variables.each_with_object({}) do |instance_variable, hash| hash[instance_variable] = instance_variable_get(instance_variable) end Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] << all_instance_variables_before_change run_before_callbacks(method_name, *args) super_value = super(*args) run_after_callbacks(method_name, *args) ensure Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].pop if Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].empty? Thread.current[:super_callbacks_all_instance_variables_before_change].delete(object_id) end if Thread.current[:super_callbacks_all_instance_variables_before_change].empty? Thread.current[:super_callbacks_all_instance_variables_before_change] = nil end end super_value end end end |
#after!(method_name, *remaining_args, &callback_proc) ⇒ Object
9 10 11 12 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 9 def after!(method_name, *remaining_args, &callback_proc) raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name after(method_name, *remaining_args, &callback_proc) end |
#before(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 14 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]] super_callbacks_prepended = self.super_callbacks_prepended # dont redefine, to save cpu cycles unless super_callbacks_prepended.instance_methods(false).include? method_name super_callbacks_prepended.send(:define_method, method_name) do |*args| return super(*args) if self.class.super_callbacks_prepended != super_callbacks_prepended begin # refactored to use Thread.current for thread-safetiness Thread.current[:super_callbacks_all_instance_variables_before_change] ||= {} Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] ||= [] all_instance_variables_before_change = instance_variables.each_with_object({}) do |instance_variable, hash| hash[instance_variable] = instance_variable_get(instance_variable) end Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] << all_instance_variables_before_change run_before_callbacks(method_name, *args) super_value = super(*args) run_after_callbacks(method_name, *args) ensure Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].pop if Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].empty? Thread.current[:super_callbacks_all_instance_variables_before_change].delete(object_id) end if Thread.current[:super_callbacks_all_instance_variables_before_change].empty? Thread.current[:super_callbacks_all_instance_variables_before_change] = nil end end super_value end end end |
#before!(method_name, *remaining_args, &callback_proc) ⇒ Object
4 5 6 7 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 4 def before!(method_name, *remaining_args, &callback_proc) raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name before(method_name, *remaining_args, &callback_proc) end |
#instance_variable_before_change(instance_variable) ⇒ Object
131 132 133 134 135 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 131 def instance_variable_before_change(instance_variable) raise 'You cannot call this method outside the SuperCallbacks cycle' if Thread.current[:super_callbacks_all_instance_variables_before_change].nil? raise ArgumentError, "#{instance_variable} should be a string that starts with `@`" unless instance_variable.to_s.start_with? '@' instance_variables_before_change[instance_variable.to_sym] end |
#instance_variable_changed?(instance_variable) ⇒ Boolean
137 138 139 140 141 142 143 144 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 137 def instance_variable_changed?(instance_variable) raise 'You cannot call this method outside the SuperCallbacks cycle' if Thread.current[:super_callbacks_all_instance_variables_before_change].nil? raise ArgumentError, "#{instance_variable} should be a string that starts with `@`" unless instance_variable.to_s.start_with? '@' before_change_value = instance_variable_before_change(instance_variable.to_sym) current_value = instance_variable_get(instance_variable) before_change_value != current_value end |
#instance_variables_before_change ⇒ Object
126 127 128 129 |
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 126 def instance_variables_before_change raise 'You cannot call this method outside the SuperCallbacks cycle' if Thread.current[:super_callbacks_all_instance_variables_before_change].nil? Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].last end |