Class: Kazoo::Sinatra

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/kazoo/sinatra.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.path(name, path = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/kazoo/sinatra.rb', line 26

def self.path(name, path = nil)
  if path
    raise ArgumentError, "Path must be a string" unless path.is_a?(String)
    paths[name.to_sym] = path
    path
  else
    paths[name.to_sym]
  end
end

.pathsObject



36
37
38
# File 'lib/kazoo/sinatra.rb', line 36

def self.paths
  @paths ||= {}
end

.set_paths(opts) ⇒ Object



21
22
23
# File 'lib/kazoo/sinatra.rb', line 21

def self.set_paths(opts)
  opts.each { |k,v| path(k,v) }
end

Instance Method Details

#path(name, path) ⇒ Object



40
41
42
# File 'lib/kazoo/sinatra.rb', line 40

def path(name,path)
  self.class.path(name,path)
end

#pathsObject



44
45
46
# File 'lib/kazoo/sinatra.rb', line 44

def paths
  self.class.paths
end

#render(engine, data, options = {}, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kazoo/sinatra.rb', line 5

def render(engine, data, options = {}, *args)
  
  if !options[:views] && data.is_a?(Symbol) && !data.to_s.match('/')
    data = :"#{decamelize(self.class.name)}/#{data}"
    options[:layout] ||= :'layouts/application'
  end
  
  if options[:layout] && !options[:layout].to_s.match('/')
    options[:layout] = :"layouts/#{options[:layout]}"
  end
  
  super(engine, data, options, *args)
  
end

#url(name, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kazoo/sinatra.rb', line 48

def url(name, opts = {})
  n = name.to_sym
  raise ArgumentError, "Invalid url name: #{name}" unless paths[n]
  
  url_path = paths[n].split('/').map { |part|
    next unless part.is_a?(String)
    if matches = part.match(/^:([a-z_]+)$/i)
      matched = matches[1].downcase
      opts[matched] || opts[matched.to_sym] || params[matched] || raise("You need to pass '#{matched}' to generate URL")
    else
      part
    end
  }.join('/')
  
  # Check for prefix
  full_path = request.env['HTTP_PREFIX'] ? File.join(request.env['HTTP_PREFIX'], url_path) : url_path
  
  format = opts[:format] || opts['format']
  full_path << ".#{format}" if format
  
  full_path
end