Sha256: c201d4316f8986ba4697121d617e2167191ecc82dbd01657c142d4b65ba659b9

Contents?: true

Size: 1.71 KB

Versions: 93

Compression:

Stored size: 1.71 KB

Contents

# Copyright (c) 2015 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.io/terms.html

require 'sqreen/log'

module Sqreen
  class CBTree
    include Enumerable
    # Callbacks tree:
    # class
    # methods
    # position

    def initialize
      @by_class = {}
    end

    def add(cb)
      @by_class[cb.klass] = {} unless @by_class[cb.klass]

      cb_klass = @by_class[cb.klass]
      unless cb_klass[cb.method]
        cb_klass[cb.method] = { :pre => [], :post => [], :failing => [] }
      end

      methods = cb_klass[cb.method]

      methods[:pre] << cb if cb.pre?
      methods[:post] << cb if cb.post?
      methods[:failing] << cb if cb.failing?
    end

    def remove(cb)
      types = @by_class[cb.klass][cb.method]

      types[:pre].delete cb  if cb.pre?
      types[:post].delete cb if cb.post?
      types[:failing].delete cb if cb.failing?
    end

    def get(klass, method, type = nil)
      k = @by_class[klass]
      unless k
        log(format('Error: no cb registered for class %s (%s)', klass.inspect, klass.class))
        log(inspect)
        return []
      end
      cbs = k[method]
      unless cbs
        log(format('Error: no cbs registered for method %s.%s', klass, method))
        log(inspect)
        return []
      end

      if type.nil?
        res = Set.new
        cbs.values.each do |v|
          res += v
        end
        return res.to_a
      else
        return cbs[type]
      end
    end

    def each
      @by_class.each do |_klass, values|
        values.each do |_method, cbs|
          cbs.values.each do |cb_ary|
            cb_ary.each do |cb|
              yield cb
            end
          end
        end
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 2 rubygems

Version Path
sqreen-1.10.4-java lib/sqreen/callback_tree.rb
sqreen-alt-1.10.4 lib/sqreen/callback_tree.rb
sqreen-1.10.4 lib/sqreen/callback_tree.rb
sqreen-1.10.3-java lib/sqreen/callback_tree.rb
sqreen-alt-1.10.3 lib/sqreen/callback_tree.rb
sqreen-1.10.3 lib/sqreen/callback_tree.rb
sqreen-1.10.2-java lib/sqreen/callback_tree.rb
sqreen-alt-1.10.2 lib/sqreen/callback_tree.rb
sqreen-1.10.2 lib/sqreen/callback_tree.rb
sqreen-1.10.1-java lib/sqreen/callback_tree.rb
sqreen-alt-1.10.1 lib/sqreen/callback_tree.rb
sqreen-1.10.1 lib/sqreen/callback_tree.rb
sqreen-1.10.0-java lib/sqreen/callback_tree.rb
sqreen-alt-1.10.0 lib/sqreen/callback_tree.rb
sqreen-1.10.0 lib/sqreen/callback_tree.rb
sqreen-1.9.2-java lib/sqreen/callback_tree.rb
sqreen-1.9.2 lib/sqreen/callback_tree.rb
sqreen-1.9.1-java lib/sqreen/callback_tree.rb
sqreen-1.9.1 lib/sqreen/callback_tree.rb
sqreen-1.9.0-java lib/sqreen/callback_tree.rb