Class: SimpleTemplater::Hooks::Hook
- Inherits:
-
Object
- Object
- SimpleTemplater::Hooks::Hook
show all
- Defined in:
- lib/simple-templater/hooks/hook.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context) ⇒ Hook
Returns a new instance of Hook.
50
51
52
|
# File 'lib/simple-templater/hooks/hook.rb', line 50
def initialize(context)
@context = context
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
49
50
51
|
# File 'lib/simple-templater/hooks/hook.rb', line 49
def context
@context
end
|
#reply ⇒ Object
Returns the value of attribute reply.
49
50
51
|
# File 'lib/simple-templater/hooks/hook.rb', line 49
def reply
@reply
end
|
Class Method Details
.find(key) ⇒ Object
29
30
31
|
# File 'lib/simple-templater/hooks/hook.rb', line 29
def self.find(key)
self.hooks.find { |hook| hook.name.split("::").last.snake_case.to_sym == key }
end
|
.hooks ⇒ Object
25
26
27
|
# File 'lib/simple-templater/hooks/hook.rb', line 25
def self.hooks
@@hooks ||= Array.new
end
|
.inherited(klass) ⇒ Object
33
34
35
|
# File 'lib/simple-templater/hooks/hook.rb', line 33
def self.inherited(klass)
self.hooks.push(klass)
end
|
.invoke(context) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/simple-templater/hooks/hook.rb', line 37
def self.invoke(context)
hook = self.new(context)
if hook.will_run?
puts "Running hook #{self}"
hook.run
return true
else
puts "Skipping hook #{self}"
return false
end
end
|
Instance Method Details
#key ⇒ Object
54
55
56
|
# File 'lib/simple-templater/hooks/hook.rb', line 54
def key
self.class.name.split("::").last.snake_case.to_sym
end
|
#question ⇒ Object
67
68
69
|
# File 'lib/simple-templater/hooks/hook.rb', line 67
def question
raise NotImplementedError, "Hook #{self.key} have to have implemented method #question"
end
|
#run ⇒ Object
71
72
73
|
# File 'lib/simple-templater/hooks/hook.rb', line 71
def run
raise NotImplementedError, "Hook #{self.key} have to have implemented method #run"
end
|
#will_run? ⇒ Boolean
58
59
60
61
62
63
64
65
|
# File 'lib/simple-templater/hooks/hook.rb', line 58
def will_run?
return @reply unless @reply.nil?
if self.context.has_key?(key)
@reply = self.context[key]
else
@reply = self.question
end
end
|