Class: Maveric::Route

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

Overview

Encapsulating route functionality into a single class.

Route instances are typically stored in a Maveric instance and used for matching incoming paths to controllers. After a #match is made the resulting Struct instance has members consisting of the parameters of the seed path, as well as struct pointing to the destination Controller and struct pointing to the Route instance itself.

Constant Summary collapse

URI_CHAR =

A negative class of URI delimiting and reserved characters.

'[^/?:,&#]'
PARAM_MATCH =

Standard pattern for finding parameters in provided paths.

%r~:(#{URI_CHAR}+):?~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, path, opts = {}) ⇒ Route

Builds a regex and respective param list from path by parsing out fragments matching PARAM_MATCH and replacing them with either a corresponding symbol from the opts hash or the default regex.

The final Route instance is utilized to build paths and match absolute paths for routing.

Raises:

  • (ArgumentError)


667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/maveric.rb', line 667

def initialize controller, path, opts={}
  @controller = controller
  @path = path.dup.freeze
  params = []
  regex = @path.gsub PARAM_MATCH do
    params << param = $1.to_sym
    "(#{opts[param] || URI_CHAR+'+'})"
  end
  raise ArgumentError, "Duplicated parameters in path."+
    " <#{params.inspect}>" if params.size != params.uniq.size
  @struct = Struct.new(:route, :controller, *params)
  @regex = /\A#{regex}\z/.freeze
  @opts = opts.reject{|k,v| !params.include? k }.freeze
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



682
683
684
# File 'lib/maveric.rb', line 682

def controller
  @controller
end

#optsObject (readonly)

Returns the value of attribute opts.



682
683
684
# File 'lib/maveric.rb', line 682

def opts
  @opts
end

#pathObject (readonly)

Returns the value of attribute path.



682
683
684
# File 'lib/maveric.rb', line 682

def path
  @path
end

#regexObject (readonly)

Returns the value of attribute regex.



682
683
684
# File 'lib/maveric.rb', line 682

def regex
  @regex
end

#structObject (readonly)

Returns the value of attribute struct.



682
683
684
# File 'lib/maveric.rb', line 682

def struct
  @struct
end

Instance Method Details

#match(path) ⇒ Object

Matches the Route’s regex against path, returning a Struct instance of the resulting data, or nil.



687
688
689
# File 'lib/maveric.rb', line 687

def match path
  @regex =~ path and @struct.new(self, @controller, *$~.captures)
end

#path_to(args) ⇒ Object

Returns nil unless all parameters are included in args, preventing the creation of incomplete paths, otherwise returns a path string.



694
695
696
697
# File 'lib/maveric.rb', line 694

def path_to args
  return nil unless @opts.keys.sort == args.keys.sort
  args.inject(@path){|u,(k,v)| u.sub(/#{k.inspect}:?/, v.to_s) }
end