solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

Hash with a default_proc being set #347

Open pnomolos opened 8 years ago

pnomolos commented 8 years ago

I'm trying to get a default_proc set for attributes, and I feel like either I'm missing something obvious or something wacky is going on. I've tried all of the following with no success:

attribute :features, Hash, default: Hash.new{ |h, k| h[k] = 0 }
attribute :features, Hash, default: ->(*){ Hash.new{ |h, k| h[k] = 0 } }
attribute :features, Hash, default: :create_default_proc

def create_default_proc
  Hash.new{ |h, k| h[k] = 0 }
end
attribute :features, Hash.new{ |h, k| h[k] = 0 }

In every case, I end up with something like:

obj = Foobar.new
obj.features # => {}

# The following should be 0
obj.features["blah"] # => nil 

# The following should not be nil
obj.features.default_proc # => nil