Last active
December 5, 2015 22:37
-
-
Save dux/6c0467d22f6f37b25589 to your computer and use it in GitHub Desktop.
add before_save and after_save filters to sequel
This file contains hidden or 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
output | |
validate | |
before save BASE | |
before save MODEL | |
after_save MODEL |
This file contains hidden or 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
require 'sequel' | |
require 'ap' | |
Sequel.postgres('databse', :user=>'user', :password=>'pass', :host=>'localhost', :port=>5432, :max_connections=>10) | |
class Sequel::Model | |
@@_filters = { before_save:[] } | |
def self.before_save(&proc) | |
@@_filters[:before_save].push(proc) | |
end | |
def before_save | |
for func in @@_filters[:before_save] | |
instance_exec &func | |
end | |
end | |
before_save do | |
ap 'before save BASE' | |
end | |
end | |
class Country < Sequel::Model | |
before_save do | |
ap 'before save MODEL' | |
end | |
def after_save | |
ap 'after_save MODEL' | |
end | |
def validate | |
ap 'validate' | |
end | |
end | |
c = Country.last | |
c.code = 'aa' | |
c.save | |
ap c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment