Rufus::Jig::Path

Some helper methods for URI paths.

Public Class Methods

add_params(path, h) click to toggle source

Given a path and a hash of options, …

  p Rufus::Jig::Path.add_params('/toto', :q => 'nada')
    # => "/toto?q=nada"

  p Rufus::Jig::Path.add_params('/toto?rev=2', :q => 'nada')
    # => "/toto?rev=2&q=nada"

  p Rufus::Jig::Path.add_params('/toto', :q => 'nada', 'x' => 2)
    # => "/toto?q=nada&x=2"
    # File lib/rufus/jig/path.rb, line 44
44:     def self.add_params (path, h)
45: 
46:       params = h.collect { |k, v| "#{k}=#{v}" }.join('&')
47: 
48:       sep = path.index('?') ? '&' : '?'
49: 
50:       params.length > 0 ? "#{path}#{sep}#{params}" : path
51:     end
join(*elts) click to toggle source

Joins paths into a ’/’ prefixed path.

  p Rufus::Jig::Path.join('division', 'customer', :restricted => true)
    # => '/division/customer?restricted=true'
    # File lib/rufus/jig/path.rb, line 58
58:     def self.join (*elts)
59: 
60:       elts, params = if elts.last.is_a?(Hash)
61:         [ elts[0..2], elts.last ]
62:       else
63:         [ elts, {} ]
64:       end
65: 
66:       r = elts.collect { |e|
67: 
68:         e = to_path(e)
69: 
70:         if m = e.match(/^(.*)\/$/)
71:           m[1]
72:         else
73:           e
74:         end
75:       }.join
76: 
77:       add_params(r, params)
78:     end
to_name(s) click to toggle source

Removes any forward slashes at the beginning of the given string.

    # File lib/rufus/jig/path.rb, line 89
89:     def self.to_name (s)
90: 
91:       if m = s.match(/^\/+(.+)$/)
92:         m[1]
93:       else
94:         s
95:       end
96:     end
to_path(s) click to toggle source

Makes sure there is a forward slash in the given string.

    # File lib/rufus/jig/path.rb, line 82
82:     def self.to_path (s)
83: 
84:       s.match(/^\//) ? s : "/#{s}"
85:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.