Created
July 18, 2013 04:17
-
-
Save anthonylewis/6026685 to your computer and use it in GitHub Desktop.
An example using class instance variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Record | |
def self.before_save(*methods) | |
@callbacks ||= [] | |
@callbacks += methods | |
end | |
def self.callbacks | |
@callbacks | |
end | |
def save | |
self.class.callbacks.each do |c| | |
send c if respond_to? c | |
end | |
puts "Saved" | |
end | |
end | |
class User < Record | |
before_save :saving | |
def saving | |
puts "Saving..." | |
end | |
end | |
u = User.new | |
u.save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from class, this might be more readable for beginners..
send self.c if self.respond_to? c