Sha256: a9056418a3dd029713effd85bb9efa57321a20494dc44302937626b32da32779

Contents?: true

Size: 905 Bytes

Versions: 1

Compression:

Stored size: 905 Bytes

Contents

require 'rubygems'

gem 'dm-core', '=0.9.6'
require 'dm-core'

module DataMapper
  module Timestamp
    TIMESTAMP_PROPERTIES = {
      :updated_at => lambda { |r| r.updated_at = DateTime.now },
      :updated_on => lambda { |r| r.updated_on = Date.today   },
      :created_at => lambda { |r| r.created_at = DateTime.now if r.new_record? && r.created_at.nil? },
      :created_on => lambda { |r| r.created_on = Date.today   if r.new_record? && r.created_on.nil?},
    }

    def self.included(model)
      model.before :save, :set_timestamp_properties
    end

    private

    def set_timestamp_properties
      if dirty?
        self.class.properties.slice(*TIMESTAMP_PROPERTIES.keys).compact.each do |property|
          TIMESTAMP_PROPERTIES[property.name][self] unless attribute_dirty?(property.name)
        end
      end
    end
  end # module Timestamp

  Resource::append_inclusions Timestamp
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-timestamps-0.9.6 lib/dm-timestamps.rb