Class: Femto::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/femto.rb

Defined Under Namespace

Classes: TemplateMissingException

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.content_typeObject

Returns the value of attribute content_type.



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

def content_type
  @content_type
end

.envObject

Returns the value of attribute env.



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

def env
  @env
end

.layout(options = {}, &block) ⇒ Object

Returns the value of attribute layout.



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

def layout
  @layout
end

.layout_blockObject

Returns the value of attribute layout_block.



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

def layout_block
  @layout_block
end

.render(options) ⇒ Object

Returns the value of attribute render.



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

def render
  @render
end

.requestObject

Returns the value of attribute request.



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

def request
  @request
end

.responseObject

Returns the value of attribute response.



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

def response
  @response
end

.routesObject

Returns the value of attribute routes.



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

def routes
  @routes
end

.template_dirObject

Returns the value of attribute template_dir.



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

def template_dir
  @template_dir
end

Class Method Details

.builderObject



104
105
106
107
# File 'lib/femto.rb', line 104

def builder
  @builder = Rack::Builder.new unless @builder
  @builder
end

.call(env) ⇒ Object



109
110
111
# File 'lib/femto.rb', line 109

def call(env)
  dup.call! env
end

.call!(env) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/femto.rb', line 113

def call!(env)
  env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
  @request = Rack::Request.new env
  @response = Rack::Response.new
  @render = nil
  @content_type = nil
  @env = env
  handle_request
  @response.finish
end

.handle_requestObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/femto.rb', line 38

def handle_request
  catch :stop do
    verb = request.request_method.downcase
    request_path = request.path_info
    routes = @routes[verb]
    if routes
      routes.each do |group|
        path = group[0]
        options = group[1]
        block = group[2]
        if request_path == path
          instance_eval(&block)
          if options[:template]
            render options[:template]
          elsif options[:view]
            render options[:view]
          end
          if @layout and @content_type == 'text/html'
            if @layout_block
              @layout_block.call
            end
            @render = render_template(@layout) { @render }
          end
          if @render == nil
            raise TemplateMissingException.new
          end
          response.write @render
          response['Content-Type'] = content_type
          return
        end
      end
    end
    stop
  end
end

.model(name, &block) ⇒ Object



87
88
89
# File 'lib/femto.rb', line 87

def model(name, &block)
  Femto::Model.create_model name.to_s, &block
end

.render_template(template, &block) ⇒ Object



95
96
97
98
# File 'lib/femto.rb', line 95

def render_template(template, &block)
  @content_type = 'text/html'
  Tilt.new(template).render self, {}, &block
end

.resolve_template(file) ⇒ Object



91
92
93
# File 'lib/femto.rb', line 91

def resolve_template(file)
  Dir[File.join(@template_dir, file + '.*')][0]
end

.set_route(verb, path, options, block) ⇒ Object



32
33
34
35
36
# File 'lib/femto.rb', line 32

def set_route(verb, path, options, block)
  @routes = {} unless @routes
  @routes[verb] = [] unless @routes[verb]
  @routes[verb] << [path, options, block]
end

.stopObject



100
101
102
# File 'lib/femto.rb', line 100

def stop
  throw :stop
end