Class: Leech::Handler Abstract
- Inherits:
-
Object
- Object
- Leech::Handler
- Defined in:
- lib/leech/handler.rb
Overview
Handlers are extending functionality of server by defining custom command handling callbacks or server instance methods.
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#env ⇒ Leecher::Server
readonly
Passed server instance.
-
#params ⇒ Array<String>
readonly
Parameters matched in passed command.
Class Method Summary collapse
-
.handle(pattern, method = nil, &block) ⇒ Object
It defines matching pattern and callback related with.
-
.matchers ⇒ Object
List of declared matchers.
-
.used(server) ⇒ Object
You should implement this method in your handler eg.
Instance Method Summary collapse
-
#call ⇒ Object
This method can be called only after #match.
-
#initialize(env) ⇒ Handler
constructor
Constructor.
-
#match(command) ⇒ Leech::Handler?
Compare specified command with declared patterns.
Constructor Details
#initialize(env) ⇒ Handler
Constructor.
81 82 83 |
# File 'lib/leech/handler.rb', line 81 def initialize(env) @env = env end |
Instance Attribute Details
#env ⇒ Leecher::Server (readonly)
Returns Passed server instance.
71 72 73 |
# File 'lib/leech/handler.rb', line 71 def env @env end |
#params ⇒ Array<String> (readonly)
Returns Parameters matched in passed command.
75 76 77 |
# File 'lib/leech/handler.rb', line 75 def params @params end |
Class Method Details
.handle(pattern, method = nil, &block) ⇒ Object
It defines matching pattern and callback related with. When specified command will match this pattern then callback will be called.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/leech/handler.rb', line 52 def self.handle(pattern, method=nil, &block) if block_given? method = block end if method.is_a?(Proc) || method.is_a?(Symbol) self.matchers[pattern] = method else raise Error, "Invalid handler callback" end end |
.matchers ⇒ Object
List of declared matchers
32 33 34 |
# File 'lib/leech/handler.rb', line 32 def self.matchers @matchers ||= {} end |
.used(server) ⇒ Object
You should implement this method in your handler eg. if you would like to modify server class.
65 66 67 |
# File 'lib/leech/handler.rb', line 65 def self.used(server) # nothing... end |
Instance Method Details
#call ⇒ Object
This method can be called only after #match. It executes callback related with matched pattern.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/leech/handler.rb', line 109 def call case @matcher when Proc @matcher.call(env, params) when String, Symbol env.send(@matcher.to_sym, params) else raise Error, "Can not call unmatched command" end end |
#match(command) ⇒ Leech::Handler?
Compare specified command with declared patterns.
93 94 95 96 97 98 99 100 101 |
# File 'lib/leech/handler.rb', line 93 def match(command) self.class.matchers.each_pair do |p,m| if @params = p.match(command) @matcher = m return self end end nil end |