smarter_dates 0.2.7.8
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/.gitignore +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +38 -0
- data/Rakefile +43 -0
- data/VERSION.yml +5 -0
- data/lib/smarter_dates.rb +59 -0
- data/smarter_dates.gemspec +52 -0
- data/test/smarter_dates_test.rb +50 -0
- metadata +92 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [Paul Belt]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
20
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= SmarterDates -- Natural languange date parsing for all date/datetime attributes
|
2
|
+
|
3
|
+
machina to automatically parse (with the chronic gem) date/datetime attributes upon assignment
|
4
|
+
|
5
|
+
|
6
|
+
== Example
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__),'lib','smarter_dates')
|
9
|
+
|
10
|
+
class MyObject
|
11
|
+
attr_accessor :birth_d
|
12
|
+
|
13
|
+
include SmarterDates
|
14
|
+
end
|
15
|
+
|
16
|
+
obj = MyObject.new
|
17
|
+
obj.birth_d = '7 days ago'
|
18
|
+
|
19
|
+
puts obj.birth_d
|
20
|
+
# => Thu Apr 22 12:00:00 -0500 1976
|
21
|
+
|
22
|
+
== Use case
|
23
|
+
|
24
|
+
Humans want to think of date and datetime attributes in a natural manner.
|
25
|
+
Standard ruby Date and DateTime objects do not support this well.
|
26
|
+
|
27
|
+
== Installation
|
28
|
+
|
29
|
+
% gem install smarter_dates
|
30
|
+
|
31
|
+
== License
|
32
|
+
|
33
|
+
Copyright (c) 2010 [Paul Belt], released under the MIT license
|
34
|
+
|
35
|
+
== Support
|
36
|
+
|
37
|
+
http://github.com/belt/smarter_dates
|
38
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
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 smarter_dates gem.'
|
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
|
+
desc 'Generate documentation for the smarter_dates gem.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'SmarterDates'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler::Tasks.new do |gemspec|
|
28
|
+
gemspec.name = 'smarter_dates'
|
29
|
+
gemspec.summary = 'machina to automatically parse date/datetime attributes upon assignment.'
|
30
|
+
gemspec.description = "Humans want to think of date and datetime attributes in a natural manner.\nStandard ruby Date and DateTime objects do not support this well."
|
31
|
+
gemspec.email = 'Paul Belt'
|
32
|
+
gemspec.authors = ['Paul Belt']
|
33
|
+
gemspec.homepage = 'http://github.com/belt/smarter_dates'
|
34
|
+
gemspec.extra_rdoc_files = ['README.rdoc']
|
35
|
+
gemspec.rdoc_options = ['--charset=UTF-8']
|
36
|
+
gemspec.add_dependency('chronic', '~> 0.2.3')
|
37
|
+
gemspec.rubyforge_project = 'smarter_dates'
|
38
|
+
end
|
39
|
+
Jeweler::GemcutterTasks.new
|
40
|
+
rescue LoadError
|
41
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
42
|
+
end
|
43
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module SmarterDates
|
2
|
+
require 'chronic'
|
3
|
+
|
4
|
+
def self.included( klass ) # :nodoc:
|
5
|
+
dt_attributes = []
|
6
|
+
db_migrated = true
|
7
|
+
if defined?(Rails)
|
8
|
+
begin
|
9
|
+
dt_attributes.concat(klass.column_names.select{|k|k.match(/_(?:at|on|dt|d)$/)})
|
10
|
+
Rails.logger.debug(RuntimeError,"unused include - #{klass.class.to_s} does not have any attributes ending in _at, _on, _dt, or _d") if db_migrated && dt_attributes.empty?
|
11
|
+
rescue ActiveRecord::StatementInvalid => e
|
12
|
+
db_migrated = false
|
13
|
+
rescue => e
|
14
|
+
Rails.logger.debug(e.inspect)
|
15
|
+
db_migrated = false
|
16
|
+
end
|
17
|
+
else
|
18
|
+
dt_attributes.concat(klass.instance_methods.select{|k|k.match(/_(?:at|on|dt|d)$/)})
|
19
|
+
end
|
20
|
+
|
21
|
+
# :call-seq:
|
22
|
+
# _on(string)
|
23
|
+
# _at(string)
|
24
|
+
# _dt(string)
|
25
|
+
# _d(string)
|
26
|
+
#
|
27
|
+
# Any attribute ending in _at, _on, _dt, or _d are parsed by Chronic.parse
|
28
|
+
# (for flexibility). Values are passed as is to Chronic.parse()
|
29
|
+
#
|
30
|
+
# == Arguments
|
31
|
+
# <tt>string</tt>:: A string
|
32
|
+
|
33
|
+
dt_attributes.each do |k|
|
34
|
+
parse = Proc.new do |val|
|
35
|
+
begin
|
36
|
+
dt = Chronic.parse(val.to_s(:db))
|
37
|
+
rescue
|
38
|
+
dt = Chronic.parse(val.to_s)
|
39
|
+
rescue
|
40
|
+
dt = DateTime.parse(val)
|
41
|
+
rescue
|
42
|
+
dt = Date.parse(val)
|
43
|
+
rescue
|
44
|
+
dt = val
|
45
|
+
end
|
46
|
+
|
47
|
+
if respond_to?(:write_attribute)
|
48
|
+
write_attribute(k,dt)
|
49
|
+
else
|
50
|
+
instance_variable_set(:"@#{k}",dt)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
klass.send(:define_method, "#{k}=".to_sym, &parse)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# /module
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{smarter_dates}
|
8
|
+
s.version = "0.2.7.8"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Paul Belt"]
|
12
|
+
s.date = %q{2010-10-20}
|
13
|
+
s.description = %q{Humans want to think of date and datetime attributes in a natural manner.
|
14
|
+
Standard ruby Date and DateTime objects do not support this well.}
|
15
|
+
s.email = %q{Paul Belt}
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"lib/smarter_dates.rb",
|
26
|
+
"smarter_dates.gemspec",
|
27
|
+
"test/smarter_dates_test.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/belt/smarter_dates}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubyforge_project = %q{smarter_dates}
|
33
|
+
s.rubygems_version = %q{1.3.7}
|
34
|
+
s.summary = %q{machina to automatically parse date/datetime attributes upon assignment.}
|
35
|
+
s.test_files = [
|
36
|
+
"test/smarter_dates_test.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<chronic>, ["~> 0.2.3"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<chronic>, ["~> 0.2.3"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<chronic>, ["~> 0.2.3"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require File.join(File.dirname(__FILE__),'..','lib','smarter_dates')
|
4
|
+
|
5
|
+
class Model
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :birth_d
|
8
|
+
attr_accessor :meeting_dt
|
9
|
+
attr_accessor :created_on
|
10
|
+
attr_accessor :updated_at
|
11
|
+
|
12
|
+
include SmarterDates
|
13
|
+
end
|
14
|
+
|
15
|
+
class SmarterDatesTest < Test::Unit::TestCase
|
16
|
+
def setup
|
17
|
+
@model = Model.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_date_parsing
|
21
|
+
now = Time.now
|
22
|
+
last_week = now - 60 * 60 * 24 * 7
|
23
|
+
yesterday = now - 60 * 60 * 24
|
24
|
+
|
25
|
+
@model.name = "Paul Belt"
|
26
|
+
@model.birth_d = "22 April 1976"
|
27
|
+
@model.meeting_dt = "today"
|
28
|
+
@model.created_on = "one week ago"
|
29
|
+
@model.updated_at = "yesterday"
|
30
|
+
|
31
|
+
assert_equal "Paul Belt", @model.name
|
32
|
+
|
33
|
+
assert_equal 1976, @model.birth_d.year
|
34
|
+
assert_equal 4, @model.birth_d.mon
|
35
|
+
assert_equal 22, @model.birth_d.mday
|
36
|
+
|
37
|
+
assert_equal now.year, @model.meeting_dt.year
|
38
|
+
assert_equal now.month, @model.meeting_dt.mon
|
39
|
+
assert_equal now.mday, @model.meeting_dt.mday
|
40
|
+
|
41
|
+
assert_equal last_week.year, @model.created_on.year
|
42
|
+
assert_equal last_week.month, @model.created_on.mon
|
43
|
+
assert_equal last_week.day, @model.created_on.mday
|
44
|
+
|
45
|
+
assert_equal yesterday.year, @model.updated_at.year
|
46
|
+
assert_equal yesterday.month, @model.updated_at.mon
|
47
|
+
assert_equal yesterday.day, @model.updated_at.mday
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smarter_dates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 83
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 7
|
10
|
+
- 8
|
11
|
+
version: 0.2.7.8
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Paul Belt
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-20 00:00:00 -04:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: chronic
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 17
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
- 2
|
34
|
+
- 3
|
35
|
+
version: 0.2.3
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
description: |-
|
39
|
+
Humans want to think of date and datetime attributes in a natural manner.
|
40
|
+
Standard ruby Date and DateTime objects do not support this well.
|
41
|
+
email: Paul Belt
|
42
|
+
executables: []
|
43
|
+
|
44
|
+
extensions: []
|
45
|
+
|
46
|
+
extra_rdoc_files:
|
47
|
+
- README.rdoc
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- LICENSE
|
51
|
+
- README.rdoc
|
52
|
+
- Rakefile
|
53
|
+
- VERSION.yml
|
54
|
+
- lib/smarter_dates.rb
|
55
|
+
- smarter_dates.gemspec
|
56
|
+
- test/smarter_dates_test.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/belt/smarter_dates
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: smarter_dates
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: machina to automatically parse date/datetime attributes upon assignment.
|
91
|
+
test_files:
|
92
|
+
- test/smarter_dates_test.rb
|