Module: Sinatra::Namespace::NamespacedMethods

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Decompile

#decompile

Methods included from SharedMethods

#namespace

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



157
158
159
# File 'lib/sinatra/namespace.rb', line 157

def base
  @base
end

#templatesObject (readonly)

Returns the value of attribute templates.



157
158
159
# File 'lib/sinatra/namespace.rb', line 157

def templates
  @templates
end

Class Method Details

.prefixed(*names) ⇒ Object



159
160
161
# File 'lib/sinatra/namespace.rb', line 159

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

Instance Method Details

#disable(*opts) ⇒ Object



211
212
213
# File 'lib/sinatra/namespace.rb', line 211

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

#enable(*opts) ⇒ Object



207
208
209
# File 'lib/sinatra/namespace.rb', line 207

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

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



191
192
193
# File 'lib/sinatra/namespace.rb', line 191

def error(codes = Exception, &block)
  [*codes].each { |c| errors[c] = block }
end

#errorsObject



183
184
185
# File 'lib/sinatra/namespace.rb', line 183

def errors
  @errors ||= {}
end

#helpers(*extensions, &block) ⇒ Object



165
166
167
168
# File 'lib/sinatra/namespace.rb', line 165

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

#invoke_hook(name, *args) ⇒ Object



179
180
181
# File 'lib/sinatra/namespace.rb', line 179

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

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



220
221
222
# File 'lib/sinatra/namespace.rb', line 220

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

#not_found(&block) ⇒ Object



187
188
189
# File 'lib/sinatra/namespace.rb', line 187

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

#register(*extensions, &block) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/sinatra/namespace.rb', line 170

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



195
196
197
198
# File 'lib/sinatra/namespace.rb', line 195

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)


200
201
202
203
204
205
# File 'lib/sinatra/namespace.rb', line 200

def set(key, value = self, &block)
  raise ArgumentError, "may not set #{key}" if key != :views
  return key.each { |k,v| set(k, v) } if block.nil? and value == self
  block ||= proc { value }
  singleton_class.send(:define_method, key, &block)
end

#template(name, &block) ⇒ Object



215
216
217
218
# File 'lib/sinatra/namespace.rb', line 215

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