Class: Lolita::Hooks::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/hooks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_class, hook_name, options) ⇒ Runner

Returns a new instance of Runner.



91
92
93
94
95
96
# File 'lib/lolita/hooks.rb', line 91

def initialize(hook_class,hook_name, options)
  @hook_class = hook_class
  @hook_name = hook_name
  @options = options
  @options[:once] = @options[:once] == true ? @hook_class : @options[:once]
end

Instance Attribute Details

#given_callback_contentObject

Returns the value of attribute given_callback_content.



88
89
90
# File 'lib/lolita/hooks.rb', line 88

def given_callback_content
  @given_callback_content
end

#hooks_run_scopeObject

Returns the value of attribute hooks_run_scope.



88
89
90
# File 'lib/lolita/hooks.rb', line 88

def hooks_run_scope
  @hooks_run_scope
end

#hooks_scopeObject

Hooks scope is used to execute callbacks. By default it is class itself.



99
100
101
# File 'lib/lolita/hooks.rb', line 99

def hooks_scope
  @hooks_scope || @hook_class
end

Class Method Details

.runned?(hook_object, hook_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/lolita/hooks.rb', line 77

def runned?(hook_object,hook_name)
  if hook_object.respond_to?(:hooks_runned)
    hook_object.hooks_runned.include?(hook_name)
  end
end

.singleton_hook(hook_object, hook_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lolita/hooks.rb', line 65

def singleton_hook(hook_object,hook_name)
  class << hook_object
    def hooks_runned(name=nil)
      @hooks_runned ||=[]
      @hooks_runned << name if name
      @hooks_runned
    end
  end

  hook_object.hooks_runned(hook_name)
end

.singleton_hooksObject



83
84
85
# File 'lib/lolita/hooks.rb', line 83

def singleton_hooks
  @singleton_hooks || {}
end

Instance Method Details

#let_contentObject

Call callback block inside of run block.

Example

MyClass.run(:before_save) do
   do_stuff
   let_content # execute callback block(-s) in same scope as run is executed.
end


121
122
123
124
125
126
127
# File 'lib/lolita/hooks.rb', line 121

def let_content
  if self.given_callback_content.respond_to?(:call)
    run_block(self.given_callback_content)
  elsif self.given_callback_content
    self.given_callback_content
  end
end

#run(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lolita/hooks.rb', line 103

def run(&block)
  if !@options[:once] || (@options[:once] && !self.class.runned?(@options[:once],@hook_name))
    self.class.singleton_hook(@options[:once] || Object,@hook_name)
    result = nil
    in_hooks_scope(@options[:scope],@options[:run_scope]) do
      callback = get_callback(@hook_name)
      result = run_callback(callback,&block)
    end
    result
  end
end