Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template: Don't assume mapping is enumerable #512

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/addressable/template.rb
Expand Up @@ -922,6 +922,7 @@ def normalize_value(value)
# @return [Hash]
# A hash with stringified keys
def normalize_keys(mapping)
return mapping unless mapping.respond_to?(:inject)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was adding more tests/docs for this I realized it's incomplete for #partial_expand which requires something responding to #key? because nil is a valid expansion value (which becomes an empty string)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apoligize I did not mean to resolve it!

return mapping.inject({}) do |accu, pair|
name, value = pair
if Symbol === name
Expand Down
6 changes: 6 additions & 0 deletions spec/addressable/template_spec.rb
Expand Up @@ -102,6 +102,12 @@
}
end

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

describe "Level 1:" do
subject {
{:var => "value", :hello => "Hello World!"}
Expand Down