Sha256: f1971c95e85d1e071706554329d9ac1ea39a60742cde9dd5cba4f113d7fac2be

Contents?: true

Size: 943 Bytes

Versions: 2

Compression:

Stored size: 943 Bytes

Contents

module Ohm
  # Ohm has already added its own `to_hash` method. The difference
  # is that it chose the albeit better whitelisted approach.
  #
  # @example
  #   
  #   # 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
  module ToHash
    def to_hash
      atts = attributes + counters
      hash = atts.inject({}) { |h, att| h[att] = send(att); h }
      hash[:id] = @id
      hash
    end
    alias :to_h :to_hash
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ohm-contrib-0.0.16 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.15 lib/ohm/contrib/to_hash.rb