Class: Sinatra::Trails::Route

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

Defined Under Namespace

Classes: Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, name, ancestors, scope) ⇒ Route

Returns a new instance of Route.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sinatra/trails.rb', line 18

def initialize route, name, ancestors, scope
  @name        = name.to_s
  @full_name   = ancestors.map { |ancestor| Scope === ancestor ? ancestor.name : ancestor }.push(name).select{ |name| Symbol === name }.join('_')
  @components  = Array === route ? route.compact : route.to_s.scan(/[^\/]+/)
  @components.unshift *ancestors.map { |ancestor| ancestor.path if Scope === ancestor }.compact
  @scope       = scope
  @captures    = []
  @to_route    = "/#{@components.join('/')}"
  namespace    = ancestors.reverse.find { |ancestor| ancestor.class == Scope && ancestor.name }

  @to_regexp, @keys = Sinatra::Base.send :compile, @to_route

  add_param 'resource', scope.name if [Resource, Resources].include?(scope.class)
  add_param 'namespace', namespace.name if namespace
  add_param 'action', name
  scope.routes << self
end

Instance Attribute Details

#full_nameObject (readonly)

Returns the value of attribute full_name.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def full_name
  @full_name
end

#keysObject (readonly)

Returns the value of attribute keys.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def scope
  @scope
end

#to_regexpObject (readonly)

Returns the value of attribute to_regexp.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def to_regexp
  @to_regexp
end

#to_routeObject (readonly)

Returns the value of attribute to_route.



15
16
17
# File 'lib/sinatra/trails.rb', line 15

def to_route
  @to_route
end

Instance Method Details

#match(str) ⇒ Object



36
37
38
39
40
# File 'lib/sinatra/trails.rb', line 36

def match str
  if match_data = to_regexp.match(str)
    Match.new(match_data.captures + @captures)
  end
end

#to_path(*args) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sinatra/trails.rb', line 42

def to_path *args
  query      = args.pop if Hash === args.last
  components = @components.dup
  components.each_with_index do |component, index|
    next unless /^:/ === component
    val = args.pop or raise ArgumentError.new("Please provide `#{component}`")
    components[index] =
    case val
    when Numeric, String, Symbol then val
    else val.to_param end
  end
  raise ArgumentError.new("Too many params where passed") unless args.empty?
  "/#{components.join('/')}#{'?' + Rack::Utils.build_nested_query(query) if query}"
end