rfc-3339-attributes 0.1.0
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.
- data/LICENSE +20 -0
- data/README.rdoc +9 -0
- data/Rakefile +26 -0
- data/VERSION.yml +4 -0
- data/lib/active_record/rfc_3339_attributes.rb +42 -0
- data/rails/init.rb +2 -0
- data/test/rfc_3339_attributes_test.rb +48 -0
- data/test/test_helper.rb +63 -0
- metadata +76 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Fingertips, Manfred Stienstra <manfred@fngtps.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
=== RFC-3339-Attributes
|
2
|
+
|
3
|
+
A simple Rails plugin which adds an rfc_3339_attributes and a validates_rf_3339_format_of method
|
4
|
+
to ActiveRecord::Base. This makes it possible to validate DateTime columns.
|
5
|
+
|
6
|
+
class Member < ActiveRecord::Base
|
7
|
+
rfc_3339_attributes :measured_at
|
8
|
+
validates_rf_3339_format_of :measured_at
|
9
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'jeweler'
|
18
|
+
Jeweler::Tasks.new do |s|
|
19
|
+
s.name = "rfc-3339-attributes"
|
20
|
+
s.summary = s.description = "A tiny Rails plugin to allow validation on RFC-3339 datetime attributes."
|
21
|
+
s.homepage = "http://fingertips.github.com"
|
22
|
+
s.email = "manfred@fngtps.com"
|
23
|
+
s.authors = ["Manfred Stienstra"]
|
24
|
+
end
|
25
|
+
rescue LoadError
|
26
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module RFC3339Attributes
|
3
|
+
RFC_3339_REGEXP = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d*)?(Z|(([+-]\d\d):\d\d))$/
|
4
|
+
|
5
|
+
def rfc_3339_attributes(*attr_names)
|
6
|
+
attr_names.each do |attr_name|
|
7
|
+
define_method "#{attr_name}=" do |time|
|
8
|
+
@attributes_cache.delete(attr_name.to_s)
|
9
|
+
instance_variable_set("@#{attr_name}_before_type_cast", time)
|
10
|
+
|
11
|
+
if time.respond_to?(:in_time_zone)
|
12
|
+
time = time.in_time_zone
|
13
|
+
elsif parsed_time = Time.zone.parse(time.to_s)
|
14
|
+
time = parsed_time.in_time_zone
|
15
|
+
end
|
16
|
+
|
17
|
+
@attributes[attr_name.to_s] = time
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validates_rf_3339_format_of(*attr_names)
|
23
|
+
configuration = {
|
24
|
+
:on => :save,
|
25
|
+
:with => RFC_3339_REGEXP,
|
26
|
+
:message => "should be a valid RFC 3339 timestamp"
|
27
|
+
}.update(attr_names.extract_options!)
|
28
|
+
|
29
|
+
send(validation_method(configuration[:on] || :save), configuration) do |record|
|
30
|
+
attr_names.each do |attr_name|
|
31
|
+
value_before_type_cast = record.send(:instance_variable_get, "@#{attr_name}_before_type_cast")
|
32
|
+
next if value_before_type_cast.acts_like?(:time)
|
33
|
+
if value_before_type_cast.blank? and !configuration[:allow_blank]
|
34
|
+
record.errors.add(attr_name, :invalid, :default => "can't be blank", :value => value_before_type_cast)
|
35
|
+
elsif value_before_type_cast.to_s !~ configuration[:with]
|
36
|
+
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value_before_type_cast)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class Member < ActiveRecord::Base
|
4
|
+
rfc_3339_attributes :measured_at
|
5
|
+
|
6
|
+
validates_rf_3339_format_of :measured_at
|
7
|
+
end
|
8
|
+
|
9
|
+
class RFC3339AttributesValidationTest < ActiveSupport::TestCase
|
10
|
+
def setup
|
11
|
+
RFC3339AttributesTest::Initializer.setup_database
|
12
|
+
@member = Member.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
RFC3339AttributesTest::Initializer.teardown_database
|
17
|
+
end
|
18
|
+
|
19
|
+
test "accepts valid timestamps" do
|
20
|
+
%w{
|
21
|
+
2010-03-21T21:20:56+01:00
|
22
|
+
}.each do |timestamp|
|
23
|
+
assert_valid_timestamp(timestamp)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "rejects invalid timestamps" do
|
28
|
+
%w{
|
29
|
+
invalid
|
30
|
+
}.each do |timestamp|
|
31
|
+
assert_invalid_timestamp(timestamp)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def assert_valid_timestamp(timestamp)
|
38
|
+
@member.measured_at = timestamp
|
39
|
+
@member.valid?
|
40
|
+
assert @member.errors.on(:measured_at).blank?, "Expected `#{timestamp}' to be valid"
|
41
|
+
end
|
42
|
+
|
43
|
+
def assert_invalid_timestamp(timestamp)
|
44
|
+
@member.measured_at = timestamp
|
45
|
+
@member.valid?
|
46
|
+
assert !@member.errors.on(:measured_at).blank?, "Expected `#{timestamp}' to be invalid"
|
47
|
+
end
|
48
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module RFC3339AttributesTest
|
2
|
+
module Initializer
|
3
|
+
VENDOR_RAILS = File.expand_path('../../../../../vendor/rails', __FILE__)
|
4
|
+
OTHER_RAILS = File.expand_path('../../../rails', __FILE__)
|
5
|
+
PLUGIN_ROOT = File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
def self.rails_directory
|
8
|
+
if File.exist?(VENDOR_RAILS)
|
9
|
+
VENDOR_RAILS
|
10
|
+
elsif File.exist?(OTHER_RAILS)
|
11
|
+
OTHER_RAILS
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.load_dependencies
|
16
|
+
if rails_directory
|
17
|
+
$:.unshift(File.join(rails_directory, 'activesupport', 'lib'))
|
18
|
+
$:.unshift(File.join(rails_directory, 'activerecord', 'lib'))
|
19
|
+
else
|
20
|
+
require 'rubygems' rescue LoadError
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'test/unit'
|
24
|
+
|
25
|
+
require 'active_support/all'
|
26
|
+
require 'active_support/core_ext'
|
27
|
+
require 'active_support/test_case'
|
28
|
+
require 'active_record'
|
29
|
+
require 'active_record/test_case'
|
30
|
+
require 'active_record/base' # this is needed because of dependency hell
|
31
|
+
|
32
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
33
|
+
require File.join(PLUGIN_ROOT, 'rails', 'init')
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.configure_database
|
37
|
+
Time.zone = 'UTC'
|
38
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
39
|
+
ActiveRecord::Migration.verbose = false
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.setup_database
|
43
|
+
ActiveRecord::Schema.define(:version => 1) do
|
44
|
+
create_table :members do |t|
|
45
|
+
t.column :measured_at, :datetime
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.teardown_database
|
51
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
52
|
+
ActiveRecord::Base.connection.drop_table(table)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.start
|
57
|
+
load_dependencies
|
58
|
+
configure_database
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
RFC3339AttributesTest::Initializer.start
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rfc-3339-attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Manfred Stienstra
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-05 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A tiny Rails plugin to allow validation on RFC-3339 datetime attributes.
|
23
|
+
email: manfred@fngtps.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- VERSION.yml
|
36
|
+
- lib/active_record/rfc_3339_attributes.rb
|
37
|
+
- rails/init.rb
|
38
|
+
- test/rfc_3339_attributes_test.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://fingertips.github.com
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A tiny Rails plugin to allow validation on RFC-3339 datetime attributes.
|
74
|
+
test_files:
|
75
|
+
- test/rfc_3339_attributes_test.rb
|
76
|
+
- test/test_helper.rb
|