Class: Sinatra::Trails::Resources

Inherits:
Resource show all
Defined in:
lib/sinatra/trails.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#opts

Attributes inherited from Scope

#ancestors, #name, #path, #routes

Instance Method Summary collapse

Methods inherited from Resource

#generate_routes!, #member

Methods inherited from Scope

#after, #before, #generate_routes!, #namespace, #resource, #resources, #route_for, #routes_hash

Constructor Details

#initialize(app, raw_name, ancestors, opts) ⇒ Resources

Returns a new instance of Resources.



196
197
198
199
200
201
# File 'lib/sinatra/trails.rb', line 196

def initialize app, raw_name, ancestors, opts
  super
  @plural_name = name
  @name        = name.to_s.singularize.to_sym
  @path        = [plural_name, ":#{name}_id"]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sinatra::Trails::Scope

Instance Attribute Details

#name_prefixObject (readonly)

Returns the value of attribute name_prefix.



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

def name_prefix
  @name_prefix
end

#plural_nameObject (readonly)

Returns the value of attribute plural_name.



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

def plural_name
  @plural_name
end

Instance Method Details

#collection(action) ⇒ Object



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

def collection action
  ancestors  = [*self.ancestors, action == :new ? name : plural_name]
  path       = [plural_name]
  ancestors.unshift(action) and path.push(action) unless action == :index

  ancestors[0, ancestors.size - 2] = ancestors[0..-3].reject{ |ancestor| Resource === ancestor } if opts[:shallow]
  Route.new(path, action.to_s, ancestors, self)
end

#map(action) ⇒ Object



212
213
214
215
216
# File 'lib/sinatra/trails.rb', line 212

def map action
  ancestors  = [*self.ancestors, plural_name, action]
  ancestors[0, ancestors.size - 2] = ancestors[0..-3].reject{ |ancestor| Resource === ancestor } if opts[:shallow]
  Route.new([plural_name, action], action.to_s, ancestors, self)
end