Class: Gopher::Application
- Inherits:
-
Object
- Object
- Gopher::Application
- Defined in:
- lib/gopher.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
-
#selectors ⇒ Object
Returns the value of attribute selectors.
Instance Method Summary collapse
- #add_handler(selector, handler) ⇒ Object
- #app(selector, handler) ⇒ Object (also: #application)
- #helpers(&block) ⇒ Object
-
#initialize(&block) ⇒ Application
constructor
A new instance of Application.
- #lookup(selector) ⇒ Object
- #map(selector, &block) ⇒ Object
- #mount(selector, path) ⇒ Object
- #reload(*f) ⇒ Object
- #request(selector) ⇒ Object
- #text(selector, &block) ⇒ Object
- #use(mod) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Application
Returns a new instance of Application.
15 16 17 18 |
# File 'lib/gopher.rb', line 15 def initialize(&block) reset! self.instance_eval(&block) end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
13 14 15 |
# File 'lib/gopher.rb', line 13 def root @root end |
#selectors ⇒ Object
Returns the value of attribute selectors.
13 14 15 |
# File 'lib/gopher.rb', line 13 def selectors @selectors end |
Instance Method Details
#add_handler(selector, handler) ⇒ Object
63 64 65 66 67 |
# File 'lib/gopher.rb', line 63 def add_handler(selector, handler) selector = Gopher.sanitize_selector(selector) selector.sub!(/^\/*/, '') selectors[/^\/?#{selector}$/] = handler end |
#app(selector, handler) ⇒ Object Also known as: application
46 47 48 |
# File 'lib/gopher.rb', line 46 def app(selector, handler) add_handler selector, handler end |
#helpers(&block) ⇒ Object
41 42 43 44 |
# File 'lib/gopher.rb', line 41 def helpers(&block) MapContext.class_eval(&block) TextContext.class_eval(&block) end |
#lookup(selector) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/gopher.rb', line 56 def lookup(selector) selectors.find do |k, v| return v, *$~[1..-1] if k =~ Gopher.sanitize_selector(selector) end raise NotFound end |
#map(selector, &block) ⇒ Object
31 32 33 34 |
# File 'lib/gopher.rb', line 31 def map(selector, &block) add_handler selector.gsub(/:(.+)/, '(.+)'), MapHandler.new(&block).with(self) end |
#mount(selector, path) ⇒ Object
26 27 28 29 |
# File 'lib/gopher.rb', line 26 def mount(selector, path) add_handler "#{selector}/?(.*)", DirectoryHandler.new(path, selector).with(self) end |
#request(selector) ⇒ Object
51 52 53 54 |
# File 'lib/gopher.rb', line 51 def request(selector) handler, *args = lookup(selector) handler.call(*args) end |
#text(selector, &block) ⇒ Object
36 37 38 39 |
# File 'lib/gopher.rb', line 36 def text(selector, &block) add_handler selector.gsub(/:(.+)/, '(.+)'), TextHandler.new(&block).with(self) end |
#use(mod) ⇒ Object
22 23 24 |
# File 'lib/gopher.rb', line 22 def use(mod) extend(mod) end |