Class: Chatterbot::Skeleton

Inherits:
Object
  • Object
show all
Defined in:
lib/chatterbot/skeleton.rb

Overview

bot template generator

Class Method Summary collapse

Class Method Details

.apply_vars(s, args) ⇒ Object

handle string interpolation in ruby 1.8. modified from raw.github.com/svenfuchs/i18n/master/lib/i18n/core_ext/string/interpolate.rb

:nocov:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chatterbot/skeleton.rb', line 33

def apply_vars(s, args)
  pattern = Regexp.union(
                                       /%\{(\w+)\}/,                               # matches placeholders like "%{foo}"
                                       /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/  # matches placeholders like "%<foo>.d"
                                       )
  
  pattern_with_escape = Regexp.union(
                                                   /%%/,
                                                   pattern
                                                   )
  
  s.gsub(pattern_with_escape) do |match|
    if match == '%%'
      '%'
    else
      key = ($1 || $2).to_sym
      raise KeyError unless args.has_key?(key)
      $3 ? sprintf("%#{$3}", args[key]) : args[key]
    end
  end
end

.generate(bot) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chatterbot/skeleton.rb', line 7

def generate(bot)
  path = File.join(Chatterbot.libdir, "..", "templates", "skeleton.txt")
  src = File.read(path)
  puts bot.config.inspect
  opts = bot.config.merge({
    :name => bot.botname,
    :timestamp => Time.now
  })

  puts opts.inspect
  
  if RUBY_VERSION =~ /^1\.8\./
    #:nocov:
    apply_vars(src, opts)
    #:nocov:

  else
    src % opts
  end
end