1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Update from_hash to walk Array objects (#1900)

Update from_hash to walk Array objects

Before:

    c = Pry::Config.from_hash ary: [{b: 2}]
    c.ary[0].class # => Hash
    c.ary[0].b     # => Raises NoMethodError

After:

    c = Pry::Config.from_hash ary: [{b: 2}]
    c.ary[0].class # => Pry::Config
    c.ary[0].b     # => 2
This commit is contained in:
r-obert 2018-12-13 08:46:39 +01:00 committed by GitHub
parent 28b3c9ff63
commit 8248fe588f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -98,7 +98,13 @@ class Pry
def from_hash(attributes, default = nil)
new(default).tap do |config|
attributes.each do |key,value|
config[key] = Hash === value ? from_hash(value) : value
config[key] = if Hash === value
from_hash(value)
elsif Array === value
value.map { |v| Hash === v ? from_hash(v) : v }
else
value
end
end
end
end

View file

@ -11,12 +11,19 @@ RSpec.describe Pry::Config do
expect(local.default).to eq(local)
end
it "recursively creates Pry::Config objects from a Hash" do
it "recursively walks a Hash" do
h = {'foo1' => {'foo2' => {'foo3' => 'foobar'}}}
default = described_class.from_hash(h)
expect(default.foo1).to be_instance_of(described_class)
expect(default.foo1.foo2).to be_instance_of(described_class)
end
it "recursively walks an Array" do
c = described_class.from_hash(ary: [{number: 2}, Object, BasicObject.new])
expect(c.ary[0].number).to eq(2)
expect(c.ary[1]).to eq(Object)
expect(BasicObject === c.ary[2]).to be(true)
end
end
describe "bug #1552" do