Class: Gopher::Application

Inherits:
Object show all
Defined in:
lib/gopher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Application

Returns a new instance of Application.



17
18
19
20
# File 'lib/gopher.rb', line 17

def initialize(&block)
  reset!
  self.instance_eval(&block)
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



15
16
17
# File 'lib/gopher.rb', line 15

def root
  @root
end

#selectorsObject

Returns the value of attribute selectors.



15
16
17
# File 'lib/gopher.rb', line 15

def selectors
  @selectors
end

Instance Method Details

#add_handler(selector, handler) ⇒ Object



61
62
63
64
65
# File 'lib/gopher.rb', line 61

def add_handler(selector, handler)
  selector = Gopher.sanitize_selector(selector)
  selector.sub!(/^\/*/, '')
  selectors[/^\/?#{selector}$/] = handler
end

#application(selector, handler) ⇒ Object



45
46
47
# File 'lib/gopher.rb', line 45

def application(selector, handler)
  add_handler selector, handler
end

#helpers(&block) ⇒ Object



40
41
42
43
# File 'lib/gopher.rb', line 40

def helpers(&block)
  MapContext.class_eval(&block)
  TextContext.class_eval(&block)
end

#lookup(selector) ⇒ Object

Raises:



54
55
56
57
58
59
# File 'lib/gopher.rb', line 54

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



32
33
34
# File 'lib/gopher.rb', line 32

def map(selector, &block)
  add_handler selector.gsub(/:(.+)/, '(.+)'), MapHandler.new(&block).with(self)
end

#mount(selector, path) ⇒ Object



28
29
30
# File 'lib/gopher.rb', line 28

def mount(selector, path)
  add_handler "#{selector}/?(.*)", DirectoryHandler.new(path, selector).with(self)
end

#reload(*f) ⇒ Object



22
# File 'lib/gopher.rb', line 22

def reload(*f); Gopher.reload(*f) end

#request(selector) ⇒ Object



49
50
51
52
# File 'lib/gopher.rb', line 49

def request(selector)
  handler, *args = lookup(selector)
  handler.call(*args)
end

#text(selector, &block) ⇒ Object



36
37
38
# File 'lib/gopher.rb', line 36

def text(selector, &block)
  add_handler selector.gsub(/:(.+)/, '(.+)'), TextHandler.new(&block).with(self)
end

#use(mod) ⇒ Object



24
25
26
# File 'lib/gopher.rb', line 24

def use(mod)
  extend(mod)
end