addressable icon indicating copy to clipboard operation
addressable copied to clipboard

Addressable::Template: Substitution with something that is not a Hash

Open levinalex opened this issue 12 years ago • 6 comments

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.

levinalex avatar Jul 30 '13 11:07 levinalex

Coverage Status

Coverage increased (+0%) when pulling 2bdeb5f01244b7b7036382e51e4a9a0ccd3ddf7e on levinalex:expand-lambda into 18f230538581a7ae95764452494d823ba6771f11 on sporkmonger:master.

coveralls avatar Jul 30 '13 11:07 coveralls

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.

sporkmonger avatar Sep 27 '13 23:09 sporkmonger

Sorry BTW about the really late reply, somehow the email notification for this PR slipped through the cracks.

sporkmonger avatar Sep 27 '13 23:09 sporkmonger

Also interested in @therabidbanana's take on this PR.

sporkmonger avatar Nov 04 '16 22:11 sporkmonger

This seems entirely reasonable to me - documentation would probably be good.

therabidbanana avatar Nov 06 '16 21:11 therabidbanana

@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.

sporkmonger avatar Dec 21 '16 00:12 sporkmonger