rtopia 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ /doc
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,14 +1,13 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+
1
4
  module Rtopia
2
5
  def self.R(*args)
3
6
  @rtopia ||= Object.new.extend(self)
4
-
5
7
  @rtopia.R(*args)
6
8
  end
7
9
 
8
- # Pass any collection of objects
9
- # R will use it's :to_param, then :id (if ruby19), then :to_s
10
- #
11
- # Examples:
10
+ # Usage:
12
11
  #
13
12
  # R(:items) # => /items
14
13
  #
@@ -17,15 +16,55 @@ module Rtopia
17
16
  # R(@person, :posts) # => '/john-doe/posts'
18
17
  # R(@person, :posts, :replied) # => '/john-doe/posts/replied'
19
18
  #
20
- # @entry = Entry.create # has an id of 1001 for example
19
+ # @entry = Entry.create # has an id of 1001 for example and Ruby1.9
21
20
  # R(@entry) # => '/1001'
22
21
  # R(:entry, @entry) # => '/entry/1001'
23
22
  #
23
+ # R(:search, :q => 'Ruby', :page => 1) => '/search?q=Ruby&page=1'
24
+ # R(:q => 'Ruby', :page => 1) => '?q=Ruby&page=1'
25
+ # R(:q => ['first, 'second']) => '?q[]=first&q[]=second'
26
+ # R(:user => { :lname => 'Doe', :fname => 'John' })
27
+ # => '?user[lname]=Doe&user[fname]=John'
28
+ #
24
29
  def R(*args)
25
- args.unshift('/').map { |arg| to_param(arg) }.join('/').squeeze('/')
30
+ hash = args.last.is_a?(Hash) ? args.pop : {}
31
+
32
+ # No args, so we opt to make ?q=Ruby&page=1 style URIs
33
+ if hash.any? and args.empty?
34
+ '?' + query_string(hash)
35
+ else
36
+ r = args.unshift('/').map { |arg| to_param(arg) }.join('/').squeeze('/')
37
+ if hash.any?
38
+ r << '?'
39
+ r << query_string(hash)
40
+ end
41
+ r
42
+ end
26
43
  end
27
44
 
28
45
  private
46
+ def query_string(hash)
47
+ hash.inject([]) { |arr, (key, value)|
48
+ if value.is_a?(Array)
49
+ value.each do |e|
50
+ arr << to_query("#{key}[]", e)
51
+ end
52
+ arr
53
+ elsif value.is_a?(Hash)
54
+ value.each do |namespace, deeper|
55
+ arr << to_query("#{key}[#{namespace}]", deeper)
56
+ end
57
+ arr
58
+ else
59
+ arr << to_query(key, value)
60
+ end
61
+ }.join('&')
62
+ end
63
+
64
+ def to_query(k, v)
65
+ '%s=%s' % [CGI.escape(k.to_s), URI.escape(to_param(v))]
66
+ end
67
+
29
68
  # Primary difference of this method is that it checks if
30
69
  # the passed object has an :id method
31
70
  # after checking for a :to_param
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rtopia}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sinefunc"]
12
- s.date = %q{2010-04-21}
12
+ s.date = %q{2010-04-25}
13
13
  s.description = %q{For use anywhere you have objects with to_params, ids, or just to_s's}
14
14
  s.email = %q{sinefunc@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.homepage = %q{http://github.com/sinefunc/rtopia}
32
32
  s.rdoc_options = ["--charset=UTF-8"]
33
33
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.6}
34
+ s.rubygems_version = %q{1.3.5}
35
35
  s.summary = %q{A cute helper for route generation}
36
36
  s.test_files = [
37
37
  "test/helper.rb",
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
+ require 'mocha'
3
4
 
4
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -60,4 +60,22 @@ class TestRtopia < Test::Unit::TestCase
60
60
  def test_Rtopia_R_is_calleable_directly
61
61
  assert_equal '/items/yeah/boi', Rtopia.R(:items, :yeah, :boi)
62
62
  end
63
+
64
+ def test_Rtopia_R_with_just_hash_returns_query_string
65
+ assert_equal '?q=Ruby&page=1', Rtopia.R(:q => 'Ruby', :page => 1)
66
+ end
67
+
68
+ def test_Rtopia_R_with_search_then_hash_returns_search_with_query_string
69
+ assert_equal '/search?q=Ruby&page=1', R(:search, :q => 'Ruby', :page => 1)
70
+ end
71
+
72
+ def test_Rtopia_R_of_array_produces_empty_brackets
73
+ assert_equal '/search?q%5B%5D=first&q%5B%5D=second',
74
+ R(:search, :q => ['first', 'second'])
75
+ end
76
+
77
+ def test_Rtopia_R_of_nested_hash_produces_namespaced_hash
78
+ assert_equal '/users?user%5Blname%5D=Doe&user%5Bfname%5D=John',
79
+ R(:users, :user => { :lname => 'Doe', :fname => 'John' })
80
+ end
63
81
  end
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtopia
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
4
+ version: 0.2.0
10
5
  platform: ruby
11
6
  authors:
12
7
  - Sinefunc
@@ -14,7 +9,7 @@ autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
11
 
17
- date: 2010-04-21 00:00:00 +08:00
12
+ date: 2010-04-25 00:00:00 +08:00
18
13
  default_executable:
19
14
  dependencies: []
20
15
 
@@ -51,20 +46,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
46
  requirements:
52
47
  - - ">="
53
48
  - !ruby/object:Gem::Version
54
- segments:
55
- - 0
56
49
  version: "0"
50
+ version:
57
51
  required_rubygems_version: !ruby/object:Gem::Requirement
58
52
  requirements:
59
53
  - - ">="
60
54
  - !ruby/object:Gem::Version
61
- segments:
62
- - 0
63
55
  version: "0"
56
+ version:
64
57
  requirements: []
65
58
 
66
59
  rubyforge_project:
67
- rubygems_version: 1.3.6
60
+ rubygems_version: 1.3.5
68
61
  signing_key:
69
62
  specification_version: 3
70
63
  summary: A cute helper for route generation