Class: Gopher::Application

Inherits:
Object
  • 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.



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

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

Instance Attribute Details

#rootObject

Returns the value of attribute root.



13
14
15
# File 'lib/gopher.rb', line 13

def root
  @root
end

#selectorsObject

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

Raises:



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

#reload(*f) ⇒ Object



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

def reload(*f); Gopher.reload(*f) 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