Class: Dolphy::Core

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
TemplateEngines
Defined in:
lib/dolphy/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Core

Returns a new instance of Core.



18
19
20
21
22
# File 'lib/dolphy/core.rb', line 18

def initialize(&block)
  @router   = Dolphy::Router.new
  @settings = Dolphy::Settings.new
  instance_eval(&block)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



16
17
18
# File 'lib/dolphy/core.rb', line 16

def settings
  @settings
end

Instance Method Details

#call(env) ⇒ Object

The main logic of Dolphy nests inside the call(env) method. It looks up the route for the current request method in the routes hash, and if it finds a route that matches the current path, it evaluates the block and sets the response accordingly.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dolphy/core.rb', line 49

def call(env)
  @request  = Dolphy::Request.new(env)
  @response = Dolphy::Response.new

  router.find_route_for(request).each do |matcher, block|
    if match = router.find_match_data_for(request, with: matcher)
      response.body = [block.call(*match.captures)]
    end
  end

  response.status = 404 if response.body.empty?
  response.body << "Page not found." if response.body.empty?
  response.finish
end

#paramsObject



41
42
43
# File 'lib/dolphy/core.rb', line 41

def params
  request.params
end

#redirect_to(path, status = 302) ⇒ Object



36
37
38
39
# File 'lib/dolphy/core.rb', line 36

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

#render(template_name, locals = {}) ⇒ Object



32
33
34
# File 'lib/dolphy/core.rb', line 32

def render(template_name, locals = {})
  template_engine.render(template_name, locals)
end

#serve!Object



24
25
26
# File 'lib/dolphy/core.rb', line 24

def serve!
  Rack::Server.start(app: self)
end

#setup(&block) ⇒ Object



28
29
30
# File 'lib/dolphy/core.rb', line 28

def setup(&block)
  instance_eval(&block)
end