Module: Ohm::ToHash

Defined in:
lib/ohm/contrib/to_hash.rb

Overview

TODO:

use super when Ohm has finally release their #to_hash impl.

Ohm has already added its own `to_hash` method. The difference is that it chose the albeit better whitelisted approach.

Examples:


# this is the core Ohm#to_hash implementation
class Post < Ohm::Model
  attribute :body
  def validate
    assert_present :body
  end
end

Post.create.to_hash == { :errors => [[:body, :not_present]] }
# => true

Post.create(:body => "The body").to_hash == { :id => 1 }
# => true

# now this is the all-in-one (kinda Railsy) method.
class Post < Ohm::Model
  attribute :body
end

Post.create(:body => "Body").to_hash == { :id => 1, :body => "Body" }
# => true

Instance Method Summary (collapse)

Instance Method Details

- (Object) to_hash Also known as: to_h



31
32
33
34
35
36
# File 'lib/ohm/contrib/to_hash.rb', line 31

def to_hash
  atts = attributes + counters
  hash = atts.inject({}) { |h, att| h[att] = send(att); h }
  hash[:id] = @id
  hash
end