Sha256: 8dbc6aa5c1d38dc3885e3d174d1cdd8e93d4ceefdccb0d9466a041fa6b069314

Contents?: true

Size: 1.08 KB

Versions: 27

Compression:

Stored size: 1.08 KB

Contents

module Foobara
  module Callback
    class Set
      attr_accessor :callbacks

      def initialize(callback_hash = {})
        self.callbacks = {}

        callback_hash.each do |type, blocks|
          send("#{type}=", blocks.dup)
        end
      end

      foobara_delegate :size, to: :callbacks

      def union(set)
        unioned = Set.new(callbacks)

        set.callbacks.each_pair do |type, blocks|
          next if blocks.empty?

          unioned_blocks = unioned[type]
          blocks.each { |block| unioned_blocks << block }
        end

        unioned
      end

      def |(other)
        union(other)
      end

      def [](type)
        send(type)
      end

      Block.types.each do |type|
        define_method type do
          callbacks[type] ||= []
        end

        define_method "#{type}=" do |blocks|
          callbacks[type] ||= blocks
        end

        define_method "each_#{type}" do |&block|
          send(type).each(&block)
        end

        define_method "has_#{type}_callbacks?" do
          !send(type).empty?
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/callback/src/set.rb
foobara-0.0.6 projects/callback/src/set.rb
foobara-0.0.5 projects/callback/src/set.rb
foobara-0.0.4 projects/callback/src/set.rb
foobara-0.0.3 projects/callback/src/set.rb
foobara-0.0.2 projects/callback/src/set.rb
foobara-0.0.1 projects/callback/src/set.rb