class Cognition::Plugins::Base

Constants

RENDER_DEFAULTS

Attributes

view_root[RW]
bot[RW]
matchers[RW]

Public Class Methods

definitions() click to toggle source
# File lib/cognition/plugins/base.rb, line 39
def self.definitions
  @definitions ||= []
end
inherited(plugin) click to toggle source

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.

# 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 = {}) click to toggle source
# File lib/cognition/plugins/base.rb, line 35
def self.match(trigger, action, options = {})
  definitions << [trigger, action, options]
end
new(bot = nil) click to toggle source
# 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

Public Instance Methods

render(opts = {}) click to toggle source
# 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

Private Instance Methods

template_file(name, type, extension) click to toggle source
# File lib/cognition/plugins/base.rb, line 54
def template_file(name, type, extension)
  # Defaults to html ERB for now. Override when calling #render(path_to_file)
  File.join(templates_path, "#{name}.#{type}.#{extension}")
end
templates_path() click to toggle source
# File lib/cognition/plugins/base.rb, line 68
def templates_path
  File.expand_path("#{underscore(self.class.name)}/views", self.class.view_root)
end
underscore(string) click to toggle source
# File lib/cognition/plugins/base.rb, line 59
def underscore(string)
  word = string.to_s.gsub(/::/, "/")
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, "\\1_\\2")
  word.gsub!(/([a-z\d])([A-Z])/, "\\1_\\2")
  word.tr!("-", "_")
  word.downcase!
  word
end