Permalink
Browse files

Made Resource#path prettier to my eyes.

  • Loading branch information...
1 parent 142ebdc commit 63f5a8a95ff3b06ffcee5529b570d1962c9ffaf4 @nakajima committed Feb 3, 2009
Showing with 8 additions and 8 deletions.
  1. +8 −8 lib/sinatras-hat/resource.rb
@@ -8,21 +8,21 @@ def initialize(maker)
end
def path(suffix, record=nil)
- suffix = suffix.dup
-
- parents = path_records_for(record) if record
-
- path = resources.inject("") do |memo, maker|
+ records = record ? path_records_for(record) : []
+ results = resources.inject("") do |memo, maker|
memo += fragment(maker, record)
end
- path = clean(path + suffix)
- path.gsub!(/:(\w+)/) { parents.pop.send(@maker.to_param) } if record
- path
+ interpolate(clean(results + suffix.dup), records)
end
private
+ def interpolate(uri, records)
+ return uri if records.empty?
+ uri.gsub(/:(\w+)/) { records.pop.send(@maker.to_param) }
+ end
+
def path_records_for(record)
[record].tap do |parents|
resources.reverse.each do |resource|

0 comments on commit 63f5a8a

Please sign in to comment.