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(method, *args, &block) ⇒ Object (private)



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

def method_missing(method, *args, &block)
  base.send(method, *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



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

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

#enable(*opts) ⇒ Object



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

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

#error(*codes, &block) ⇒ Object



187
188
189
190
191
192
193
194
195
# 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 do |c|
    errors = @errors[c] ||= []
    errors << args
  end
end

#errorsObject



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

def errors
  base.errors.merge(namespace_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



222
223
224
# File 'lib/sinatra/namespace.rb', line 222

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



197
198
199
200
# File 'lib/sinatra/namespace.rb', line 197

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)


202
203
204
205
206
207
# File 'lib/sinatra/namespace.rb', line 202

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



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

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