Class: Www::Base

Inherits:
Object
  • Object
show all
Includes:
View
Defined in:
lib/www/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from View

included, #render

Constructor Details

#initialize(request = nil) ⇒ Base

Returns a new instance of Base.



80
81
82
83
# File 'lib/www/base.rb', line 80

def initialize(request = nil)
  @request = request
  @response = Rack::Response.new
end

Class Method Details

.adjust_args_for(args, arity) ⇒ Object



75
76
77
# File 'lib/www/base.rb', line 75

def adjust_args_for(args, arity)
  args = args[0, arity] if arity >= 0
end

.before(*route_names, &block) ⇒ Object



21
22
23
# File 'lib/www/base.rb', line 21

def before(*route_names, &block)
  before_blocks << [route_names.map{ |i| i.to_sym }, block]
end

.before_blocksObject



25
26
27
# File 'lib/www/base.rb', line 25

def before_blocks
  @before_blocks ||= []
end

.call(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/www/base.rb', line 61

def call(env)
  request = Rack::Request.new(env)
  route, match = find_route(request.path_info, request.request_method)
  if route
    handler = route.clazz.new(request)
    args = match[1..-1] << request.params
    args = adjust_args_for(args, handler.method(:"www_#{route.name}").arity)
    # puts "#{route.clazz}##{route.name}(#{args.map{|i| "'#{i}'"}.join(', ')})"
    handler.send(route.name, *args)
  else
    error 404
  end
end

.delete(pattern) ⇒ Object



19
# File 'lib/www/base.rb', line 19

def delete(pattern) _(pattern, :delete) end

.error(code) ⇒ Object



52
53
54
# File 'lib/www/base.rb', line 52

def error(code)
  [code, {"Content-Type" => "text/html"}, []]
end

.find_route(path, request_method) ⇒ Object



56
57
58
59
# File 'lib/www/base.rb', line 56

def find_route(path, request_method)
  [routes.detect { |route|
      path.match(route.pattern) && route.request_methods.include?(request_method.downcase.to_sym) }, $~]
end

.get(pattern) ⇒ Object



16
# File 'lib/www/base.rb', line 16

def get(pattern)    _(pattern, :get)    end

.method_added(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/www/base.rb', line 29

def method_added(name)
  return unless @current_route
  @current_route.name = name
  routes << @current_route
  @current_route = nil

  class_eval do
    alias_method :"www_#{name}", name
    define_method name do |*args|
      self.class.before_blocks.each do |route_names, block|
        if route_names.empty? || route_names.include?(name)
          instance_eval(&block)
        end
      end

      @route_name = name
      body = send(:"www_#{name}", *args) || ''
      body = body.inspect unless body.is_a?(String)
      [@response.status, @response.header, [body].flatten]
    end
  end
end

.post(pattern) ⇒ Object



17
# File 'lib/www/base.rb', line 17

def post(pattern)   _(pattern, :post)   end

.put(pattern) ⇒ Object



18
# File 'lib/www/base.rb', line 18

def put(pattern)    _(pattern, :put)    end

.route(pattern, methods = [:get]) ⇒ Object Also known as: _



11
12
13
# File 'lib/www/base.rb', line 11

def route(pattern, methods = [:get])
  @current_route = Route.new(pattern, methods, self)
end

.routesObject



7
8
9
# File 'lib/www/base.rb', line 7

def routes
  @@routes ||= []
end