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)



271
272
273
# File 'lib/sinatra/namespace.rb', line 271

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base



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

def base
  @base
end

#templatesObject (readonly)

Returns the value of attribute templates



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

def templates
  @templates
end

Class Method Details

.prefixed(*names) ⇒ Object



153
154
155
# File 'lib/sinatra/namespace.rb', line 153

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

Instance Method Details

#disable(*opts) ⇒ Object



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

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

#enable(*opts) ⇒ Object



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

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

#error(*codes, &block) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/sinatra/namespace.rb', line 189

def error(*codes, &block)
  args  = Sinatra::Base.send(:compile!, "ERROR", regexpify(@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



181
182
183
# File 'lib/sinatra/namespace.rb', line 181

def errors
  base.errors.merge(namespace_errors)
end

#helpers(*extensions, &block) ⇒ Object



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

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

#invoke_hook(name, *args) ⇒ Object



173
174
175
# File 'lib/sinatra/namespace.rb', line 173

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

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



225
226
227
# File 'lib/sinatra/namespace.rb', line 225

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

#namespace_errorsObject



185
186
187
# File 'lib/sinatra/namespace.rb', line 185

def namespace_errors
  @errors
end

#not_found(&block) ⇒ Object



177
178
179
# File 'lib/sinatra/namespace.rb', line 177

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

#register(*extensions, &block) ⇒ Object



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

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



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

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)


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

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



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

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