Skip to content

Instantly share code, notes, and snippets.

@dux
Last active December 5, 2015 22:37
Show Gist options
  • Save dux/6c0467d22f6f37b25589 to your computer and use it in GitHub Desktop.
Save dux/6c0467d22f6f37b25589 to your computer and use it in GitHub Desktop.
add before_save and after_save filters to sequel
output
validate
before save BASE
before save MODEL
after_save MODEL
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