Sha256: 717293d1e09f96b6e9ba89836e497bc8afc5a50e7bbb353f68f9e1f3bc25cde9

Contents?: true

Size: 828 Bytes

Versions: 2

Compression:

Stored size: 828 Bytes

Contents

module Sequel
  class Model
    def self.get_hooks(key)
      @hooks ||= {}
      @hooks[key] ||= []
    end
    
    def self.has_hooks?(key)
      !get_hooks(key).empty?
    end
    
    def run_hooks(key)
      self.class.get_hooks(key).each {|h| instance_eval(&h)}
    end
    
    def self.before_save(&block)
      get_hooks(:before_save).unshift(block)
    end
    
    def self.before_create(&block)
      get_hooks(:before_create).unshift(block)
    end
    
    def self.before_destroy(&block)
      get_hooks(:before_destroy).unshift(block)
    end
    
    def self.after_save(&block)
      get_hooks(:after_save) << block
    end
    
    def self.after_create(&block)
      get_hooks(:after_create) << block
    end
    
    def self.after_destroy(&block)
      get_hooks(:after_destroy) << block
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel-0.2.1 lib/sequel/model/hooks.rb
sequel-0.2.1.1 lib/sequel/model/hooks.rb