public
Description: Sequel Model plugin to implicitly set created_at and updated_at
Clone URL: git://github.com/bricooke/sequel_timestamped.git
Search Repo:
Initial version
bricooke (author)
Tue Mar 11 23:21:45 -0700 2008
commit  4a9c255651aa2058b0089d4f6eaa6b31f9bed916
tree    d18976004930dec9077241b250ae4c13464b804e
...
 
...
1
0
@@ -0,0 +1 @@
0
+coverage
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
@@ -0,0 +1,18 @@
0
+Copyright (c) 2007 Sharon Rosner, Wayne E. Seguin, Aman Gupta
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining a copy
0
+of this software and associated documentation files (the "Software"), to
0
+deal in the Software without restriction, including without limitation the
0
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
0
+sell copies of the Software, and to permit persons to whom the Software is
0
+furnished to do so, subject to the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be included in
0
+all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0
+THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
0
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+If your model is :timestamped, it will automatically set the
0
+:created_at and :updated_at columns if they exist
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
0
@@ -0,0 +1,155 @@
0
+##############################################################################
0
+# Constants
0
+##############################################################################
0
+
0
+PluginName = "sequel_timestamped"
0
+Version = "0.0.1"
0
+Title = "Timestamped Sequel Plugin"
0
+Summary = "Sequel Plugin"
0
+Authors = "Brian Cooke"
0
+Emails = "cookebri@gmail.com"
0
+Homepage = "http://sequel.rubyforge.org"
0
+
0
+##############################################################################
0
+# Gem Management
0
+##############################################################################
0
+require "rake"
0
+require "rake/clean"
0
+require "rake/gempackagetask"
0
+require "rake/rdoctask"
0
+require "fileutils"
0
+
0
+include FileUtils
0
+
0
+CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
0
+
0
+RDocOptions = [
0
+ "--quiet", "--title", Title,
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--main", "README",
0
+ "--inline-source"
0
+]
0
+
0
+desc "Packages up the Sequel Plugin: #{PluginName}."
0
+task :default => [:package]
0
+task :package => [:clean]
0
+
0
+task :doc => [:rdoc]
0
+
0
+Rake::RDocTask.new do |rdoc|
0
+ rdoc.rdoc_dir = "doc/rdoc"
0
+ rdoc.options += RDocOptions
0
+ rdoc.main = "README"
0
+ rdoc.title = Title
0
+ rdoc.rdoc_files.add ["README", "COPYING", "lib/#{PluginName}.rb", "lib/**/*.rb"]
0
+end
0
+
0
+spec = Gem::Specification.new do |s|
0
+ s.name = PluginName
0
+ s.version = Version
0
+ s.platform = Gem::Platform::RUBY
0
+ s.has_rdoc = true
0
+ s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
0
+ s.rdoc_options += RDocOptions# +
0
+ #["--exclude", "^(examples|extras)\/", "--exclude", "lib/sequel.rb"]
0
+ s.summary = Summary
0
+ s.description = Summary
0
+ s.author = Authors
0
+ s.email = Emails
0
+ s.homepage = Homepage
0
+ # change this to the plugin name, if the plugin has command line portion
0
+ #s.executables = ["sequel"]
0
+
0
+ s.add_dependency("sequel_model")
0
+
0
+ s.files = %w(COPYING README Rakefile) + Dir.glob("{bin,doc,spec,lib}/**/*")
0
+
0
+ s.require_path = "lib"
0
+ s.bindir = "bin"
0
+end
0
+
0
+Rake::GemPackageTask.new(spec) do |p|
0
+ p.need_tar = true
0
+ p.gem_spec = spec
0
+end
0
+
0
+task :release => [:package] do
0
+ sh %{rubyforge login}
0
+ sh %{rubyforge add_release sequel #{PluginName} #{Version} pkg/#{PluginName}-#{Version}.tgz}
0
+ sh %{rubyforge add_file sequel #{PluginName} #{Version} pkg/#{PluginName}-#{Version}.gem}
0
+end
0
+
0
+task :install do
0
+ sh %{rake package}
0
+ sh %{sudo gem install pkg/#{PluginName}-#{Version}.gem}
0
+end
0
+
0
+task :install_no_docs do
0
+ sh %{rake package}
0
+ sh %{sudo gem install pkg/#{PluginName}-#{Version}.gem --no-rdoc --no-ri}
0
+end
0
+
0
+task :uninstall => [:clean] do
0
+ sh %{sudo gem uninstall #{PluginName}}
0
+end
0
+
0
+desc "Update docs and upload to rubyforge.org"
0
+task :doc_rforge do
0
+ sh %{rake doc}
0
+ sh %{scp -r doc/rdoc/* ciconia@rubyforge.org:/var/www/gforge-projects/sequel/plugins/#{PluginName}}
0
+end
0
+
0
+##############################################################################
0
+# rSpec
0
+##############################################################################
0
+
0
+require "spec/rake/spectask"
0
+
0
+desc "Run specs with coverage"
0
+Spec::Rake::SpecTask.new("spec") do |spec_task|
0
+ spec_task.spec_opts = File.read("spec/spec.opts").split("\n")
0
+ spec_task.spec_files = FileList["spec/*_spec.rb"].sort
0
+ spec_task.rcov = true
0
+end
0
+
0
+desc "Run specs without coverage"
0
+Spec::Rake::SpecTask.new("spec_no_cov") do |spec_task|
0
+ spec_task.spec_opts = File.read("spec/spec.opts").split("\n")
0
+ spec_task.spec_files = FileList["spec/*_spec.rb"].sort
0
+end
0
+
0
+desc "Run all specs with coverage"
0
+Spec::Rake::SpecTask.new("specs") do |spec_task|
0
+ spec_task.spec_opts = File.read("spec/spec.opts").split("\n")
0
+ spec_task.spec_files = FileList["spec/**/*_spec.rb"].sort
0
+ spec_task.rcov = true
0
+end
0
+
0
+desc "Run all specs without coverage"
0
+Spec::Rake::SpecTask.new("specs_no_cov") do |spec_task|
0
+ spec_task.spec_opts = File.read("spec/spec.opts").split("\n")
0
+ spec_task.spec_files = FileList["spec/**/*_spec.rb"].sort
0
+end
0
+
0
+desc "Run all specs and output html"
0
+Spec::Rake::SpecTask.new("specs_html") do |spec_task|
0
+ spec_task.spec_opts = ["--format", "html"]
0
+ spec_task.spec_files = Dir["spec/**/*_spec.rb"].sort
0
+end
0
+
0
+##############################################################################
0
+# Statistics
0
+##############################################################################
0
+
0
+STATS_DIRECTORIES = [
0
+ %w(Code lib/),
0
+ %w(Spec spec/)
0
+].collect { |name, dir| [ name, "./#{dir}" ] }.select { |name, dir| File.directory?(dir) }
0
+
0
+desc "Report code statistics (KLOCs, etc) from the application"
0
+task :stats do
0
+ require "extra/stats"
0
+ verbose = true
0
+ CodeStatistics.new(*STATS_DIRECTORIES).to_s
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,24 @@
0
+module Sequel
0
+ module Plugins
0
+ module Timestamped
0
+ # Apply the plugin to the model.
0
+ def self.apply(model, options = {})
0
+ model.send(:include, InstanceMethods)
0
+
0
+ model.before_create { set_created_at; set_updated_at }
0
+ model.before_update { set_updated_at }
0
+ end
0
+
0
+ module InstanceMethods
0
+ # Define methods that will be instance-specific here.
0
+ def set_created_at
0
+ self.created_at = Time.now if self.columns.include?(:created_at)
0
+ end
0
+
0
+ def set_updated_at
0
+ self.updated_at = Time.now if self.columns.include?(:updated_at)
0
+ end
0
+ end
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
0
@@ -0,0 +1,64 @@
0
+require File.join(File.dirname(__FILE__), "spec_helper")
0
+
0
+DB = Sequel.sqlite
0
+
0
+
0
+class TimestampedModel < Sequel::Model(:items)
0
+ set_schema do
0
+ primary_key :id
0
+ varchar :name
0
+ datetime :created_at
0
+ datetime :updated_at
0
+ end
0
+
0
+ is :timestamped
0
+end
0
+
0
+class NonTimestampedModel < Sequel::Model(:items)
0
+ set_schema do
0
+ primary_key :id
0
+ varchar :name
0
+ end
0
+
0
+ is :timestamped
0
+end
0
+
0
+describe Sequel::Plugins::Timestamped, "#created_at" do
0
+
0
+ before(:each) do
0
+ TimestampedModel.create_table!
0
+ end
0
+
0
+ it "should handle a model without a created_at field" do
0
+ NonTimestampedModel.create.should be_valid
0
+ end
0
+
0
+ it "should set created_at to Time.now on INSERT" do
0
+ TimestampedModel.create.created_at.should_not be_nil
0
+ end
0
+
0
+
0
+ it "should set updated_at to Time.now on INSERT" do
0
+ TimestampedModel.create.updated_at.should_not be_nil
0
+ end
0
+
0
+
0
+end
0
+
0
+describe Sequel::Plugins::Timestamped, "#updated_at" do
0
+
0
+ it "should set updated_at to Time.now upon UPDATE" do
0
+ t = TimestampedModel.create
0
+ t.updated_at = nil
0
+ t.save
0
+ t.updated_at.should_not be_nil
0
+ end
0
+
0
+ it "should handle a model without an updated_at field" do
0
+ nt = NonTimestampedModel.create
0
+ nt.name = "hi"
0
+ nt.save
0
+ nt.should be_valid
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
0
@@ -0,0 +1,6 @@
0
+--colour
0
+--loadby
0
+mtime
0
+--backtrace
0
+--format
0
+specdoc
0
\ No newline at end of file
...
 
 
...
1
2
0
@@ -0,0 +1,2 @@
0
+require "sequel"
0
+require File.join(File.dirname(__FILE__), "../lib/sequel_timestamped")

Comments

    No one has commented yet.