Sha256: a7dae35756f84bb2934623d15702877f97345df938a1ca70e69f0818d85c414a

Contents?: true

Size: 1.92 KB

Versions: 48

Compression:

Stored size: 1.92 KB

Contents

module Stripe
  module Callbacks
    module Builder
      extend ActiveSupport::Concern

      included do
        extend ActiveSupport::Concern
        @critical_callbacks = Hash.new do |h, k|
          h[k] = []
        end
        @noncritical_callbacks = Hash.new do |h, k|
          h[k] = []
        end
        module ClassMethods
        end

        class << self
          attr_reader :critical_callbacks, :noncritical_callbacks

          def clear_callbacks!
            critical_callbacks.clear
            noncritical_callbacks.clear
          end

          def callback(name)
            method_name = "after_#{name.gsub('.', '_')}"

            self::ClassMethods.send(:define_method, method_name) do |options = {}, &block|
              ::Stripe::Callbacks::noncritical_callbacks[name] << ::Stripe::Callbacks.callback_matcher(options, block)
            end
            self::ClassMethods.send(:define_method, "#{method_name}!") do |options = {}, &block|
              ::Stripe::Callbacks::critical_callbacks[name] << ::Stripe::Callbacks.callback_matcher(options, block)
            end
          end

          def callback_matcher(options, block)
            case only = options[:only]
            when Proc, Method
              proc do |target, evt|
                block.call(target, evt) if only.call(target, evt)
              end
            when Array, Set
              stringified_keys = only.map(&:to_s)
              proc do |target, evt|
                stringified_previous_attributes_keys = evt.data.previous_attributes.keys.map(&:to_s)
                intersection =  stringified_previous_attributes_keys - stringified_keys
                block.call(target, evt) if intersection != stringified_previous_attributes_keys
              end
            when nil
              block
            else
              callback_matcher options.merge(:only => [only]), block
            end
          end
        end
      end
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
stripe-rails-2.6.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.5.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.4.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.5 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.4 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.3 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.2 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.1 lib/stripe/callbacks/builder.rb
stripe-rails-2.3.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.2.1 lib/stripe/callbacks/builder.rb
stripe-rails-2.2.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.1.0 lib/stripe/callbacks/builder.rb
stripe-rails-2.0.0 lib/stripe/callbacks/builder.rb
stripe-rails-1.10.2 lib/stripe/callbacks/builder.rb
stripe-rails-2.0.0.pre lib/stripe/callbacks/builder.rb
stripe-rails-1.10.1 lib/stripe/callbacks/builder.rb
stripe-rails-1.10.0 lib/stripe/callbacks/builder.rb
stripe-rails-1.9.1 lib/stripe/callbacks/builder.rb
stripe-rails-1.9.0 lib/stripe/callbacks/builder.rb
stripe-rails-1.8.2 lib/stripe/callbacks/builder.rb