Module: SimpleRecord::Callbacks

Included in:
Base
Defined in:
lib/simple_record/callbacks.rb

Constant Summary collapse

@@callbacks =

this bit of code creates a “run_blank” function for everything value in the @@callbacks array. this function can then be inserted in the appropriate place in the save, new, destroy, etc overrides basically, this is how we recreate the callback functions

["before_validation", "before_validation_on_create", "before_validation_on_update",
"after_validation", "after_validation_on_create", "after_validation_on_update",
"before_save", "before_create", "before_update", "before_destroy",
"after_create", "after_update", "after_save",
"after_destroy"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
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
# File 'lib/simple_record/callbacks.rb', line 38

def self.included(base)
  #puts 'Callbacks included in ' + base.inspect

#        puts "setup callbacks #{base.inspect}"
  base.instance_eval <<-endofeval

        def callbacks
            @callbacks ||= {}
            @callbacks
        end


  endofeval

  @@callbacks.each do |callback|
    base.class_eval <<-endofeval

     def run_#{callback}
        # puts 'CLASS CALLBACKS for ' + self.inspect + ' = ' + self.class.callbacks.inspect
        return true if self.class.callbacks.nil?
        cnames = self.class.callbacks['#{callback}']
        cnames = [] if cnames.nil?
        # cnames += super.class.callbacks['#{callback}'] unless super.class.callbacks.nil?
        # puts 'cnames for #{callback} = ' + cnames.inspect
        return true if cnames.nil?
        cnames.each { |name|
            #puts 'run_  #{name}'
          if eval(name) == false # nil should be an ok return, only looking for false
            return false
          end
      }
      # super.run_#{callback}
      return true
    end

    endofeval

    #this bit of code creates a "run_blank" function for everything value in the @@callbacks array.
    #this function can then be inserted in the appropriate place in the save, new, destroy, etc overrides
    #basically, this is how we recreate the callback functions
    base.instance_eval <<-endofeval

#            puts 'defining callback=' + callback + ' for ' + self.inspect
        #we first have to make an initialized array for each of the callbacks, to prevent problems if they are not called

        def #{callback}(*args)
#                puts 'callback called in ' + self.inspect + ' with ' + args.inspect

            #make_dirty(arg_s, value)
            #self[arg.to_s]=value
            #puts 'value in callback #{callback}=' + value.to_s
            args.each do |arg|
                cnames = callbacks['#{callback}']
                #puts '\tcnames1=' + cnames.inspect + ' for class ' + self.inspect
                cnames = [] if cnames.nil?
                cnames << arg.to_s if cnames.index(arg.to_s).nil?
                #puts '\tcnames2=' + cnames.inspect
                callbacks['#{callback}'] = cnames
            end
        end

    endofeval
  end
end

.setup_callbacks(base) ⇒ Object



110
111
112
# File 'lib/simple_record/callbacks.rb', line 110

def self.setup_callbacks(base)

end

Instance Method Details

#after_destroyObject



106
107
# File 'lib/simple_record/callbacks.rb', line 106

def after_destroy()
end

#before_destroyObject



103
104
# File 'lib/simple_record/callbacks.rb', line 103

def before_destroy()
end