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)



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

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



149
150
151
# File 'lib/sinatra/namespace.rb', line 149

def base
  @base
end

#templatesObject (readonly)

Returns the value of attribute templates.



149
150
151
# File 'lib/sinatra/namespace.rb', line 149

def templates
  @templates
end

Class Method Details

.prefixed(*names) ⇒ Object



151
152
153
# File 'lib/sinatra/namespace.rb', line 151

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

Instance Method Details

#disable(*opts) ⇒ Object



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

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

#enable(*opts) ⇒ Object



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

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

#error(*codes, &block) ⇒ Object



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

def error(*codes, &block)
  args  = Sinatra::Base.send(:compile!, "ERROR", /^#{@pattern}/, block)
  codes = codes.map { |c| Array(c) }.flatten
  codes << Exception if codes.empty?
  codes.each { |c| @errors[c] = args }
end

#errorsObject



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

def errors
  base.errors.merge(@errors)
end

#helpers(*extensions, &block) ⇒ Object



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

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

#invoke_hook(name, *args) ⇒ Object



171
172
173
# File 'lib/sinatra/namespace.rb', line 171

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

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



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

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

#namespace_errorsObject



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

def namespace_errors
  @errors
end

#not_found(&block) ⇒ Object



175
176
177
# File 'lib/sinatra/namespace.rb', line 175

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

#register(*extensions, &block) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/sinatra/namespace.rb', line 162

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



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

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)


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

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



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

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