Module: Kungfuig::Aspector

Defined in:
lib/kungfuig/aspector.rb

Overview

Generic helper for massive attaching aspects

Class Method Summary collapse

Class Method Details

.attach(to, before: nil, after: nil, exclude: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

‘Test’:

after:
  'yo': 'YoCalledAsyncHandler#process'
  'yo1' : 'YoCalledAsyncHandler#process'
before:
  'yo': 'YoCalledAsyncHandler#process'


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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