Class: Roy::Context

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

Overview

Application context for Roy applications.

Everything must be namespaced in this context to avoid any clashes and to make the code cleaner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Context

Creates a new Context object.

Parameters:

  • app

    the context’s application



31
32
33
34
35
36
37
38
# File 'lib/roy/context.rb', line 31

def initialize(app)
  @app  = app
  @conf = app.class.conf

  app.class.ancestors.reverse.each do |mod|
    mod.setup(self) if mod.respond_to?(:setup)
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the current application



8
9
10
# File 'lib/roy/context.rb', line 8

def app
  @app
end

#confObject (readonly)

Returns the application’s configuration



11
12
13
# File 'lib/roy/context.rb', line 11

def conf
  @conf
end

#envObject (readonly)

Returns the environment passed to #call



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

def env
  @env
end

#headersObject (readonly)

Returns the current response’s headers



23
24
25
# File 'lib/roy/context.rb', line 23

def headers
  @headers
end

#paramsObject (readonly)

Returns the current request’s params



26
27
28
# File 'lib/roy/context.rb', line 26

def params
  @params
end

#requestObject (readonly)

Returns the current request



17
18
19
# File 'lib/roy/context.rb', line 17

def request
  @request
end

#responseObject (readonly)

Returns the current response



20
21
22
# File 'lib/roy/context.rb', line 20

def response
  @response
end

Instance Method Details

#prepare!(env) ⇒ Object

Initializes the attributes based on an environment.

Parameters:

  • env

    the environment to use



43
44
45
46
47
48
49
50
51
52
# File 'lib/roy/context.rb', line 43

def prepare!(env)
  @env      = env
  @request  = Rack::Request.new(env)
  @response = Rack::Response.new
  @headers  = @response.header
  @params   = @request.GET.merge(@request.POST)
  @params.default_proc = proc do |hash, key|
    hash[key.to_s] if Symbol === key
  end
end