Class: SimpleTemplater::Hooks::Hook

Inherits:
Object
  • Object
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

#contextObject (readonly)

Returns the value of attribute context.



49
50
51
# File 'lib/simple-templater/hooks/hook.rb', line 49

def context
  @context
end

#replyObject (readonly)

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

.hooksObject



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

#keyObject



54
55
56
# File 'lib/simple-templater/hooks/hook.rb', line 54

def key
  self.class.name.split("::").last.snake_case.to_sym
end

#questionObject

Raises:

  • (NotImplementedError)


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

#runObject

Raises:

  • (NotImplementedError)


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

Returns:

  • (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