Module: Rtopia

Extended by:
Rtopia
Included in:
Rtopia
Defined in:
lib/rtopia.rb

Constant Summary collapse

VERSION =
"0.2.3"

Instance Method Summary collapse

Instance Method Details

#RString #R(: arg1, : arg2, ..., : argN) ⇒ String #R(: arg1, : arg2, ..., : argN, hash) ⇒ String #R(hash) ⇒ String

Examples:


R(:items) # => /items

@person = Person.new # has a to_param of john-doe
R(@person) # => '/john-doe'
R(@person, :posts) # => '/john-doe/posts'
R(@person, :posts, :replied) # => '/john-doe/posts/replied'

@entry = Entry.create # has an id of 1001 for example and Ruby1.9
R(@entry) # => '/1001'
R(:entry, @entry) # => '/entry/1001'

R(:search, :q => 'Ruby', :page => 1) => '/search?q=Ruby&page=1'
R(:q => 'Ruby', :page => 1) => '?q=Ruby&page=1'
R(:q => ['first, 'second']) => '?q[]=first&q[]=second'
R(:user => { :lname => 'Doe', :fname => 'John' })
=> '?user[lname]=Doe&user[fname]=John'

R("http://google.com/search", :q => "Robots")
=> 'http://google.com/search?q=Robots'

Overloads:

  • #RString

    Returns the root ‘/’

    Returns:

    • (String)

      returns the root ‘/’

  • #R(: arg1, : arg2, ..., : argN) ⇒ String

    Returns all args joined on ‘/’ prefixed with ‘/’.

    Returns:

    • (String)

      all args joined on ‘/’ prefixed with ‘/’

  • #R(: arg1, : arg2, ..., : argN, hash) ⇒ String

    Returns all args joined on ‘/’ with the hash represented as a query string.

    Parameters:

    • arg1 (#to_param)

      any object checked for #to_param, #id, and #to_s

    • arg2 (#to_param)

      any object checked for #to_param, #id, and #to_s

    • argN (#to_param)

      any object checked for #to_param, #id, and #to_s

    • hash (Hash)

      to be represented as a query string.

    Returns:

    • (String)

      all args joined on ‘/’ with the hash represented as a query string.

  • #R(hash) ⇒ String

    Returns the hash represented as a query string, prefix with a ‘?`.

    Parameters:

    • hash (Hash)

      to be represented as a query string.

    Returns:

    • (String)

      the hash represented as a query string, prefix with a ‘?`



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rtopia.rb', line 46

def R(*args)
  hash = args.last.is_a?(Hash) ? args.pop : {}

  return '?' + query_string(hash)  if hash.any? and args.empty?

  host_with_port = args.shift  if args.first =~ /^(https?|ftp)/

  path = args.unshift('/').map { |arg| to_param(arg) }.join('/').squeeze('/')

  path.tap do |ret|
    if hash.any?
      ret.gsub!(/^\//, '')  if ret == '/'
      ret << '?'
      ret << query_string(hash)
    end
    ret.insert(0, host_with_port.gsub(/\/$/, ''))  if host_with_port
  end
end