Addressable::Template: Substitution with something that is not a Hash
I'm trying to replace my ad-hoc URI-Template parser with Addressable::Template.
However, I don't have the substitutions in Hash form. The current Template implementation assumes that substitutions are Enumerable (it calls inject on mapping)
Not calling inject makes it possible to pass a lambda (or anything else that responds to [] as a substitute:
describe "Substituting from an optional block" do
sub = lambda { |x| "foo-#{x}" }
Addressable::Template.new("{bar}/{baz}").expand(sub).to_str.should == "foo-bar/foo-baz"
end
Ideally I'd like to change expand to take a block like so:
template.expand { |field| "foo-#{field}" }
but I'd like to hear what you think about changing the API in that way.
Coverage increased (+0%) when pulling 2bdeb5f01244b7b7036382e51e4a9a0ccd3ddf7e on levinalex:expand-lambda into 18f230538581a7ae95764452494d823ba6771f11 on sporkmonger:master.
I like this idea, but I'd like to see documentation updated for the methods that would start accepting anything that responds to [] as well as some responds_to? type checking with TypeErrors thrown to keep the exceptions from getting too cryptic. I generally consider NoMethodError a bug in the library.
Sorry BTW about the really late reply, somehow the email notification for this PR slipped through the cracks.
Also interested in @therabidbanana's take on this PR.
This seems entirely reasonable to me - documentation would probably be good.
@levinalex I like this change, but I'd like to see at least one more test covering the "anything else that responds to []" scenario as well as a documentation update, as @therabidbanana mentioned.