Sha256: eeafeb5f15378a683846ad907ae439dd8a6a2851c426470798a50b08b37b0a0f

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

$:.unshift File.dirname(__FILE__)

require 'route/path'
require 'route/variable'
require 'route/request_method'

class Usher
  class Route
    attr_reader :paths, :original_path, :requirements, :conditions, :destination, :named
    
    def initialize(original_path, router, conditions, requirements, default_values) # :nodoc:
      @original_path = original_path
      @router = router
      @requirements = requirements
      @conditions = conditions
      @default_values = default_values
      @paths = @router.splitter.split(@original_path, @requirements, @default_values).collect {|path| Path.new(self, path)}
    end
    
    def grapher
      unless @grapher
        @grapher = Grapher.new
        @grapher.add_route(self)
      end
      @grapher
    end

    def find_matching_path(params)
      @paths.size == 1 ? @paths.first : grapher.find_matching_path(params)
    end
    
    
    # Sets +options+ on a route. Returns +self+.
    #   
    #   Request = Struct.new(:path)
    #   set = Usher.new
    #   route = set.add_route('/test')
    #   route.to(:controller => 'testing', :action => 'index')
    #   set.recognize(Request.new('/test')).first.params => {:controller => 'testing', :action => 'index'}
    def to(options = nil, &block)
      @destination = (block_given? ? block : options)
      self
    end

    # Sets route as referenceable from +name+. Returns +self+.
    #   
    #   set = Usher.new
    #   route = set.add_route('/test').name(:route)
    #   set.generate_url(:route) => '/test'
    def name(name)
      @named = name
      @router.name(name, self)
      self
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
joshbuddy-usher-0.4.5 lib/usher/route.rb
joshbuddy-usher-0.4.6 lib/usher/route.rb
joshbuddy-usher-0.4.7 lib/usher/route.rb