module Kungfuig::Aspector

Generic helper for massive attaching aspects

Public Class Methods

attach(to, before: nil, after: nil, exclude: nil) click to toggle source
# File lib/kungfuig/aspector.rb, line 44
def attach(to, before: nil, after: nil, exclude: nil)
  fail ArgumentError, "Trying to attach nothing to #{klazz}##{to}. I need a block!" unless block_given?

  cb = Proc.new

  klazz = case to
          when Module then to # got a class! wow, somebody has the documentation read
          when String, Symbol then H.new.try_to_class(to) # we are ready to get a class name
          else class << to; self; end # attach to klazz’s eigenclass if object given
          end

  { before: before, after: after }.each do |k, var|
    H.new.value_to_method_list(klazz, var, exclude).each do |m|
      Kungfuig::Prepender.new(to, m).public_send(k, &cb).hook!
    end unless var.nil?
  end

  klazz.is_a?(Module) ? klazz.aspects : { promise: klazz }
end
bulk(hos) click to toggle source

'Test':

after:
  'yo': 'YoCalledAsyncHandler#process'
  'yo1' : 'YoCalledAsyncHandler#process'
before:
  'yo': 'YoCalledAsyncHandler#process'
# File lib/kungfuig/aspector.rb, line 71
def bulk(hos)
  Kungfuig.load_stuff(hos).map do |klazz, hash|
    next if hash.empty?
    [klazz, H.new.remap_hash_for_easy_iteration(hash).map do |handler, methods|
      begin
        attach(klazz, **methods, &H.new.proc_instance(handler))
      rescue => e
        raise ArgumentError, [
          "Bad input to Kungfuig::Aspector##{__callee__}.",
          "Args: #{methods.inspect}",
          "Original exception: #{e.message}.",
          e.backtrace.unshift("Backtrace:").join("#{$/}⮩  ")
        ].join($/.to_s)
      end
    end]
  end.compact.to_h
end

Private Instance Methods

attach(to, before: nil, after: nil, exclude: nil) click to toggle source
# File lib/kungfuig/aspector.rb, line 44
def attach(to, before: nil, after: nil, exclude: nil)
  fail ArgumentError, "Trying to attach nothing to #{klazz}##{to}. I need a block!" unless block_given?

  cb = Proc.new

  klazz = case to
          when Module then to # got a class! wow, somebody has the documentation read
          when String, Symbol then H.new.try_to_class(to) # we are ready to get a class name
          else class << to; self; end # attach to klazz’s eigenclass if object given
          end

  { before: before, after: after }.each do |k, var|
    H.new.value_to_method_list(klazz, var, exclude).each do |m|
      Kungfuig::Prepender.new(to, m).public_send(k, &cb).hook!
    end unless var.nil?
  end

  klazz.is_a?(Module) ? klazz.aspects : { promise: klazz }
end
bulk(hos) click to toggle source

'Test':

after:
  'yo': 'YoCalledAsyncHandler#process'
  'yo1' : 'YoCalledAsyncHandler#process'
before:
  'yo': 'YoCalledAsyncHandler#process'
# File lib/kungfuig/aspector.rb, line 71
def bulk(hos)
  Kungfuig.load_stuff(hos).map do |klazz, hash|
    next if hash.empty?
    [klazz, H.new.remap_hash_for_easy_iteration(hash).map do |handler, methods|
      begin
        attach(klazz, **methods, &H.new.proc_instance(handler))
      rescue => e
        raise ArgumentError, [
          "Bad input to Kungfuig::Aspector##{__callee__}.",
          "Args: #{methods.inspect}",
          "Original exception: #{e.message}.",
          e.backtrace.unshift("Backtrace:").join("#{$/}⮩  ")
        ].join($/.to_s)
      end
    end]
  end.compact.to_h
end