Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
212 additions
and
5 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 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,44 @@ | ||
require 'set' | ||
|
||
module SidekiqScheduler | ||
module Utils | ||
|
||
# Stringify keys belonging to a hash. | ||
# | ||
# Also stringifies nested keys and keys of hashes inside arrays, and sets | ||
# | ||
# @param [Object] object | ||
# | ||
# @return [Object] | ||
def self.stringify_keys(object) | ||
if object.is_a?(Hash) | ||
Hash[[*object.map { |k, v| [k.to_s, stringify_keys(v) ]} ]] | ||
|
||
elsif object.is_a?(Array) || object.is_a?(Set) | ||
object.map { |v| stringify_keys(v) } | ||
|
||
else | ||
object | ||
end | ||
end | ||
|
||
# Symbolize keys belonging to a hash. | ||
# | ||
# Also symbolizes nested keys and keys of hashes inside arrays, and sets | ||
# | ||
# @param [Object] object | ||
# | ||
# @return [Object] | ||
def self.symbolize_keys(object) | ||
if object.is_a?(Hash) | ||
Hash[[*object.map { |k, v| [k.to_sym, symbolize_keys(v) ]} ]] | ||
|
||
elsif object.is_a?(Array) || object.is_a?(Set) | ||
object.map { |v| symbolize_keys(v) } | ||
|
||
else | ||
object | ||
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
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,163 @@ | ||
require 'sidekiq-scheduler/utils' | ||
|
||
describe SidekiqScheduler::Utils do | ||
subject(:utils) { described_class } | ||
|
||
describe '.stringify_keys' do | ||
subject(:result) { utils.stringify_keys(object) } | ||
|
||
context 'with a Hash' do | ||
let(:object) do | ||
{ | ||
some_symbol_key: 'symbol', | ||
'some_string_key' => 'string', | ||
[1, 2] => 'object', | ||
nesting: { | ||
level_1: { | ||
level_2: 2, | ||
} | ||
} | ||
} | ||
end | ||
|
||
let(:expected) do | ||
{ | ||
'some_symbol_key' => 'symbol', | ||
'some_string_key' => 'string', | ||
'[1, 2]' => 'object', | ||
'nesting' => { | ||
'level_1' => { | ||
'level_2' => 2, | ||
} | ||
} | ||
} | ||
end | ||
|
||
it { should eq(expected) } | ||
end | ||
|
||
context 'with an Array' do | ||
let(:object) do | ||
[ | ||
1, | ||
2, | ||
'a string', | ||
:a_symbol, | ||
{ | ||
'some_string_key' => 'string', | ||
nesting: { | ||
level_1: { | ||
level_2: 2, | ||
} | ||
} | ||
} | ||
] | ||
end | ||
|
||
let(:expected) do | ||
[ | ||
1, | ||
2, | ||
'a string', | ||
:a_symbol, | ||
{ | ||
'some_string_key' => 'string', | ||
'nesting' => { | ||
'level_1' => { | ||
'level_2' => 2, | ||
} | ||
} | ||
} | ||
] | ||
end | ||
|
||
it { should eq(expected) } | ||
end | ||
|
||
context 'with some other object' do | ||
let(:object) { Object.new } | ||
|
||
let(:expected) { object } | ||
|
||
it { should eq(expected) } | ||
end | ||
end | ||
|
||
describe '.symbolize_keys' do | ||
subject(:result) { utils.symbolize_keys(object) } | ||
|
||
context 'with a Hash' do | ||
let(:object) do | ||
{ | ||
some_symbol_key: 'symbol', | ||
'some_string_key' => 'string', | ||
'nesting': { | ||
'level_1': { | ||
'level_2': 2, | ||
} | ||
} | ||
} | ||
end | ||
|
||
let(:expected) do | ||
{ | ||
some_symbol_key: 'symbol', | ||
some_string_key: 'string', | ||
nesting: { | ||
level_1: { | ||
level_2: 2, | ||
} | ||
} | ||
} | ||
end | ||
|
||
it { should eq(expected) } | ||
end | ||
|
||
context 'with an Array' do | ||
let(:object) do | ||
[ | ||
1, | ||
2, | ||
'a string', | ||
:a_symbol, | ||
{ | ||
some_symbol_key: 'symbol', | ||
'nesting' => { | ||
'level_1' => { | ||
'level_2' => 2, | ||
} | ||
} | ||
} | ||
] | ||
end | ||
|
||
let(:expected) do | ||
[ | ||
1, | ||
2, | ||
'a string', | ||
:a_symbol, | ||
{ | ||
some_symbol_key: 'symbol', | ||
nesting: { | ||
level_1: { | ||
level_2: 2, | ||
} | ||
} | ||
} | ||
] | ||
end | ||
|
||
it { should eq(expected) } | ||
end | ||
|
||
context 'with some other object' do | ||
let(:object) { Object.new } | ||
|
||
let(:expected) { object } | ||
|
||
it { should eq(expected) } | ||
end | ||
end | ||
end |