Class: Nyane

Inherits:
Object
  • Object
show all
Defined in:
lib/nyane.rb,
lib/nyane/erb.rb,
lib/nyane/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Nyane

Returns a new instance of Nyane.



7
8
9
10
11
12
# File 'lib/nyane.rb', line 7

def initialize(&block)
  @actions = []
  @root = File.dirname(eval("__FILE__", block.binding))
  @app = self
  instance_eval(&block)
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/nyane.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/nyane.rb', line 5

def response
  @response
end

#views_directoryObject

Returns the value of attribute views_directory.



3
4
5
# File 'lib/nyane/renderer.rb', line 3

def views_directory
  @views_directory
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nyane.rb', line 32

def call(env)
  @request = Rack::Request.new(env)
  @response = Rack::Response.new
  
  @params = @request.params
  path_info = nil
  action = @actions.detect { |route, method, block| @request.request_method == method.to_s.upcase! && path_info = @request.path_info.match(Regexp.new("^\/?#{route}\/?$")) }

  if action
    @response.write(action.last.call(path_info[1..-1]))
  else
    @response.write("Not found")
    @response.status = 404
  end

  @response.finish
end

#erb(template, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/nyane/erb.rb', line 11

def erb(template, options={})
  file = read_template_file(:erb, template)
  layout = read_layout_file(:erb, options)

  result = process(file)
  result = process(layout) { result } if layout

  result
end

#get(route, &block) ⇒ Object



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

def get(route, &block)
  @actions << [route, :get, block]
end

#load(file) ⇒ Object



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

def load(file)
  path = File.join(@root, file) + ".rb"
  eval(File.read(path), binding, path)
end

#post(route, &block) ⇒ Object



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

def post(route, &block)
  @actions << [route, :post, block]
end

#redirect_to(path) ⇒ Object



22
23
24
25
# File 'lib/nyane.rb', line 22

def redirect_to(path)
  @response.status = 302
  @response.headers["Location"] = path
end