Sha256: 18c91800147aa98651a1d076fb1f8815e0f124cbb92ad672f68ea4ce5f34fb77

Contents?: true

Size: 706 Bytes

Versions: 1

Compression:

Stored size: 706 Bytes

Contents

require_relative "datatypes"

module Ohm
  # Provides created_at / updated_at timestamps.
  #
  # @example
  #
  #   class Post < Ohm::Model
  #     include Ohm::Timestamps
  #   end
  #
  #   post = Post.create
  #   post.created_at.to_i == Time.now.utc.to_i
  #   # => true
  #
  #   post = Post[post.id]
  #   post.save
  #   post.updated_at.to_i == Time.now.utc.to_i
  #   # => true
  module Timestamps
    def self.included(model)
      model.attribute :created_at, DataTypes::Type::Timestamp
      model.attribute :updated_at, DataTypes::Type::Timestamp
    end

    def save!
      self.created_at = Time.now.utc.to_i if new?
      self.updated_at = Time.now.utc.to_i

      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ohm-contrib-1.0.0.rc2 lib/ohm/timestamps.rb