Ambito

The scope you are currently in determines what methods and variables are available.

Ámbito de Clase/Class Scope

  1. Every Sinatra application corresponds to a subclass of Sinatra::Base
  2. If you are using the top-level DSL (require 'sinatra'), then this class is Sinatra::Application, otherwise it is the subclass you created explicitly
  3. At class level you have methods like get or before, but you cannot access the request or session objects, as there is only a single application class for all requests

Options created via set are methods at class level:

class MyApp < Sinatra::Base
  # Hey, I'm in the application scope!
  set :foo, 42
  foo # => 42

  get '/foo' do
    # Hey, I'm no longer in the application scope!
  end
end

You have the application scope binding inside:

  1. Your application class body
  2. Methods defined by extensions
  3. The block passed to helpers
  4. Procs/blocks used as value for set
  5. The block passed to Sinatra.new

You can reach the scope object (the class) like this:

  1. Via the object passed to configure blocks (configure { |c| ... })
  2. settings from within the request scope

Ámbito de Instancia/Instance Scope

For every incoming request, a new instance of your application class is created and all handler blocks run in that scope

  1. From within this scope you can access the request and session objects or
  2. call rendering methods like erb or haml
  3. You can access the application scope from within the request scope via the settings helper:

[~/sinatra/sinatra-scope]$ cat app.rb 
require 'sinatra'

class MyApp < Sinatra::Base
  # Hey, I'm in the application scope!
  get '/define_route/:name' do
    # Request scope for '/define_route/:name'
    @value = 42
    puts "Inside /define_route/:name @value = #{@value}"
    puts self.class

    settings.get("/#{params[:name]}") do
      # Request scope for "/#{params[:name]}"
      puts "@value = <#{@value}>"
      "Inside defined route #{params[:name]}"
    end

    "Route #{params[:name]} defined!"
  end
  
  run! if __FILE__ == $0
end

Ejecución en el servidor

[~/sinatra/sinatra-scope]$ ruby app.rb 
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
Inside /define_route/:name @value = 42
MyApp
@value = <>

Ejecución en el cliente. Ruta: /define_route/juan

[~/sinatra/sinatra-scope]$ curl -v 'http://localhost:4567/define_route/juan'
* Adding handle: conn: 0x7fbacb004000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fbacb004000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 4567 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4567 (#0)
> GET /define_route/juan HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:4567
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 19
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
* Server thin 1.6.1 codename Death Proof is not blacklisted
< Server: thin 1.6.1 codename Death Proof
< 
* Connection #0 to host localhost left intact
Route juan defined!
[~/sinatra/sinatra-scope]$

Ejecución en el cliente. Ruta: /juan

[~/sinatra/sinatra-scope]$ curl -v 'http://localhost:4567/juan'
* Adding handle: conn: 0x7fbdd1800000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fbdd1800000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 4567 (#0)
*   Trying ::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4567 (#0)
> GET /juan HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:4567
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 21
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
* Server thin 1.6.1 codename Death Proof is not blacklisted
< Server: thin 1.6.1 codename Death Proof
< 
* Connection #0 to host localhost left intact
Inside defined route
You have the request scope binding inside:

  1. get, head, post, put, delete, options, patch, link, and unlink blocks
  2. before and after filters
  3. helper methods
  4. templates/views



Subsecciones
Casiano Rodriguez León 2015-01-07