Class: Cinch::Plugins::YamlKeywords
- Inherits:
-
Object
- Object
- Cinch::Plugins::YamlKeywords
- Includes:
- Cinch::Plugin
- Defined in:
- lib/cinch/plugins/yamlkeywords.rb
Instance Method Summary collapse
- #find_keyword(m) ⇒ Object
-
#initialize(*args) ⇒ YamlKeywords
constructor
A new instance of YamlKeywords.
- #keyword_check(m, keyword) ⇒ Object
- #keyword_define(m, keyword, definition) ⇒ Object
- #keyword_forget(m, keyword) ⇒ Object
- #keywords(m) ⇒ Object
- #listen(m) ⇒ Object
- #update_store ⇒ Object
Constructor Details
#initialize(*args) ⇒ YamlKeywords
Returns a new instance of YamlKeywords.
8 9 10 11 12 13 14 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 8 def initialize(*args) super if File.exist?('keywords.yaml') then @keywords = YAML.load_file('keywords.yaml') else @keywords = {} end end |
Instance Method Details
#find_keyword(m) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 56 def find_keyword(m) @keywords.keys.map{|keyword| Regexp.new(keyword).match(m.) }.compact.sort_by{|matchdata| matchdata.begin(0) }.each{|matchdata| m.reply @keywords[matchdata[0]] } end |
#keyword_check(m, keyword) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 37 def keyword_check(m, keyword) if @keywords[keyword] then m.reply "'#{keyword}' is defined as: '#{@keywords[keyword]}'." else m.reply "'#{keyword}' is not defined." end end |
#keyword_define(m, keyword, definition) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 28 def keyword_define(m, keyword, definition) if @keywords[keyword] then m.reply "Redefining '#{keyword}' from: '#{@keywords[keyword]}' to: '#{definition}'." else m.reply "Defining '#{keyword}' with: '#{definition}'." end @keywords[keyword] = definition update_store end |
#keyword_forget(m, keyword) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 44 def keyword_forget(m, keyword) if @keywords[keyword] then m.reply "'#{keyword}' was defined as: '#{@keywords[keyword]}'." @keywords.delete(keyword) update_store else m.reply "'#{keyword}' is not defined." end end |
#keywords(m) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 16 def keywords(m) if @keywords.size > 0 then m.reply "Keywords start:" @keywords.each{|k,v| m.reply "'#{k}': '#{v}'." } m.reply "Keywords end." else m.reply "No keywords defined yet." end end |
#listen(m) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 67 def listen(m) case m. when /^!keywords/ keywords(m) when /^!keyword '([^']+)' (.+)$/, /^!keyword "([^"]+)" (.+)$/, /^!keyword (\S+) (.+)$/ keyword_define(m, $1, $2) when /^!keyword (\S+)$/, /^!keyword\? (.+)$/ keyword_check(m, $1) when /^!forget (.+)$/ keyword_forget(m, $1) else find_keyword(m) end end |
#update_store ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/cinch/plugins/yamlkeywords.rb', line 83 def update_store synchronize(:update) do File.open('keywords.yaml', 'w') do |fh| YAML.dump(@keywords, fh) end end end |