Sha256: a2cf4c32944b02c16c6f21e92076cc26b0fe09aa5644d52bd3ff7471a85a5107

Contents?: true

Size: 1020 Bytes

Versions: 4

Compression:

Stored size: 1020 Bytes

Contents

module Light
  module Services
    module Callbacks
      def self.included(base)
        base.extend ClassMethods
        base.class_eval do
          class << self
            attr_accessor :callbacks
          end
        end
      end

      private

      def run_callbacks(type, opts = {})
        opts[:break] = true unless opts.key?(:break)

        callbacks = (self.class.callbacks || []).select { |callback| callback[:type] == type }
        callbacks.each do |callback|
          break if !success? && opts[:break]
          send(callback[:method])
        end
      end

      module ClassMethods
        def before(method)
          set_callback(:before, method)
        end

        def after(method)
          set_callback(:after, method)
        end

        def finally(method)
          set_callback(:finally, method)
        end

        def set_callback(type, method)
          self.callbacks ||= []
          self.callbacks << { type: type, method: method }
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
light-services-0.4.0 lib/light/services/callbacks.rb
light-services-0.3.0 lib/light/services/callbacks.rb
light-services-0.2.0 lib/light/services/callbacks.rb
light-services-0.1.0 lib/light/services/callbacks.rb