Sha256: 9927b9163a9dd1c088dafadafc9f1ae12661d89ca2b5e7de24c3d1bf1485bdcf
Contents?: true
Size: 1.23 KB
Versions: 1
Compression:
Stored size: 1.23 KB
Contents
module DataMapper class Callbacks EVENTS = [ :before_materialize, :after_materialize, :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy, :before_validate, :after_validate ] def initialize @callbacks = Hash.new { |h,k| h[k.to_sym] = [] } end alias ruby_method_missing method_missing def method_missing(sym, *args) if EVENTS.include?(sym) self.class.send(:define_method, sym) { @callbacks[sym] } return send(sym) elsif sym.to_s =~ /^execute_(\w+)/ && EVENTS.include?($1.to_sym) return execute(args.first, $1.to_sym) end super end def execute(name, instance) @callbacks[name].each do |callback| if callback.kind_of?(String) instance.instance_eval(callback) else instance.instance_eval(&callback) end end end def add(name, string = nil, &block) callback = send(name) raise ArgumentError.new("You didn't specify a callback in either string or block form.") if string.nil? && block.nil? callback << (string.nil? ? block : string) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
datamapper-0.1.0 | lib/data_mapper/callbacks.rb |