Sha256: 157d9664db5ac2ec1e9a2040394110ef0b9658a56ca73c15cb25b19a6a9a3b70

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Syncano
  module Model
    # Module with callbacks functionality for Syncano::ActiveRecord
    module Callbacks
      extend ActiveSupport::Concern

      # Class methods for Syncano::ActiveRecord::Callbacks module
      module ClassMethods
        private

        [:validation, :save, :create, :update, :destroy].each do |action|
          [:before, :after].each do |type|
            define_method("prepend_#{type}_#{action}") do |argument|
              send("#{type}_#{action}_callbacks").unshift(argument)
            end

            define_method("#{type}_#{action}") do |argument|
              send("#{type}_#{action}_callbacks") << argument
            end
          end
        end

        def inherited(subclass)
          # Initializes chains for all types of callbacks
          [:validation, :save, :create, :update, :destroy].each do |action|
            [:before, :after].each do |type|
              chain_name = "#{type}_#{action}_callbacks"

              subclass.class_attribute chain_name
              subclass.send("#{chain_name}=", [])
            end
          end

          super
        end
      end

      # Processes callbacks with specified type
      # @param [Symbol, String] type
      def process_callbacks(type)
        if respond_to?("#{type}_callbacks")
          send("#{type}_callbacks").each do |callback_name|
            send(callback_name)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
syncano-4.0.0.alpha1 lib/syncano/model/callbacks.rb
syncano-4.0.0.alpha lib/syncano/model/callbacks.rb
syncano-4.0.0.pre lib/syncano/model/callbacks.rb