Sha256: 3b967a2c5bf8db349ef629c9031dd8334e8d149d425ffde40b0fb2937b2f7d20

Contents?: true

Size: 939 Bytes

Versions: 11

Compression:

Stored size: 939 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

11 entries across 11 versions & 1 rubygems

Version Path
ohm-contrib-0.0.27 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.26 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.25 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.24 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.23 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.22 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.21 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.20 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.19 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.18 lib/ohm/contrib/to_hash.rb
ohm-contrib-0.0.17 lib/ohm/contrib/to_hash.rb