-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2388 from zapnap/hash-helper-symbolize
Move hash symbolizer into a helper, prevent namespace conflicts
- Loading branch information
Showing
5 changed files
with
67 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module RailsAdmin | ||
class HashHelper | ||
def self.symbolize(obj) | ||
case obj | ||
when Array | ||
obj.each_with_object([]) do |val, res| | ||
res << case val | ||
when Hash, Array then symbolize(val) | ||
when String then val.to_sym | ||
else val | ||
end | ||
end | ||
when Hash | ||
obj.each_with_object({}) do |(key, val), res| | ||
nkey = key.is_a?(String) ? key.to_sym : key | ||
nval = case val | ||
when Hash, Array then symbolize(val) | ||
when String then val.to_sym | ||
else val | ||
end | ||
res[nkey] = nval | ||
end | ||
else | ||
obj | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe RailsAdmin::HashHelper do | ||
let(:hash) do | ||
{'subject' => 'Test', | ||
'user' => {name: 'Dirk', | ||
'title' => 'Holistic Detective', | ||
'clients' => [ | ||
{name: 'Zaphod'}, | ||
{'name' => 'Arthur'}]}} | ||
end | ||
|
||
describe 'symbolize' do | ||
let(:symbolized_hash) { RailsAdmin::HashHelper.symbolize(hash) } | ||
|
||
it 'symbolizes top-level hash keys' do | ||
[:subject, :user].each do |key| | ||
expect(symbolized_hash.keys).to include(key) | ||
end | ||
end | ||
|
||
it 'symbolizes nested hashes' do | ||
[:name, :title, :clients].each do |key| | ||
expect(symbolized_hash[:user].keys).to include(key) | ||
end | ||
end | ||
|
||
it 'symbolizes nested hashes inside of array values' do | ||
clients = symbolized_hash[:user][:clients] | ||
expect(clients.length).to eq(2) | ||
expect(clients[0][:name]).to eq(:Zaphod) | ||
expect(clients[1][:name]).to eq(:Arthur) | ||
end | ||
end | ||
end |