Updates the consuming behavior to consume the options:

This commit is contained in:
Daniel Neighman 2010-06-07 16:23:57 +10:00
parent 83a655083b
commit 4b093b3145
3 changed files with 8 additions and 16 deletions

View File

@ -5,6 +5,7 @@ class UrlMount
attr_accessor :raw_path, :options, :url_mount, :host, :scheme
alias_method :defaults, :options
alias_method :defaults=, :options=
def initialize(path, opts = {}, &blk)
@raw_path, @options = path, opts
@ -74,14 +75,14 @@ class UrlMount
nil
else
raise Ungeneratable, "Missing required variables" if !requirements_met
File.join(local_segments.inject([]){|url, segment| str = segment.to_s(opts); url << str if str; url}) =~ /(.*?)\/?$/
path = local_segments.inject([]){|url, segment| str = segment.to_s(opts); url << str if str; url}.join
match = /(.*?)\/?$/.match(path)
result = match[1]
path = url_mount.nil? ? result : File.join(url_mount.to_s(opts), result)
if opts[:host] || host || opts[:scheme] || scheme
_host = opts[:host] || host
_scheme = opts[:scheme] || scheme || "http"
_host = opts.delete(:host) || host
_scheme = opts.delete(:scheme) || scheme
if _host || _scheme
_scheme ||= "http"
raise Ungeneratable, "Missing host when generating absolute url" if _scheme && !_host
uri = URI.parse(path)
uri.host = _host
@ -171,7 +172,7 @@ class UrlMount
end
def to_s(opts = {})
item = opts[name] || @options[name]
item = opts.delete(name) || @options[name]
item.respond_to?(:call) ? item.call : item
end
end

View File

@ -50,7 +50,7 @@ class TestUrlMount < Test::Unit::TestCase
opts = {:bar => "bar", :other => "other"}
u = UrlMount.new("/foo/:bar", :bar => "some_default_bar")
u.url(opts)
assert_equal( {:bar => "bar", :other => "other"}, opts )
assert_equal( {:other => "other"}, opts )
end
should "alias to_s to url" do
@ -148,15 +148,6 @@ class TestUrlMount < Test::Unit::TestCase
u2.url_mount = u1
assert_equal "/root/different/baz/barry", u2.url(:bar => "different")
end
should "not consume params to nested routes" do
u1 = UrlMount.new("/root/:bar", :bar => "bar")
u2 = UrlMount.new("/baz/:barry", :barry => "barry")
u2.url_mount = u1
opts = {:bar => "sue", :barry => "wendy"}
assert_equal "/root/sue/baz/wendy", u2.url(opts)
assert_equal({:bar => "sue", :barry => "wendy"}, opts)
end
end
context "host options" do

View File

@ -3,7 +3,7 @@ require 'bundler'
Gem::Specification.new do |s|
s.name = %q{url_mount}
s.version = "0.2.0"
s.version = "0.2.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Neighman"]