path-to 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.2.1 2009-04-18
2
+
3
+ * Minor enhancements
4
+ * Use new Addressable API
5
+ * support app[params]
6
+ * more tests!
7
+
1
8
  == 0.2.0 2009-04-18
2
9
 
3
10
  * Major enhancement
@@ -1,6 +1,6 @@
1
1
  require "path-to/path"
2
2
  require "path-to/http_client"
3
- require "addressable/uri"
3
+ require "addressable/template"
4
4
 
5
5
  module PathTo
6
6
  #
@@ -114,7 +114,7 @@ module PathTo
114
114
  # TODO it's a 1-line fix to Addressable to permit symbols (etc) as keys
115
115
  if (t = uri_template_for(method, params))
116
116
  string_keyed_params = params.keys.inject({}){|hash, key| hash[key.to_s] = params[key]; hash}
117
- Addressable::URI.expand_template(t, string_keyed_params).to_s
117
+ Addressable::Template.new(t).expand(string_keyed_params).to_s
118
118
  end
119
119
  end
120
120
  end
@@ -26,8 +26,7 @@ module PathTo
26
26
 
27
27
  def uri
28
28
  @uri ||= begin
29
- string_keyed_params = params.keys.inject({}){|hash, key| hash[key.to_s] = params[key]; hash}
30
- Addressable::URI.expand_template(uri_template, string_keyed_params).to_s
29
+ Addressable::Template.new(uri_template).expand(params).to_s
31
30
  end
32
31
  end
33
32
 
@@ -65,7 +64,7 @@ module PathTo
65
64
  #
66
65
  def method_missing(method, *args)
67
66
  child_resource_template = resource_template.resource_templates.detect{|t| t.rel == method.to_s}
68
- if resource_template && (child_class = child_class_for(self, method, params, child_resource_template))
67
+ if child_resource_template && (child_class = child_class_for(self, method, params, child_resource_template))
69
68
  params = args.inject(Hash.new){|h, arg| h.merge(arg)}
70
69
  child(child_class, method, params, child_resource_template)
71
70
  else
@@ -89,9 +88,10 @@ module PathTo
89
88
  attr_reader :base
90
89
 
91
90
  def initialize(options)
91
+ super(options[:parent], options[:service], options[:params])
92
+
92
93
  @base = options[:base]
93
94
  @base.sub(/\/$/, '') if base
94
- @params = options[:params] || {}
95
95
  @default_type = options[:default_type] || TemplatedPath
96
96
  @http_client = options[:http_client] || HTTPClient
97
97
 
@@ -106,9 +106,21 @@ module PathTo
106
106
  end
107
107
  end
108
108
 
109
- super(nil, nil, params)
109
+ if parent
110
+ @base ||= parent.base
111
+ @default_type ||= parent.default_type
112
+ @http_client ||= parent.http_client
113
+ @resource_templates ||= parent.resource_templates
114
+ end
110
115
  end
111
116
 
117
+ #
118
+ # Creates a copy of self with additional params
119
+ #
120
+ def [](params = {})
121
+ self.class.new(:parent => self, :params => params)
122
+ end
123
+
112
124
  #
113
125
  # Tries to respond to a missing method. We can do so if
114
126
  #
@@ -24,7 +24,7 @@ module PathTo
24
24
  # [params] Value for the params attribute
25
25
  #
26
26
  def initialize(parent = nil, service = nil, params = {})
27
- @parent, @service, @params = parent, service, params
27
+ @parent, @service, @params = parent, service, params || {}
28
28
  end
29
29
 
30
30
  #
@@ -37,7 +37,7 @@ module PathTo
37
37
  def child(child_class = nil, service = nil, params = {}, *other_args)
38
38
  child_class ||= child_class_for(instance, service, params)
39
39
  service ||= self.service
40
- params = self.params.merge(params)
40
+ params = self.params.merge(params || {})
41
41
  child_class.new(self, service, params, *other_args)
42
42
  end
43
43
 
data/lib/path-to.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module PathTo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
4
4
 
5
5
  $:.push File.dirname(__FILE__)
@@ -91,4 +91,32 @@ class TestDescribedRoutes < Test::Unit::TestCase
91
91
  app.users.users
92
92
  end
93
93
  end
94
+
95
+ def test_uri_template_expansion
96
+ assert_equal(
97
+ "http://localhost:3000/users/dojo/articles/recent",
98
+ app.users["user_id" => "dojo"].articles.recent.uri)
99
+ assert_equal(
100
+ "http://localhost:3000/users/dojo/articles/recent.json",
101
+ app.users["user_id" => "dojo", "format" => "json"].articles.recent.uri)
102
+ end
103
+
104
+ def test_path_optional_params
105
+ users_json = app.users["format" => "json"]
106
+ user_json = app.users["user_id" => "dojo"]["format" => "json"]
107
+
108
+ assert_equal("users", users_json.service)
109
+ assert_equal("user", user_json.service)
110
+ assert_equal("http://localhost:3000/users.json", users_json.uri)
111
+ assert_equal("http://localhost:3000/users/dojo.json", user_json.uri)
112
+ end
113
+
114
+ def test_app_params
115
+ app_json = app["format" => "json"]
116
+ users_json = app_json.users
117
+
118
+ assert_kind_of(PathTo::DescribedRoutes::Application, app_json)
119
+ assert_equal(app, app_json.parent)
120
+ assert_equal("http://localhost:3000/users.json", users_json.uri)
121
+ end
94
122
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: path-to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Burrows (asplake)
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-29 00:00:00 +01:00
12
+ date: 2009-04-30 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency