Class: Gnarly::Application

Inherits:
Object
  • Object
show all
Includes:
Status
Defined in:
lib/gnarly/haml.rb,
lib/gnarly/system.rb,
lib/gnarly/application.rb

Instance Method Summary collapse

Methods included from Status

#created, #default_content_type, #internal_server_error, #method_not_allowed, #not_acceptable, #not_found, #ok, #pop_default_content_type, #push_default_content_type, #unsupported_media_type

Constructor Details

#initializeApplication

Returns a new instance of Application.



9
10
11
12
# File 'lib/gnarly/application.rb', line 9

def initialize()
  @urls = []
  @environment = Environment.new self
end

Instance Method Details

#+(other) ⇒ Object



29
30
31
# File 'lib/gnarly/system.rb', line 29

def +(other)
  System.new self, other
end

#call(state) ⇒ Object



54
55
56
57
58
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
87
# File 'lib/gnarly/application.rb', line 54

def call(state)
  environment = @environment.dup
  environment.extend *@helpers if @helpers
  environment.state state
  request = environment.request
  path = request.path
  if url = @urls.detect { |u| u.match? path }
    @responder = url.responder request.method
    if @responder
      helpers = url.helpers
      environment.extend *helpers if helpers
      if before = url.before
        environment.instance_exec *url.parameters, &before
      end
      response = environment.instance_exec *url.parameters(request.method), &@responder
      @response = environment.response ? environment.response : response
      if after = url.after
        environment.instance_exec *url.parameters, &after
      end
      if @response.is_a? Array and @response.size == 3
        @response[2] = [@response[2]] unless @response[2].respond_to? :each
        @response
      else
        name, line = @responder.source_location
        msg = "Bad return type for block in  #{name} line #{line}"
        internal_server_error msg
      end
    else
      method_not_allowed url.allow
    end
  else
    not_found
  end
end

#delete(*args, &block) ⇒ Object



48
49
50
51
52
# File 'lib/gnarly/application.rb', line 48

def delete(*args, &block)
  m = args.shift
  u = url m
  u.delete *args, &block
end

#get(*args, &block) ⇒ Object



30
31
32
33
34
# File 'lib/gnarly/application.rb', line 30

def get(*args, &block)
  m = args.shift
  u = url m
  u.get *args, &block
end

#haml(file, url_mask = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/gnarly/haml.rb', line 35

def haml(file, url_mask=nil)
  url_mask = "/#{file}" unless url_mask
  url url_mask do
    get do
      provides "text/html" do
        ok haml(file)
      end
    end
  end
end

#haml_root(haml_root = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/gnarly/haml.rb', line 27

def haml_root(haml_root=nil)
  if haml_root
    @haml_root = haml_root
  else
    @haml_root ||= "haml"
  end
end

#helper(helper = nil, &block) ⇒ Object



25
26
27
28
# File 'lib/gnarly/application.rb', line 25

def helper(helper=nil, &block)
  helper = Module.new &block if block and not helper
  (@helpers ||= []) << helper
end

#init(&block) ⇒ Object



14
15
16
# File 'lib/gnarly/application.rb', line 14

def init(&block)
  @environment.instance_exec &block
end

#post(*args, &block) ⇒ Object



42
43
44
45
46
# File 'lib/gnarly/application.rb', line 42

def post(*args, &block)
  m = args.shift
  u = url m
  u.post *args, &block
end

#put(*args, &block) ⇒ Object



36
37
38
39
40
# File 'lib/gnarly/application.rb', line 36

def put(*args, &block)
  m = args.shift
  u = url m
  u.put *args, &block
end

#url(url_mask, &block) ⇒ Object



18
19
20
21
22
23
# File 'lib/gnarly/application.rb', line 18

def url(url_mask, &block)
  url = Url.new(url_mask)
  url.instance_eval &block if block
  @urls << url
  url
end