dm-timestamps 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +14 -0
- data/lib/dm-timestamps.rb +27 -1
- data/lib/dm-timestamps/version.rb +1 -1
- data/spec/integration/timestamps_spec.rb +44 -0
- data/spec/spec_helper.rb +1 -1
- metadata +5 -5
data/README.txt
CHANGED
@@ -1,3 +1,17 @@
|
|
1
1
|
= dm-timestamps
|
2
2
|
|
3
3
|
DataMapper plugin which adds "magic" to created_at, created_on, et cetera.
|
4
|
+
|
5
|
+
= Using
|
6
|
+
|
7
|
+
This plugin works by looking for the "created_at," "updated_at," "created_on"
|
8
|
+
and "updated_on" properties. If they exist, it does the right thing by setting
|
9
|
+
them or updating them. The *at properties should be of type DateTime, while the
|
10
|
+
*on properties should be of type Date.
|
11
|
+
|
12
|
+
Alternatively, you can have all of this setup for you if you use the "timestamps"
|
13
|
+
helper:
|
14
|
+
|
15
|
+
timestamps :at # Add created_at and updated_at
|
16
|
+
timestamps :on # Add created_on and updated_on
|
17
|
+
timestamps :created_at, :updated_on # Add these only
|
data/lib/dm-timestamps.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
gem 'dm-core', '
|
3
|
+
gem 'dm-core', '~>0.9.7'
|
4
4
|
require 'dm-core'
|
5
5
|
|
6
6
|
module DataMapper
|
@@ -14,6 +14,7 @@ module DataMapper
|
|
14
14
|
|
15
15
|
def self.included(model)
|
16
16
|
model.before :save, :set_timestamp_properties
|
17
|
+
model.send :extend, ClassMethods
|
17
18
|
end
|
18
19
|
|
19
20
|
private
|
@@ -25,6 +26,31 @@ module DataMapper
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
def timestamps(*args)
|
32
|
+
if args.empty? then raise ArgumentError, "You need to pass at least one argument." end
|
33
|
+
|
34
|
+
args.each do |ts|
|
35
|
+
case ts
|
36
|
+
when :at
|
37
|
+
property :created_at, DateTime
|
38
|
+
property :updated_at, DateTime
|
39
|
+
when :on
|
40
|
+
property :created_on, Date
|
41
|
+
property :updated_on, Date
|
42
|
+
else
|
43
|
+
unless TIMESTAMP_PROPERTIES.keys.include?(ts)
|
44
|
+
raise InvalidTimestampName, "Invalid timestamp property '#{ts}'."
|
45
|
+
end
|
46
|
+
|
47
|
+
property ts, DateTime
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class InvalidTimestampName < RuntimeError; end
|
28
54
|
end # module Timestamp
|
29
55
|
|
30
56
|
Resource::append_inclusions Timestamp
|
@@ -107,5 +107,49 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
describe "inclusion" do
|
111
|
+
before :each do
|
112
|
+
@klass = Class.new do
|
113
|
+
include DataMapper::Resource
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should provide #timestamps" do
|
118
|
+
@klass.should respond_to(:timestamps)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should set the *at properties" do
|
122
|
+
@klass.timestamps :at
|
123
|
+
|
124
|
+
@klass.properties.should have_property(:created_at)
|
125
|
+
@klass.properties[:created_at].type.should == DateTime
|
126
|
+
@klass.properties.should have_property(:updated_at)
|
127
|
+
@klass.properties[:updated_at].type.should == DateTime
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should set the *on properties" do
|
131
|
+
@klass.timestamps :on
|
132
|
+
|
133
|
+
@klass.properties.should have_property(:created_on)
|
134
|
+
@klass.properties[:created_on].type.should == Date
|
135
|
+
@klass.properties.should have_property(:updated_on)
|
136
|
+
@klass.properties[:updated_on].type.should == Date
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should set multiple properties" do
|
140
|
+
@klass.timestamps :created_at, :updated_on
|
141
|
+
|
142
|
+
@klass.properties.should have_property(:created_at)
|
143
|
+
@klass.properties.should have_property(:updated_on)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should fail on unknown property name" do
|
147
|
+
lambda { @klass.timestamps :wowee }.should raise_error(DataMapper::Timestamp::InvalidTimestampName)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should fail on empty arguments" do
|
151
|
+
lambda { @klass.timestamps }.should raise_error(ArgumentError)
|
152
|
+
end
|
153
|
+
end
|
110
154
|
end
|
111
155
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,7 +10,7 @@ def load_driver(name, default_uri)
|
|
10
10
|
lib = "do_#{name}"
|
11
11
|
|
12
12
|
begin
|
13
|
-
gem lib, '
|
13
|
+
gem lib, '~>0.9.7'
|
14
14
|
require lib
|
15
15
|
DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
|
16
16
|
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-timestamps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Foy Savas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-11-18 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.
|
23
|
+
version: 0.9.7
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hoe
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.
|
33
|
+
version: 1.8.2
|
34
34
|
version:
|
35
35
|
description: DataMapper plugin for magical timestamps
|
36
36
|
email:
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
requirements: []
|
79
79
|
|
80
80
|
rubyforge_project: datamapper
|
81
|
-
rubygems_version: 1.
|
81
|
+
rubygems_version: 1.3.1
|
82
82
|
signing_key:
|
83
83
|
specification_version: 2
|
84
84
|
summary: DataMapper plugin for magical timestamps
|