Permalink
Browse files

Introduce `Utils::Hash.stringify` (#220)

* change stringify method to instance method

* make rubocop happy

* restore old stringify method

* re-introduce specs for #stringify!

* make rubocop happy

* fix rubocop

* fix rubocop
  • Loading branch information...
marionschleifer committed Jul 26, 2017
1 parent a53b1db commit abfe6808fe8a90917e8d1c301ecfa03888fafbfb
Showing with 33 additions and 0 deletions.
  1. +7 −0 lib/hanami/utils/hash.rb
  2. +26 −0 spec/unit/hanami/utils/hash_spec.rb
@@ -1,3 +1,5 @@
# rubocop:disable ClassLength

require 'hanami/utils/duplicable'
require 'transproc'

@@ -206,6 +208,7 @@ def deep_symbolize!
#
# hash.keys # => [:a, :b]
# hash.inspect # => {"a"=>23, "b"=>{"c"=>["x", "y", "z"]}}

def stringify!
keys.each do |k|
v = delete(k)
@@ -217,6 +220,10 @@ def stringify!
self
end

def self.stringify(input)
self[:stringify_keys].call(input)
end

# Return a deep copy of the current Hanami::Utils::Hash
#
# @return [Hash] a deep duplicated self
@@ -299,6 +299,32 @@ def to_h
end
end

describe ".stringify" do
it "returns ::Hash" do
hash = described_class.stringify("fub" => "baz")

expect(hash).to be_kind_of(::Hash)
end

it "stringify keys" do
hash = described_class.stringify(fub: "baz")

expect(hash).to eq("fub" => "baz")
end

it "doesn't mutate original input" do
input = { "fub" => "baz" }
described_class.stringify(input)

expect(input).to eq("fub" => "baz")
end

it "doesn't stringify nested hashes" do
hash = described_class.stringify("nested" => { key: "value" })
expect(hash["nested"].keys).to eq([:key])
end
end

describe '#deep_dup' do
it 'returns an instance of Utils::Hash' do
duped = Hanami::Utils::Hash.new('foo' => 'bar').deep_dup

0 comments on commit abfe680

Please sign in to comment.