Class: Cognition::Plugins::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cognition/plugins/base.rb

Direct Known Subclasses

Default

Constant Summary collapse

RENDER_DEFAULTS =
{
  type: "html",
  extension: "erb"
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot = nil) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
# File 'lib/cognition/plugins/base.rb', line 28

def initialize(bot = nil)
  @matchers = self.class.definitions.collect do |trigger, method_name, options|
    Matcher.new(trigger, options, &Proc.new(&method(method_name)))
  end
  @bot = bot
end

Class Attribute Details

.view_rootObject

Returns the value of attribute view_root.



11
12
13
# File 'lib/cognition/plugins/base.rb', line 11

def view_root
  @view_root
end

Instance Attribute Details

#botObject

Returns the value of attribute bot.



26
27
28
# File 'lib/cognition/plugins/base.rb', line 26

def bot
  @bot
end

#matchersObject

Returns the value of attribute matchers.



26
27
28
# File 'lib/cognition/plugins/base.rb', line 26

def matchers
  @matchers
end

Class Method Details

.definitionsObject



39
40
41
# File 'lib/cognition/plugins/base.rb', line 39

def self.definitions
  @definitions ||= []
end

.inherited(plugin) ⇒ Object

Inherited callback to set a class-level instance variable on the subclass, since we can’t use __FILE__ here, and have it use the subclass’s location.



22
23
24
# File 'lib/cognition/plugins/base.rb', line 22

def self.inherited(plugin)
  plugin.view_root = File.dirname(caller[0].split(":", 2).first)
end

.match(trigger, action, options = {}) ⇒ Object



35
36
37
# File 'lib/cognition/plugins/base.rb', line 35

def self.match(trigger, action, options = {})
  definitions << [trigger, action, options]
end

Instance Method Details

#render(opts = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/cognition/plugins/base.rb', line 43

def render(opts = {})
  options = RENDER_DEFAULTS.merge(opts)
  calling_method = caller[0][/`([^']*)'/, 1]
  template = options[:template] || template_file(calling_method, options[:type], options[:extension])
  Tilt.new(template).render(self, options[:locals])
rescue Errno::ENOENT => e
  raise PluginTemplateNotFound, e
end