Module: Sinatra::Namespace::NamespacedMethods

Includes:
SharedMethods
Defined in:
lib/sinatra/namespace.rb

Constant Summary collapse

ALLOWED_ENGINES =
%i[
  erb erubi haml hamlit builder nokogiri sass scss
  liquid markdown rdoc asciidoc markaby
  rabl slim yajl
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#namespace

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



355
356
357
# File 'lib/sinatra/namespace.rb', line 355

def method_missing(method, *args, &block)
  base.send(method, *args, &block)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



229
230
231
# File 'lib/sinatra/namespace.rb', line 229

def base
  @base
end

#templatesObject (readonly)

Returns the value of attribute templates.



229
230
231
# File 'lib/sinatra/namespace.rb', line 229

def templates
  @templates
end

Class Method Details

.prefixed(*names) ⇒ Object



237
238
239
# File 'lib/sinatra/namespace.rb', line 237

def self.prefixed(*names)
  names.each { |n| define_method(n) { |*a, &b| prefixed(n, *a, &b) } }
end

Instance Method Details

#disable(*opts) ⇒ Object



303
304
305
# File 'lib/sinatra/namespace.rb', line 303

def disable(*opts)
  opts.each { |key| set(key, false) }
end

#enable(*opts) ⇒ Object



299
300
301
# File 'lib/sinatra/namespace.rb', line 299

def enable(*opts)
  opts.each { |key| set(key, true) }
end

#error(*codes, &block) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
# File 'lib/sinatra/namespace.rb', line 273

def error(*codes, &block)
  args  = Sinatra::Base.send(:compile!, 'ERROR', /.*/, block)
  codes = codes.map { |c| Array(c) }.flatten
  codes << Exception if codes.empty?
  codes << Sinatra::NotFound if codes.include?(404)

  codes.each do |c|
    errors = @errors[c] ||= []
    errors << args
  end
end

#errorsObject



265
266
267
# File 'lib/sinatra/namespace.rb', line 265

def errors
  base.errors.merge(namespace_errors)
end

#helpers(*extensions, &block) ⇒ Object



243
244
245
246
# File 'lib/sinatra/namespace.rb', line 243

def helpers(*extensions, &block)
  class_eval(&block) if block_given?
  include(*extensions) if extensions.any?
end

#invoke_hook(name, *args) ⇒ Object



257
258
259
# File 'lib/sinatra/namespace.rb', line 257

def invoke_hook(name, *args)
  @extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
end

#layout(name = :layout, &block) ⇒ Object



314
315
316
# File 'lib/sinatra/namespace.rb', line 314

def layout(name = :layout, &block)
  template name, &block
end

#namespace_errorsObject



269
270
271
# File 'lib/sinatra/namespace.rb', line 269

def namespace_errors
  @errors
end

#not_found(&block) ⇒ Object



261
262
263
# File 'lib/sinatra/namespace.rb', line 261

def not_found(&block)
  error(Sinatra::NotFound, &block)
end

#patternObject



318
319
320
# File 'lib/sinatra/namespace.rb', line 318

def pattern
  @pattern
end

#register(*extensions, &block) ⇒ Object



248
249
250
251
252
253
254
255
# File 'lib/sinatra/namespace.rb', line 248

def register(*extensions, &block)
  extensions << Module.new(&block) if block_given?
  @extensions += extensions
  extensions.each do |extension|
    extend extension
    extension.registered(self) if extension.respond_to?(:registered)
  end
end

#respond_to(*args) ⇒ Object



285
286
287
288
289
# File 'lib/sinatra/namespace.rb', line 285

def respond_to(*args)
  return @conditions[:provides] || base.respond_to if args.empty?

  @conditions[:provides] = args
end

#set(key, value = self, &block) ⇒ Object

Raises:

  • (ArgumentError)


291
292
293
294
295
296
297
# File 'lib/sinatra/namespace.rb', line 291

def set(key, value = self, &block)
  return key.each { |k, v| set(k, v) } if key.respond_to?(:each) && block.nil? && (value == self)
  raise ArgumentError, "may not set #{key}" unless ([:views] + ALLOWED_ENGINES).include?(key)

  block ||= proc { value }
  singleton_class.send(:define_method, key, &block)
end

#template(name, &block) ⇒ Object



307
308
309
310
311
312
# File 'lib/sinatra/namespace.rb', line 307

def template(name, &block)
  first_location = caller_locations.first
  filename = first_location.path
  line = first_location.lineno
  templates[name] = [block, filename, line]
end