Class: Cogibara::Dispatcher
- Inherits:
-
Object
- Object
- Cogibara::Dispatcher
- Defined in:
- lib/cogibara/dispatcher.rb
Instance Method Summary collapse
- #call(keyword, message) ⇒ Object
- #call_file(keyword, file) ⇒ Object
- #config_from_yaml(yml) ⇒ Object
- #file_operator(keyword) ⇒ Object
- #file_operators ⇒ Object
- #operator(keyword) ⇒ Object
- #operator_options ⇒ Object
- #operators ⇒ Object
- #register_operator(keywords, options) ⇒ Object
- #registered?(keyword) ⇒ Boolean
- #registered_file?(keyword) ⇒ Boolean
Instance Method Details
#call(keyword, message) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cogibara/dispatcher.rb', line 59 def call(keyword,) begin if operators[keyword].is_a? Cogibara::OperatorBase response = operator(keyword).() else response = operator(keyword).process() end rescue Exception puts "an error occured! " + ($!).to_s Cogibara::say(Cogibara::Message.new("** O Noez, an error has occured in module #{keyword}; #{($!).to_s} **",.clientID)) nil end # if response.is_a? Hash # if response[:code] # if response[:code] == :confirm # # Cogibara::Responder.new.send_reply(response[:text],message.clientID) # Cogibara::confirmer.add_action(response[:message], response[:success]) # response[:message] # else # response # end # else # response # end # else response # end end |
#call_file(keyword, file) ⇒ Object
88 89 90 |
# File 'lib/cogibara/dispatcher.rb', line 88 def call_file(keyword, file) response = operator(keyword).process_file(file) end |
#config_from_yaml(yml) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cogibara/dispatcher.rb', line 101 def config_from_yaml(yml) yml = YAML.load_file(yml) if yml.is_a? String yml["modules"].each do |mod| mod_name = mod["module_name"] mod_keywords = mod["keywords"] mod_file = mod["file_name"] ? mod["file_name"] : mod["module_name"].downcase.gsub(' ','_') mod_class_name = mod["class_name"] ? mod["class_name"] : mod_file.split('_').map{ |w| w.capitalize }.join # mod_file += ".rb" if mod_keywords.is_a? Array register_operator(mod_keywords, {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod}) else register_operator([mod_keywords], {name: mod_name, file_name: mod_file, class_name: mod_class_name, config: mod}) end end end |
#file_operator(keyword) ⇒ Object
21 22 23 24 |
# File 'lib/cogibara/dispatcher.rb', line 21 def file_operator(keyword) file_operators[keyword] = file_operators[keyword].new([operators[keyword]]) if file_operators[keyword].is_a? Class file_operators[keyword] end |
#file_operators ⇒ Object
8 9 10 |
# File 'lib/cogibara/dispatcher.rb', line 8 def file_operators @file_operators ||= Hash.new end |
#operator(keyword) ⇒ Object
16 17 18 19 |
# File 'lib/cogibara/dispatcher.rb', line 16 def operator(keyword) operators[keyword] = operators[keyword].new([operators[keyword]]) if operators[keyword].is_a? Class operators[keyword] end |
#operator_options ⇒ Object
12 13 14 |
# File 'lib/cogibara/dispatcher.rb', line 12 def @operator_options ||= Hash.new end |
#operators ⇒ Object
4 5 6 |
# File 'lib/cogibara/dispatcher.rb', line 4 def operators @operators ||= Hash.new end |
#register_operator(keywords, options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cogibara/dispatcher.rb', line 26 def register_operator(keywords, ) mod_keywords = keywords mod_name = [:name] || keywords[0] mod_file = [:file_name] ? [:file_name] : mod_name.downcase.gsub(' ','_') mod_class_name = [:class_name] ? [:class_name] : mod_file.split('_').map{ |w| w.capitalize }.join mod_file += ".rb" loc = File.(File.dirname(__FILE__)) require "#{loc}/operators/#{mod_file}" if File.exists?("#{loc}/operators/#{mod_file}") if Object.const_defined?(mod_class_name) mod_class = eval(mod_class_name) ## TODO prooooooobably should do this a different way unless (mod_class.method_defined?("process") || mod_class.method_defined?("process_file")) puts "process method missing for #{mod_class.to_s} in #{mod_name}" else [mod_class] = [:config] || {} end if mod_class.method_defined?("process") mod_keywords.each do |kw| operators[kw] = mod_class end end if mod_class.method_defined?("process_file") mod_keywords.each do |kw| file_operators[kw] = mod_class end end else puts "main class: #{mod_class_name} not defined for module #{mod_name}" end puts "file: #{loc}/operators/#{mod_file} for module #{mod_name} does not exist, skipping load" if [:file_name] && !File.exists?("#{loc}/operators/#{mod_file}") end |
#registered?(keyword) ⇒ Boolean
92 93 94 |
# File 'lib/cogibara/dispatcher.rb', line 92 def registered?(keyword) operators.has_key?(keyword) end |
#registered_file?(keyword) ⇒ Boolean
96 97 98 99 |
# File 'lib/cogibara/dispatcher.rb', line 96 def registered_file?(keyword) puts "looking for #{keyword} in #{file_operators.keys}" file_operators.has_key?(keyword) end |