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

Instance Method Details

#after(method_name, callback_method_name = nil, options = {}, &callback_proc) ⇒ Object



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
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
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 68

def after(method_name, callback_method_name = nil, options = {}, &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 = options.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 options[:if] && ![Symbol, String, Proc].include?(options[:if].class)
    raise ArgumentError, "Only `Symbol`, `String` or `Proc` allowed for `options[:if]`, but is #{options[: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, options[: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|
      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

Raises:

  • (ArgumentError)


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
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 14

def before(method_name, callback_method_name = nil, options = {}, &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 = options.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 options[: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 #{options[: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, options[: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|
      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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


127
128
129
130
131
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 127

def instance_variable_before_change(instance_variable)
  raise 'You cannot call this method outside the SuperCallback 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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 133

def instance_variable_changed?(instance_variable)
  raise 'You cannot call this method outside the SuperCallback 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_changeObject



122
123
124
125
# File 'lib/super_callbacks/class_and_instance_methods.rb', line 122

def instance_variables_before_change
  raise 'You cannot call this method outside the SuperCallback 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