solnic / virtus

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

Readme example not working for Json Attribute #245

Closed fgarcia closed 9 years ago

fgarcia commented 10 years ago

There are some examples using a custom Json attribute:

class Json < Virtus::Attribute
  def coerce(value)
    value.is_a?(::Hash) ? value : JSON.parse(value)
  end
end

class User
  include Virtus.model

  attribute :info, Json
end

As of Ruby 2.1.0 MRI I get this error because coerce does not check for nil (but it was OK some time ago)

2.1.0 (main):0 > user = User.new
TypeError: no implicit conversion of nil into String
from /opt/rubies/2.1.0/lib/ruby/2.1.0/json/common.rb:155:in `initialize'