Class: Kungfuig::Prepender

Inherits:
Object
  • Object
show all
Defined in:
lib/kungfuig/prepender.rb

Defined Under Namespace

Classes: MalformedTarget

Constant Summary collapse

AGRESSIVE_ERRORS =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **params) ⇒ Prepender

Parameters might be: • 1

— method instance
— string in form "Class#method"

• 2

— class (String, Symbol or Class), method name (String, Symbol)
— instance (Object), method name (String, Symbol)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kungfuig/prepender.rb', line 47

def initialize *args, **params
   = { before: nil, after: nil, on_hook: nil, on_error: nil }
  @klazz, @method, @receiver =  case args.size
                                when 1
                                  case args.first
                                  when Method then [(class << args.first.receiver ; self ; end), args.first.name, args.first.receiver]
                                  when UnboundMethod then [args.first.owner, args.first.name]
                                  when String
                                    k, m = args.first.split('#')
                                    [k, m && m.to_sym]
                                  end
                                when 2
                                  case args.first
                                  when Module, String then [args.first, args.last.to_sym]
                                  when Symbol then [args.first.to_s.split('_').map(&:capitalize).join, args.last.to_sym]
                                  else
                                    [(class << args.first ; self ; end), args.last.to_sym, args.first]
                                  end
                                end

  @options = params
  after(Proc.new) if block_given? # assign the block to after by default

  fail MalformedTarget.new "Unable to lookup class", args unless @klazz
  fail MalformedTarget.new "Unable to lookup method", args unless @method
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



38
39
40
# File 'lib/kungfuig/prepender.rb', line 38

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/kungfuig/prepender.rb', line 38

def options
  @options
end

#receiverObject (readonly)

Returns the value of attribute receiver.



38
39
40
# File 'lib/kungfuig/prepender.rb', line 38

def receiver
  @receiver
end

#λObject (readonly)

Returns the value of attribute λ.



38
39
40
# File 'lib/kungfuig/prepender.rb', line 38

def λ
  
end

Class Method Details

.anteponer(*args) ⇒ Object



32
33
34
35
# File 'lib/kungfuig/prepender.rb', line 32

def anteponer *args
  fail MalformedTarget.new "Factory requires a block; use Prepender#new for more accurate tuning", args unless block_given?
  Prepender.new(*args, &Proc.new).hook!
end

.error!(e, **hash) ⇒ Object



28
29
30
# File 'lib/kungfuig/prepender.rb', line 28

def error! e, **hash
  errors << [e, hash]
end

.errorsObject



24
25
26
# File 'lib/kungfuig/prepender.rb', line 24

def errors
  @errors ||= []
end

Instance Method Details

#before(λ = nil) ⇒ Object Also known as: after, on_hook, on_error



74
75
76
# File 'lib/kungfuig/prepender.rb', line 74

def before λ = nil
  tap { [__callee__] = λ || (block_given? ? Proc.new : nil) }
end

#hook!Object



81
82
83
# File 'lib/kungfuig/prepender.rb', line 81

def hook!
  tap { postpone_hook }
end