Class: Roy::Context
- Inherits:
-
Object
- Object
- Roy::Context
- 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
-
#app ⇒ Object
readonly
Returns the current application.
-
#conf ⇒ Object
readonly
Returns the application’s configuration.
-
#env ⇒ Object
readonly
Returns the environment passed to #call.
-
#headers ⇒ Object
readonly
Returns the current response’s headers.
-
#params ⇒ Object
readonly
Returns the current request’s params.
-
#request ⇒ Object
readonly
Returns the current request.
-
#response ⇒ Object
readonly
Returns the current response.
Instance Method Summary collapse
-
#initialize(app) ⇒ Context
constructor
Creates a new Context object.
-
#prepare!(env) ⇒ Object
Initializes the attributes based on an environment.
Constructor Details
#initialize(app) ⇒ Context
Creates a new Context object.
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
#app ⇒ Object (readonly)
Returns the current application
8 9 10 |
# File 'lib/roy/context.rb', line 8 def app @app end |
#conf ⇒ Object (readonly)
Returns the application’s configuration
11 12 13 |
# File 'lib/roy/context.rb', line 11 def conf @conf end |
#env ⇒ Object (readonly)
Returns the environment passed to #call
14 15 16 |
# File 'lib/roy/context.rb', line 14 def env @env end |
#headers ⇒ Object (readonly)
Returns the current response’s headers
23 24 25 |
# File 'lib/roy/context.rb', line 23 def headers @headers end |
#params ⇒ Object (readonly)
Returns the current request’s params
26 27 28 |
# File 'lib/roy/context.rb', line 26 def params @params end |
#request ⇒ Object (readonly)
Returns the current request
17 18 19 |
# File 'lib/roy/context.rb', line 17 def request @request end |
#response ⇒ Object (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.
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 |