lib/leadlight/hyperlinkable.rb in leadlight-0.0.5 vs lib/leadlight/hyperlinkable.rb in leadlight-0.0.6
- old
+ new
@@ -21,22 +21,29 @@
else
links[key]
end
end
- def link(key, &fallback)
- result = links.at(key, &fallback)
+ def link(key, *expand_args, &fallback)
+ link = links.at(key, &fallback)
+ link && link.expand(*expand_args)
end
def add_link(url, rel=nil, title=rel, options={})
- link = Link.new(__service__, url, rel, title, options)
+ absolute_url = base_url + url
+ template = LinkTemplate.new(__service__, absolute_url, rel,
+ title, options)
+ link = template.expand(captures_for_variables(__captures__,
+ template.variables))
define_link_helper(rel) if rel
links << link
end
def add_link_template(template, rel=nil, title=rel, options={})
- link = LinkTemplate.new(__service__, template, rel, title, options)
+ absolute_url = base_url + template
+ link = LinkTemplate.new(__service__, absolute_url.to_s, rel,
+ title, options)
define_link_helper(rel) if rel
links << link
end
def add_link_set(rel=nil, helper_name=rel)
@@ -44,14 +51,14 @@
rel: rel
}
yield.each do |link_attributes|
attributes = default_link_attributes.merge(link_attributes)
define_link_set_helper(rel, helper_name) if helper_name
- links << Link.new(__service__,
- attributes[:href],
- attributes[:rel],
- attributes[:title],
+ links << Link.new(__service__,
+ attributes[:href],
+ attributes[:rel],
+ attributes[:title],
attributes)
end
end
def add_links_from_headers
@@ -67,11 +74,11 @@
class LinkSet
extend Forwardable
include Enumerable
fattr(:links) { Set.new }
- def_delegators :links, :<<, :push, :size, :length, :empty?, :each,
+ def_delegators :links, :<<, :push, :size, :length, :empty?, :each,
:initialize
# Match links on rel or title
def [](key)
self.class.new(select(&link_matcher(key)))
@@ -85,11 +92,11 @@
private
def link_matcher(key)
->(link) {
- key === link.rel ||
+ key === link.rel ||
key === link.title ||
link.aliases.any?{|a| key === a}
}
end
end
@@ -119,8 +126,22 @@
links(rel).at(key).follow(*args) do |result|
return result
end
end
end
+ end
+
+ def captures_for_variables(captures, variables)
+ variables.each_with_object({}) do |key, h|
+ h[key] = captures[key] if captures[key]
+ end
+ end
+
+ def base_url
+ loc = __location__.to_s
+ unless loc[-1] == "/"
+ loc = loc + "/"
+ end
+ Addressable::URI.parse(loc)
end
end
end