Sha256: 31f5dfe5dda60d528f799c0276ea2a6a227d0528b7e7910abc09aeddc9dd94b2

Contents?: true

Size: 1.88 KB

Versions: 20

Compression:

Stored size: 1.88 KB

Contents

module ProconBypassMan
  module Callbacks
    class CallbacksChain
      attr_accessor :filter, :chain_method
      def initialize(filter: , chain_method: , block: )
        @filter = filter
        @chain_method = chain_method
        @block = block
      end
    end

    # TODO __callbacksをincludeしたクラス側で保持する. 今はnemespaceがない
    module M
      class << self
        attr_accessor :__callbacks
      end
    end

    module ClassMethods
      def define_callbacks(name)
        self.singleton_class.attr_accessor "_#{name}_callbacks"
        send "_#{name}_callbacks=", [name] # CallbacksChain

        module_eval <<-RUBY, __FILE__, __LINE__ + 1
          def _run_#{name}_callbacks(&block)
            __run_callbacks__(_#{name}_callbacks, &block)
          end
        RUBY
      end

      def set_callback(kind, filter, chain_method, &block)
        ProconBypassMan::Callbacks::M.__callbacks ||= {}
        ProconBypassMan::Callbacks::M.__callbacks[kind] = CallbacksChain.new(
          filter: filter,
          chain_method: chain_method,
          block: block,
        )
      end
    end

    # TODO haltしたらcallbackを止める
    # TODO 複数をチェインできるようにする
    def run_callbacks(kind, &block)
      chain = get_callbacks(kind) or raise("unknown callback")
      case chain.filter
      when :before
        send chain.chain_method
        block.call
      when :after
        block.call
        send chain.chain_method
      else
        raise("unknown filter")
      end
    end

    # def __run_callbacks__(name, &block)
    #   puts "called"
    # end

    def get_callbacks(kind) # :nodoc:
      ProconBypassMan::Callbacks::M.__callbacks[kind.to_sym]
    end

    def set_callbacks(name, callbacks) # :nodoc:
      send "_#{name}_callbacks=", callbacks
      ProconBypassMan::Callbacks::M.__callbacks[kind.to_sym] = callbacks
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
procon_bypass_man-0.2.1 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.2.0 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.23 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.22 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.21 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.20.2 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.20.1 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.20 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.19.1 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.19 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.18 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.17 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.16.1 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.16 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.15 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.14 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.13 lib/procon_bypass_man/support/callbacks.rb
procon_bypass_man-0.1.12 lib/procon_bypass_man/callbacks.rb
procon_bypass_man-0.1.11 lib/procon_bypass_man/callbacks.rb
procon_bypass_man-0.1.10 lib/procon_bypass_man/callbacks.rb