Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Cleanup #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions lib/dm-timestamps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
module DataMapper
module Timestamps
TIMESTAMP_PROPERTIES = {
:updated_at => [ DateTime, lambda { |r| DateTime.now } ],
:updated_on => [ Date, lambda { |r| Date.today } ],
:created_at => [ DateTime, lambda { |r| r.created_at || (DateTime.now if r.new?) } ],
:created_on => [ Date, lambda { |r| r.created_on || (Date.today if r.new?) } ],
:updated_at => [
DateTime, lambda { |r| DateTime.now }
],

:updated_on => [
Date, lambda { |r| Date.today }
],

:created_at => [
DateTime, lambda { |r| r.created_at || (DateTime.now if r.new?) }
],

:created_on => [
Date, lambda { |r| r.created_on || (Date.today if r.new?) }
],
}.freeze

def self.included(model)
Expand All @@ -23,8 +34,7 @@ def touch
private

def set_timestamps_on_save
return unless dirty?
set_timestamps
set_timestamps if dirty?
end

def set_timestamps
Expand All @@ -35,33 +45,35 @@ def set_timestamps
end
end

class InvalidTimestampName < RuntimeError; end

module ClassMethods
def timestamps(*names)
raise ArgumentError, 'You need to pass at least one argument' if names.empty?
if names.empty?
raise(ArgumentError,'You need to pass at least one argument')
end

names.each do |name|
case name
when *TIMESTAMP_PROPERTIES.keys
options = { :required => true }
options = {:required => true}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we do add spaces here


if Property.accepted_options.include?(:auto_validation)
options.update(:auto_validation => false)
end

property name, TIMESTAMP_PROPERTIES[name].first, options
property(name,TIMESTAMP_PROPERTIES[name].first,options)
when :at
timestamps(:created_at, :updated_at)
when :on
timestamps(:created_on, :updated_on)
else
raise InvalidTimestampName, "Invalid timestamp property name '#{name}'"
raise(InvalidTimestampName,"Invalid timestamp property name '#{name}'")
end
end
end
end # module ClassMethods

class InvalidTimestampName < RuntimeError; end

Model.append_inclusions self
end # module Timestamp

Expand Down