Class: Vigilem::Core::Hooks::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/vigilem/core/hooks/hook.rb

Overview

bindable group of callbacks

Direct Known Subclasses

ConditionalHook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_name, options = {}, &config) ⇒ Hook

Returns a new instance of Hook.

Parameters:

  • hook_name
  • options (Hash) (defaults to: {})
  • config (Proc)


18
19
20
21
22
23
24
# File 'lib/vigilem/core/hooks/hook.rb', line 18

def initialize(hook_name, options={}, &config)
  @name = hook_name
  options[:inheritable] = true if options[:inheritable].nil?
  (@options = options).freeze
  @callbacks = []
  instance_eval(&config) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/vigilem/core/hooks/hook.rb', line 13

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/vigilem/core/hooks/hook.rb', line 13

def options
  @options
end

#ownerObject

Returns the value of attribute owner.



13
14
15
# File 'lib/vigilem/core/hooks/hook.rb', line 13

def owner
  @owner
end

Instance Method Details

#<<(callback) ⇒ Object

Parameters:

  • callback

Returns:



35
36
37
# File 'lib/vigilem/core/hooks/hook.rb', line 35

def <<(callback)
  @callbacks << (callback.is_a?(Callback) ? callback : CallbackProc.new(&callback))
end

#add(&callback) ⇒ Object

Parameters:

  • callback (Proc)

Returns:

See Also:



43
44
45
# File 'lib/vigilem/core/hooks/hook.rb', line 43

def add(&callback)
  self << callback
end

#bind(context) ⇒ Object

TODO:

Error for name collision

Returns the result of the evaluation.

Parameters:

  • context

    the object to bind this hook to

Returns:

  • the result of the evaluation



110
111
112
113
114
115
116
117
118
# File 'lib/vigilem/core/hooks/hook.rb', line 110

def bind(context)
  hook, hook_body = self, body()
  self.owner = Support::Utils.get_class(context)
  ret = context.instance_eval do
    define_singleton_method(hook.name, &hook_body)
  end
  owner.hooks << hook
  ret
end

#call(*args, &block) ⇒ Array

Parameters:

  • args (Array)
  • block (Proc)

Returns:

  • (Array)


58
59
60
# File 'lib/vigilem/core/hooks/hook.rb', line 58

def call(*args, &block)
  enumerate({:args => args, :block => block }, &on_run)
end

#callbacks(context = nil) ⇒ Object

Parameters:

  • context (defaults to: nil)

Returns:



50
51
52
# File 'lib/vigilem/core/hooks/hook.rb', line 50

def callbacks(context=nil)
  if context then @callbacks.reject {|cb| Utils.callback_is_in_subclass?(context, cb) } else @callbacks end
end

#default_bodyProc

Returns:

  • (Proc)


76
77
78
79
80
81
82
83
84
# File 'lib/vigilem/core/hooks/hook.rb', line 76

def default_body
  @default_body ||= begin
      hook = self
      lambda do |opts={}, &block|
        raise 'block not given' unless block
        hook.callbacks << CallbackProc.new(opts, &block)
      end
    end
end

#generate(klass) ⇒ Object

uses define_method instead of define_singleton_method like bind!

hmm I could have one hook that spans multiple instances, bad or good? doesn’t work with @owner

Parameters:

  • klass (Class)

Returns:



127
128
129
130
131
132
133
134
135
# File 'lib/vigilem/core/hooks/hook.rb', line 127

def generate(klass)
  hook, hook_body = self, body()
  self.owner = klass
  ret = klass.instance_eval do
    define_method(hook.name, &hook_body)
  end
  klass.hooks << hook
  ret
end

#inheritable?TrueClass || FalseClass

Returns:

  • (TrueClass || FalseClass)


28
29
30
# File 'lib/vigilem/core/hooks/hook.rb', line 28

def inheritable?
  !!@options[:inheritable]
end

#run(context, *args, &block) ⇒ Array Also known as: evaluate

Parameters:

  • context
  • args (Array)
  • block (Proc)

Returns:

  • (Array)


67
68
69
# File 'lib/vigilem/core/hooks/hook.rb', line 67

def run(context, *args, &block)
  enumerate({:args => args, :block => block, :context => context }, &on_run)
end

#to_aryArray

Returns ; [name, self].

Returns:

  • (Array)

    ; [name, self]



92
93
94
# File 'lib/vigilem/core/hooks/hook.rb', line 92

def to_ary
  [self.name, self]
end

#to_hname => self

Returns:



97
98
99
# File 'lib/vigilem/core/hooks/hook.rb', line 97

def to_h
  {self.name => self }
end

#to_procProc

Returns:

  • (Proc)


103
104
105
# File 'lib/vigilem/core/hooks/hook.rb', line 103

def to_proc
  body()
end

#to_sString

Returns:

  • (String)


87
88
89
# File 'lib/vigilem/core/hooks/hook.rb', line 87

def to_s
  "#{super().chomp('>')} #{name}>"
end