Class: Sinatra::Base

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils, Helpers, Templates
Defined in:
lib/sinatra/base.rb

Overview

Base class for all Sinatra applications and middleware.

Direct Known Subclasses

Default

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Templates

#builder, #erb, #haml, #sass

Methods included from Helpers

#attachment, #back, #body, #content_type, #error, #etag, #last_modified, #media_type, #not_found, #redirect, #send_file, #session, #status

Constructor Details

#initialize(app = nil) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (Sinatra::Base)

    the object that the method was called on



321
322
323
324
# File 'lib/sinatra/base.rb', line 321

def initialize(app=nil)
  @app = app
  yield self if block_given?
end

Class Attribute Details

.conditionsObject

Returns the value of attribute conditions.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def conditions
  @conditions
end

.errorsObject

Returns the value of attribute errors.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def errors
  @errors
end

.filtersObject

Returns the value of attribute filters.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def filters
  @filters
end

.middlewareObject

Returns the value of attribute middleware.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def middleware
  @middleware
end

.routesObject

Returns the value of attribute routes.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def routes
  @routes
end

.templatesObject

Returns the value of attribute templates.



524
525
526
# File 'lib/sinatra/base.rb', line 524

def templates
  @templates
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



319
320
321
# File 'lib/sinatra/base.rb', line 319

def app
  @app
end

#envObject

Returns the value of attribute env.



331
332
333
# File 'lib/sinatra/base.rb', line 331

def env
  @env
end

#paramsObject

Returns the value of attribute params.



331
332
333
# File 'lib/sinatra/base.rb', line 331

def params
  @params
end

#requestObject

Returns the value of attribute request.



331
332
333
# File 'lib/sinatra/base.rb', line 331

def request
  @request
end

#responseObject

Returns the value of attribute response.



331
332
333
# File 'lib/sinatra/base.rb', line 331

def response
  @response
end

Class Method Details

.before(&block) ⇒ Object



596
597
598
# File 'lib/sinatra/base.rb', line 596

def before(&block)
  @filters << block
end

.call(env) ⇒ Object



738
739
740
741
742
743
744
# File 'lib/sinatra/base.rb', line 738

def call(env)
  synchronize do
    reload! if reload?
    construct_middleware if @callsite.nil?
    @callsite.call(env)
  end
end

.condition(&block) ⇒ Object



600
601
602
# File 'lib/sinatra/base.rb', line 600

def condition(&block)
  @conditions << block
end

.configure(*envs, &block) ⇒ Object



711
712
713
714
# File 'lib/sinatra/base.rb', line 711

def configure(*envs, &block)
  return if reloading?
  yield if envs.empty? || envs.include?(environment.to_sym)
end

.delete(path, opts = {}, &bk) ⇒ Object



646
# File 'lib/sinatra/base.rb', line 646

def delete(path, opts={}, &bk); route 'DELETE', path, opts, &bk; end

.development?Boolean

Returns:

  • (Boolean)


707
# File 'lib/sinatra/base.rb', line 707

def development? ; environment == :development ; end

.disable(*opts) ⇒ Object



547
548
549
# File 'lib/sinatra/base.rb', line 547

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

.enable(*opts) ⇒ Object



543
544
545
# File 'lib/sinatra/base.rb', line 543

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

.error(codes = Exception, &block) ⇒ Object



551
552
553
554
555
556
557
# File 'lib/sinatra/base.rb', line 551

def error(codes=Exception, &block)
  if codes.respond_to? :each
    codes.each { |err| error(err, &block) }
  else
    @errors[codes] = block
  end
end

.get(path, opts = {}, &block) ⇒ Object



636
637
638
639
640
641
642
# File 'lib/sinatra/base.rb', line 636

def get(path, opts={}, &block)
  conditions = @conditions.dup
  route('GET', path, opts, &block)

  @conditions = conditions
  route('HEAD', path, opts, &block)
end

.head(path, opts = {}, &bk) ⇒ Object



647
# File 'lib/sinatra/base.rb', line 647

def head(path, opts={}, &bk); route 'HEAD', path, opts, &bk; end

.helpers(*extensions, &block) ⇒ Object



697
698
699
700
# File 'lib/sinatra/base.rb', line 697

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

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



567
568
569
# File 'lib/sinatra/base.rb', line 567

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

.media_type(type) ⇒ Object

Look up a media type by file extension in Rack’s mime registry.



590
591
592
593
594
# File 'lib/sinatra/base.rb', line 590

def media_type(type)
  return type if type.nil? || type.to_s.include?('/')
  type = ".#{type}" unless type.to_s[0] == ?.
  Rack::Mime.mime_type(type, nil)
end

.not_found(&block) ⇒ Object



559
560
561
# File 'lib/sinatra/base.rb', line 559

def not_found(&block)
  error 404, &block
end

.post(path, opts = {}, &bk) ⇒ Object



645
# File 'lib/sinatra/base.rb', line 645

def post(path, opts={}, &bk); route 'POST', path, opts, &bk; end

.production?Boolean

Returns:

  • (Boolean)


709
# File 'lib/sinatra/base.rb', line 709

def production? ; environment == :production ; end

.put(path, opts = {}, &bk) ⇒ Object



644
# File 'lib/sinatra/base.rb', line 644

def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end

.register(*extensions, &block) ⇒ Object



702
703
704
705
# File 'lib/sinatra/base.rb', line 702

def register(*extensions, &block)
  extensions << Module.new(&block) if block
  extend *extensions
end

.reload!Object



746
747
748
749
750
751
752
# File 'lib/sinatra/base.rb', line 746

def reload!
  @reloading = true
  superclass.send :reset!, self
  $LOADED_FEATURES.delete("sinatra.rb")
  ::Kernel.load app_file
  @reloading = false
end

.run!(options = {}) ⇒ Object



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/sinatra/base.rb', line 721

def run!(options={})
  set options
  handler      = detect_rack_handler
  handler_name = handler.name.gsub(/.*::/, '')
  puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
    "on #{port} for #{environment} with backup from #{handler_name}"
  handler.run self, :Host => host, :Port => port do |server|
    trap(:INT) do
      ## Use thins' hard #stop! if available, otherwise just #stop
      server.respond_to?(:stop!) ? server.stop! : server.stop
      puts "\n== Sinatra has ended his set (crowd applauds)"
    end
  end
rescue Errno::EADDRINUSE => e
  puts "== Someone is already performing on port #{port}!"
end

.set(option, value = self) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/sinatra/base.rb', line 528

def set(option, value=self)
  if value.kind_of?(Proc)
    metadef(option, &value)
    metadef("#{option}?") { !!__send__(option) }
    metadef("#{option}=") { |val| set(option, Proc.new{val}) }
  elsif value == self && option.respond_to?(:to_hash)
    option.to_hash.each { |k,v| set(k, v) }
  elsif respond_to?("#{option}=")
    __send__ "#{option}=", value
  else
    set option, Proc.new{value}
  end
  self
end

.template(name, &block) ⇒ Object



563
564
565
# File 'lib/sinatra/base.rb', line 563

def template(name, &block)
  templates[name] = block
end

.test?Boolean

Returns:

  • (Boolean)


708
# File 'lib/sinatra/base.rb', line 708

def test? ; environment == :test ; end

.use(middleware, *args, &block) ⇒ Object



716
717
718
719
# File 'lib/sinatra/base.rb', line 716

def use(middleware, *args, &block)
  reset_middleware
  @middleware << [middleware, args, block]
end

.use_in_file_templates!Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/sinatra/base.rb', line 571

def use_in_file_templates!
  ignore = [/lib\/sinatra.*\.rb/, /\(.*\)/, /rubygems\/custom_require\.rb/]
  file = caller.
    map  { |line| line.sub(/:\d+.*$/, '') }.
    find { |line| ignore.all? { |pattern| line !~ pattern } }
  if data = ::IO.read(file).split('__END__')[1]
    data.gsub!(/\r\n/, "\n")
    template = nil
    data.each_line do |line|
      if line =~ /^@@\s*(.*)/
        template = templates[$1.to_sym] = ''
      elsif template
        template << line
      end
    end
  end
end

Instance Method Details

#call(env) ⇒ Object

Rack call interface.



327
328
329
# File 'lib/sinatra/base.rb', line 327

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/sinatra/base.rb', line 333

def call!(env)
  @env      = env
  @request  = Request.new(env)
  @response = Response.new
  @params   = nil

  invoke { dispatch! }
  invoke { error_block!(response.status) }

  # never respond with a body on HEAD requests
  @response.body = [] if @env['REQUEST_METHOD'] == 'HEAD'

  @response.finish
end

#halt(*response) ⇒ Object

Exit the current block and halt the response.



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

def halt(*response)
  response = response.first if response.length == 1
  throw :halt, response
end

#optionsObject

Access options defined with Base.set.



349
350
351
# File 'lib/sinatra/base.rb', line 349

def options
  self.class
end

#passObject

Pass control to the next matching route.



360
361
362
# File 'lib/sinatra/base.rb', line 360

def pass
  throw :pass
end