confstruct icon indicating copy to clipboard operation
confstruct copied to clipboard

Unexpected behaviour when using #presence

Open phorsuedzie opened this issue 13 years ago • 1 comments

Given I create a Confstruct::Configuration from a Hash When I call #presence on a deep value Then I should get the value # or at least something behaving equally

This fails - because #presence (called for an HashWithStructAccess with ActiveSupport) will return the value converted to an OrderedHash:

 $#> irb -f
# set up env
require 'rubygems' 
# => true
require 'active_support/core_ext/object/blank'
# => true
require 'confstruct' # 0.2.1
# => true

# set up data
c = Confstruct::Configuration.new("a" => {"b" => {"c" => "d"}})
# => {:a=>{:b=>{:c=>"d"}}}

# access data
c["a"]["b"].class
# => Confstruct::HashWithStructAccess

# ooops!
c["a"]["b"].presence.class
# => ActiveSupport::OrderedHash

Unfortunately, the OrderedHash is not .with_indifferent_access:

# surprise! :-(
c["a"]["b"]["c"]
# => "d"
c["a"]["b"].presence["c"]
# => nil

phorsuedzie avatar Feb 08 '12 16:02 phorsuedzie

This seems to be an artifact of the Delegator implementation I'm using. I've started investigating workarounds, but no luck so far. I'll have to think about the best way to ensure that the right typecasting happens all the way down. Thanks for bringing it to my attention.

mbklein avatar Feb 08 '12 22:02 mbklein