Module: SuperCallbacks::ClassAndInstanceMethods

Defined in:
lib/super_callbacks.rb

Instance Method Summary collapse

Instance Method Details

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



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
125
126
127
128
129
130
131
132
133
# File 'lib/super_callbacks.rb', line 95

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
        @instance_variables_before_change = instance_variables.each_with_object({}) do |instance_variable, hash|
          hash[instance_variable] = instance_variable_get(instance_variable)
        end

        run_before_callbacks(method_name, *args)
        super_value = super(*args)
        run_after_callbacks(method_name, *args)
      ensure
        remove_instance_variable(:@instance_variables_before_change)
      end

      super_value
    end
  end
end

#after!(method_name, *remaining_args, &callback_proc) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
# File 'lib/super_callbacks.rb', line 50

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



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
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/super_callbacks.rb', line 55

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
        @instance_variables_before_change = instance_variables.each_with_object({}) do |instance_variable, hash|
          hash[instance_variable] = instance_variable_get(instance_variable)
        end

        run_before_callbacks(method_name, *args)
        super_value = super(*args)
        run_after_callbacks(method_name, *args)
      ensure
        remove_instance_variable(:@instance_variables_before_change)
      end

      super_value
    end
  end
end

#before!(method_name, *remaining_args, &callback_proc) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
# File 'lib/super_callbacks.rb', line 45

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)


139
140
141
142
143
# File 'lib/super_callbacks.rb', line 139

def instance_variable_before_change(instance_variable)
  raise ArgumentError, "#{instance_variable} should be a string that starts with `@`" unless instance_variable.to_s.start_with? '@'
  raise 'You cannot call this method outside the SuperCallback cycle' if instance_variables_before_change.nil?
  instance_variables_before_change[instance_variable.to_sym]
end

#instance_variable_changed?(instance_variable) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


145
146
147
148
149
150
151
152
# File 'lib/super_callbacks.rb', line 145

def instance_variable_changed?(instance_variable)
  raise ArgumentError, "#{instance_variable} should be a string that starts with `@`" unless instance_variable.to_s.start_with? '@'
  raise 'You cannot call this method outside the SuperCallback cycle' if instance_variables_before_change.nil?

  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



135
136
137
# File 'lib/super_callbacks.rb', line 135

def instance_variables_before_change
  @instance_variables_before_change
end