spira-timestamps 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTc4YmQ4YjQ4ZjA4ZmE2M2FkNWQzYzIwNmE0YzMzNzk1OTUwZDkyOA==
5
+ data.tar.gz: !binary |-
6
+ NmU3NmYzZWVmNGZjZWNkMWJjODYyY2RhZTNjODIzNjA2ODJmNWRiNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NWIwYjQ1YzIyNjNiMGNhMjlkODM1ZWE3ZjJlMzUxNTNkMDMyZDU2Y2JhZTZk
10
+ YzExMWFmZjRkZDJiZWY3NGMyYTg5YThkZGExYjNjN2ZmMzJiYzIzOTE2NmZl
11
+ MDA5MDM0YTg5OGEwZjIyNGJhODZmM2Q4MmQ2NDJjMDJjYzE4NGQ=
12
+ data.tar.gz: !binary |-
13
+ YTQzNDA0ZDM2YTNhMmY0NjhlNmIzZTVlMWE4ZmY4N2E1YTQwN2ZkMzM4MWI4
14
+ MDI4NDE0ZGM5NGJjNWE4ZWNlZDZkMDI4MmY5OWExZmVhYmFhYTU2NTAzMzIw
15
+ NDlmOGVhMTg3NDgyNjFhZWU2ZGE4ZWUzMzYxMWRhMjYwY2FlZmY=
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ before_install: gem install bundler
3
+ rvm:
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - "2.1.1"
7
+ - "2.1.2"
8
+ - jruby-19mode # JRuby in 1.9 mode
9
+ - rbx
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nicholas Hurden
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # spira-timestamps
2
+
3
+ Add automatic timestamps to your Spira models.
4
+
5
+ spira-timestamps adds `created` and `updated` `DateTime` attributes to
6
+ your model with the Dublin Core [`created`](http://purl.org/dc/terms/created) and
7
+ [`modified`](http://purl.org/dc/terms/modified) predicates.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'spira-timestamps'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install spira-timestamps
24
+
25
+ ## Usage
26
+
27
+ To add timestamps to a model, just add `include Spira::Timestamps` to
28
+ your model class.
29
+
30
+ For example:
31
+ ```ruby
32
+ class Person < Spira::Base
33
+ include Spira::Timestamps
34
+
35
+ property :name, predicate: FOAF.name, type: String
36
+ end
37
+ ```
38
+
39
+ Timestamps are updated automatically when you `save` your model.
40
+
41
+ You can access the timestamps either as `DateTime` objects via the
42
+ `created_at` and `updated_at` properties or as `Date` objects via the
43
+ `created_on` and `updated_on` properties. For example:
44
+
45
+ > bob = Person.new(name: 'Bob').save
46
+ => <Person:2170778720 @subject: _:g2170778680>
47
+ > bob.created_at
48
+ => #<DateTime: 2014-12-08T20:48:27+11:00 ...>
49
+ > bob.created_on
50
+ => #<Date: 2014-12-08 ...>
51
+ > bob.touch
52
+ => <Person:2170778720 @subject: _:g2170778680>
53
+ > bob.updated_at
54
+ => #<DateTime: 2014-12-08T20:49:41+11:00 ...>
55
+ > bob.updated_on
56
+ => #<Date: 2014-12-08 ...>
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/nhurden/spira-timestamps/fork )
61
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
64
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task :default => [:spec]
@@ -0,0 +1,58 @@
1
+ # coding: utf-8
2
+
3
+ require "spira/timestamps/version"
4
+ require 'active_support/core_ext/object/try'
5
+
6
+ module Spira
7
+ module Timestamps
8
+ def self.included(model)
9
+ model.before_save :before_save
10
+ model.extend ClassMethods
11
+ model.timestamps
12
+ end
13
+
14
+ # Update this model's `updated` time without making any other changes.
15
+ def touch
16
+ update_timestamps
17
+ save
18
+ end
19
+
20
+ private
21
+
22
+ def before_save
23
+ update_timestamps if changed?
24
+ end
25
+
26
+ def update_timestamps
27
+ self.created = self.created || DateTime.now
28
+ self.updated = DateTime.now
29
+ end
30
+
31
+ module ClassMethods
32
+ # Add timestamps to this model.
33
+ def timestamps
34
+ property :created, predicate: RDF::DC.created
35
+ add_created_aliases
36
+
37
+ property :updated, predicate: RDF::DC.modified
38
+ add_updated_aliases
39
+ end
40
+
41
+ private
42
+
43
+ def add_created_aliases
44
+ alias_attribute :created_at, :created
45
+
46
+ define_method("created_on") { self.created.try(:to_date) }
47
+ define_method("created_on=") { |date| self.created = date.try(:to_datetime) }
48
+ end
49
+
50
+ def add_updated_aliases
51
+ alias_attribute :updated_at, :updated
52
+
53
+ define_method("updated_on") { self.updated.try(:to_date) }
54
+ define_method("updated_on=") { |date| self.updated = date.try(:to_datetime) }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+
3
+ module Spira
4
+ module Timestamps
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Spira::Timestamps" do
4
+ shared_examples "a timestamped resource" do
5
+ context "creating a new resource" do
6
+ it "does not override existing created dates" do
7
+ bob = Person.new(name: "Bob")
8
+ past_dt = DateTime.now - 123
9
+ bob.created = past_dt
10
+ bob.save
11
+
12
+ expect(bob.created_at).to eq(past_dt)
13
+ expect(bob.created_on).to eq(past_dt.to_date)
14
+ end
15
+
16
+ it "sets the created dates if not already set" do
17
+ bob = Person.new(name: "Bob")
18
+
19
+ expect(bob.created_at).to be_nil
20
+ expect(bob.created_on).to be_nil
21
+ expect(bob.created).to be_nil
22
+
23
+ bob.save
24
+
25
+ expect(bob.created_at).to be_a(DateTime)
26
+ expect(bob.created_on).to be_a(Date)
27
+ expect(bob.created).to eq(bob.created_at)
28
+ end
29
+
30
+ it "sets the updated_at and updated_on dates" do
31
+ bob = Person.new(name: "Bob").save
32
+
33
+ expect(bob.updated_at).to be_a(DateTime)
34
+ expect(bob.updated_on).to be_a(Date)
35
+ expect(bob.updated).to eq(bob.updated_at)
36
+ end
37
+ end
38
+
39
+ context "modifying a resource" do
40
+ it "does not update the created_at and created_on dates" do
41
+ bob = Person.new(name: "Bob").save
42
+
43
+ old_created_at = bob.created_at
44
+ old_created_on = bob.created_on
45
+
46
+ bob.name = "Bob Two"
47
+ bob.save
48
+
49
+ expect(bob.created_at).to eq(old_created_at)
50
+ expect(bob.created_on).to eq(old_created_on)
51
+ end
52
+
53
+ it "updates the updated_at and updated_on dates" do
54
+ bob = Person.new(name: "Bob").save
55
+
56
+ old_updated_at = bob.updated_at
57
+ old_updated_on = bob.updated_on
58
+
59
+ tomorrow = DateTime.now + 1
60
+ allow(DateTime).to receive(:now).and_return(tomorrow)
61
+
62
+ bob.name = "Bob Two"
63
+ bob.save
64
+
65
+ expect(bob.updated_at).to_not eq(old_updated_at)
66
+ expect(bob.updated_on).to_not eq(old_updated_on)
67
+
68
+ expect(bob.updated_at).to eq(tomorrow)
69
+ expect(bob.updated_on).to eq(tomorrow.to_date)
70
+ end
71
+ end
72
+
73
+ context "saving a clean resource" do
74
+ it "does not update the updated_at and updated_on dates" do
75
+ bob = Person.new(name: "Bob").save
76
+
77
+ old_updated_at = bob.updated_at
78
+ old_updated_on = bob.updated_on
79
+
80
+ bob.save
81
+
82
+ expect(bob.updated_at).to eq(old_updated_at)
83
+ expect(bob.updated_on).to eq(old_updated_on)
84
+ end
85
+ end
86
+
87
+ context "touching a resource" do
88
+ it "updates the updated_at and updated_on dates" do
89
+ bob = Person.new(name: "Bob").save
90
+
91
+ now = DateTime.now
92
+ allow(DateTime).to receive(:now).and_return(now)
93
+
94
+ bob.touch
95
+
96
+ expect(bob.updated_at).to eq(now)
97
+ expect(bob.updated_on).to eq(now.to_date)
98
+ end
99
+
100
+ it "does not update the created_at and created_on dates" do
101
+ bob = Person.new(name: "Bob").save
102
+
103
+ old_created_at = bob.created_at
104
+ old_created_on = bob.created_on
105
+
106
+ bob.touch
107
+
108
+ expect(bob.created_at).to eq(old_created_at)
109
+ expect(bob.created_on).to eq(old_created_on)
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "implicitly defined timestamps" do
115
+ before do
116
+ Object.send(:remove_const, :Person) if defined? Person
117
+
118
+ class Person < Spira::Base
119
+ include Spira::Timestamps
120
+
121
+ configure :base_uri => "http://example.org/example/people"
122
+ property :name, predicate: FOAF.name, type: String
123
+ end
124
+ end
125
+
126
+ it_behaves_like "a timestamped resource"
127
+ end
128
+ end
@@ -0,0 +1,4 @@
1
+ require 'spira'
2
+ require 'spira/timestamps'
3
+
4
+ Spira.repository = RDF::Repository.new
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spira/timestamps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spira-timestamps"
8
+ spec.version = Spira::Timestamps::VERSION
9
+ spec.authors = ["Nicholas Hurden"]
10
+ spec.email = ["git@nhurden.com"]
11
+ spec.summary = %q{A Spira plugin for automatic timestamps}
12
+ spec.description = spec.summary
13
+ #spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ spec.add_runtime_dependency "spira", "~> 0.7"
24
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.7"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.1"
29
+ spec.add_development_dependency "yard", "~> 0.6"
30
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spira-timestamps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Hurden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spira
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ description: A Spira plugin for automatic timestamps
98
+ email:
99
+ - git@nhurden.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/spira/timestamps.rb
111
+ - lib/spira/timestamps/version.rb
112
+ - spec/integration/timestamps_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spira-timestamps.gemspec
115
+ homepage:
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: 1.9.3
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: A Spira plugin for automatic timestamps
139
+ test_files:
140
+ - spec/integration/timestamps_spec.rb
141
+ - spec/spec_helper.rb
142
+ has_rdoc: