Module: Sinatra::Subdomain

Defined in:
lib/sinatra/subdomain.rb,
lib/sinatra/subdomain/version.rb

Defined Under Namespace

Modules: Helpers, Version

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.appObject

Returns the value of attribute app.



7
8
9
# File 'lib/sinatra/subdomain.rb', line 7

def app
  @app
end

.subdomainObject

Returns the value of attribute subdomain.



7
8
9
# File 'lib/sinatra/subdomain.rb', line 7

def subdomain
  @subdomain
end

Class Method Details

.registered(app) ⇒ Object



53
54
55
56
# File 'lib/sinatra/subdomain.rb', line 53

def self.registered(app)
  app.helpers Sinatra::Subdomain::Helpers
  app.set :tld_size, 1
end

.route_added(verb, path, block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/subdomain.rb', line 33

def self.route_added(verb, path, block)
  return unless subdomain && app

  routes = app.instance_variable_get("@routes")
  last_route = routes[verb].last
  expected = subdomain

  condition = app.instance_eval do
    generate_method :subdomain do
      if expected == true
        subdomain != nil
      else
        subdomain.to_s == expected.to_s
      end
    end
  end

  last_route[2] << condition
end

Instance Method Details

#subdomain(expected_subdomain = true, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sinatra/subdomain.rb', line 19

def subdomain(expected_subdomain = true, &block)
  ::Sinatra::Subdomain.tap do |mod|
    mod.app = self
    mod.subdomain = expected_subdomain
  end

  yield

  ::Sinatra::Subdomain.tap do |mod|
    mod.app = nil
    mod.subdomain = nil
  end
end