Sha256: 8dbc6aa5c1d38dc3885e3d174d1cdd8e93d4ceefdccb0d9466a041fa6b069314

Contents?: true

Size: 1.08 KB

Versions: 26

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

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/callback/src/set.rb
foobara-0.0.25 projects/callback/src/set.rb
foobara-0.0.24 projects/callback/src/set.rb
foobara-0.0.23 projects/callback/src/set.rb
foobara-0.0.22 projects/callback/src/set.rb
foobara-0.0.21 projects/callback/src/set.rb
foobara-0.0.20 projects/callback/src/set.rb
foobara-0.0.19 projects/callback/src/set.rb
foobara-0.0.18 projects/callback/src/set.rb
foobara-0.0.17 projects/callback/src/set.rb
foobara-0.0.16 projects/callback/src/set.rb
foobara-0.0.15 projects/callback/src/set.rb
foobara-0.0.14 projects/callback/src/set.rb
foobara-0.0.13 projects/callback/src/set.rb
foobara-0.0.12 projects/callback/src/set.rb
foobara-0.0.11 projects/callback/src/set.rb
foobara-0.0.10 projects/callback/src/set.rb
foobara-0.0.9 projects/callback/src/set.rb
foobara-0.0.8 projects/callback/src/set.rb
foobara-0.0.7 projects/callback/src/set.rb