solnic / virtus

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

Set InstanceMethods#to_h as an alias. #269

Closed fnando closed 10 years ago

fnando commented 10 years ago

ActiveRecord serializers require the to_h method. Without this PR, one must create a serializer like the following:

class UserPreferences
  include Virtus.model

  attribute :github, String
  attribute :twitter, String
  attribute :newsletter, Boolean

  alias_method :to_h, :to_hash

  def self.dump(preferences)
    preferences.to_h
  end

  def self.load(preferences)
    new(preferences)
  end
end

The to_h method used to work on the include Virtus era, but not anymore.

solnic commented 10 years ago

That's probably not related to virtus, it never had #to_h so I bet something must've changed in ActiveRecord serializers.

I'll merge this in if it fixes your problem. I just want to mention that all rails-related concerns will be extracted into separate modules or even a separate gem.

fnando commented 10 years ago

You're right. I did some more tests and I think the problem was another library. Sorry about that. You can revert that merge if you want. Using the #to_hash is enough.

Nando Vieira

On May 19, 2014, at 5:24 AM, Piotr Solnica notifications@github.com wrote:

That's probably not related to virtus, it never had #to_h so I bet something must've changed in ActiveRecord serializers.

I'll merge this in if it fixes your problem. I just want to mention that all rails-related concerns will be extracted into separate modules or even a separate gem.

— Reply to this email directly or view it on GitHub.