smart_time 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +86 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/smart_time/active_record_extension.rb +51 -0
- data/lib/smart_time/parser/ruby_date_parser.rb +12 -0
- data/lib/smart_time/parser/ruby_time_parser.rb +11 -0
- data/lib/smart_time/parser/smart_date_parser.rb +17 -0
- data/lib/smart_time/parser/smart_parser.rb +16 -0
- data/lib/smart_time/parser/smart_time_parser.rb +17 -0
- data/lib/smart_time.rb +34 -0
- data/smart_time.gemspec +79 -0
- data/test/helper.rb +23 -0
- data/test/parser/test_smart_date_parser.rb +25 -0
- data/test/parser/test_smart_time_parser.rb +17 -0
- data/test/schema.rb +8 -0
- data/test/test_active_record_extension.rb +45 -0
- data/test/test_smart_time.rb +27 -0
- metadata +182 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
gem "rails", ">= 3.0.0"
|
9
|
+
group :development do
|
10
|
+
gem "sqlite3"
|
11
|
+
gem "shoulda", ">= 0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.6.0"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
gem "mocha"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.7)
|
6
|
+
actionpack (= 3.0.7)
|
7
|
+
mail (~> 2.2.15)
|
8
|
+
actionpack (3.0.7)
|
9
|
+
activemodel (= 3.0.7)
|
10
|
+
activesupport (= 3.0.7)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.14)
|
16
|
+
rack-test (~> 0.5.7)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.7)
|
19
|
+
activesupport (= 3.0.7)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.5.0)
|
22
|
+
activerecord (3.0.7)
|
23
|
+
activemodel (= 3.0.7)
|
24
|
+
activesupport (= 3.0.7)
|
25
|
+
arel (~> 2.0.2)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.7)
|
28
|
+
activemodel (= 3.0.7)
|
29
|
+
activesupport (= 3.0.7)
|
30
|
+
activesupport (3.0.7)
|
31
|
+
arel (2.0.9)
|
32
|
+
builder (2.1.2)
|
33
|
+
erubis (2.6.6)
|
34
|
+
abstract (>= 1.0.0)
|
35
|
+
git (1.2.5)
|
36
|
+
i18n (0.5.0)
|
37
|
+
jeweler (1.6.0)
|
38
|
+
bundler (~> 1.0.0)
|
39
|
+
git (>= 1.2.5)
|
40
|
+
rake
|
41
|
+
mail (2.2.17)
|
42
|
+
activesupport (>= 2.3.6)
|
43
|
+
i18n (>= 0.4.0)
|
44
|
+
mime-types (~> 1.16)
|
45
|
+
treetop (~> 1.4.8)
|
46
|
+
mime-types (1.16)
|
47
|
+
mocha (0.9.12)
|
48
|
+
polyglot (0.3.1)
|
49
|
+
rack (1.2.2)
|
50
|
+
rack-mount (0.6.14)
|
51
|
+
rack (>= 1.0.0)
|
52
|
+
rack-test (0.5.7)
|
53
|
+
rack (>= 1.0)
|
54
|
+
rails (3.0.7)
|
55
|
+
actionmailer (= 3.0.7)
|
56
|
+
actionpack (= 3.0.7)
|
57
|
+
activerecord (= 3.0.7)
|
58
|
+
activeresource (= 3.0.7)
|
59
|
+
activesupport (= 3.0.7)
|
60
|
+
bundler (~> 1.0)
|
61
|
+
railties (= 3.0.7)
|
62
|
+
railties (3.0.7)
|
63
|
+
actionpack (= 3.0.7)
|
64
|
+
activesupport (= 3.0.7)
|
65
|
+
rake (>= 0.8.7)
|
66
|
+
thor (~> 0.14.4)
|
67
|
+
rake (0.8.7)
|
68
|
+
rcov (0.9.9)
|
69
|
+
shoulda (2.11.3)
|
70
|
+
sqlite3 (1.3.3)
|
71
|
+
thor (0.14.6)
|
72
|
+
treetop (1.4.9)
|
73
|
+
polyglot (>= 0.3.1)
|
74
|
+
tzinfo (0.3.27)
|
75
|
+
|
76
|
+
PLATFORMS
|
77
|
+
ruby
|
78
|
+
|
79
|
+
DEPENDENCIES
|
80
|
+
bundler (~> 1.0.0)
|
81
|
+
jeweler (~> 1.6.0)
|
82
|
+
mocha
|
83
|
+
rails (>= 3.0.0)
|
84
|
+
rcov
|
85
|
+
shoulda
|
86
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011
|
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,41 @@
|
|
1
|
+
= smart_time
|
2
|
+
|
3
|
+
The smart_time gem allows your date/time ActiveRecord attributes to be parsed using the formats already defined by you or Rails in Time::DATE_FORMATS and Date::DATE_FORMATS. This allows you to capture date/time from text input fields using format strings that are already used by Rails to convert Dates and Times to Strings. If a :format option is passed smart_time will force the use of the supplied format, otherwise it will attempt to parse using the formats defined in Time::DATE_FORMATS (for Times) and Date::DATE_FORMATS (for Dates). If none of the formats match it will fail back to using the default Rails/Ruby parsing technique.
|
4
|
+
|
5
|
+
== Getting Started
|
6
|
+
|
7
|
+
1. Install the smart_time gem
|
8
|
+
|
9
|
+
gem install smart_time
|
10
|
+
|
11
|
+
2. Use the <tt>smart_time</tt> macro in your models
|
12
|
+
|
13
|
+
class Event < ActiveRecord::Base
|
14
|
+
# Calling smart_time with no parameters activates smart_time for all columns that have a klass of Date or Time (aka db types :date, :time, :datetime, and :timestamp).
|
15
|
+
smart_time
|
16
|
+
|
17
|
+
# Or you can name just the attributes you want to activate
|
18
|
+
# smart_time :start_at, :end_at
|
19
|
+
|
20
|
+
# You can also explicitly pass a format string
|
21
|
+
# smart_time :start_at, :format => "%m/%d/%Y"
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
3. Enjoy! If something breaks, complain to the author or (preferably) fork it, fix it, make sure all tests still pass and create a pull request.
|
26
|
+
|
27
|
+
== Contributing to smart_time
|
28
|
+
|
29
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
30
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
31
|
+
* Fork the project
|
32
|
+
* Start a feature/bugfix branch
|
33
|
+
* Commit and push until you are happy with your contribution
|
34
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
35
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2011 Matthew Daubert. See LICENSE.txt for
|
40
|
+
further details.
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "smart_time"
|
18
|
+
gem.homepage = "http://github.com/mdaubs/smart_time"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Intelligent parsing of date/time attributes}
|
21
|
+
gem.description = %Q{Leverages your app's DATE_FORMATS hash when parsing dates and times or specify your own format on a per-app, per-model, or per-attribute basis. Fails back to the default Ruby implementation.}
|
22
|
+
gem.email = "mdaubert@gmail.com"
|
23
|
+
gem.authors = ["Matthew Daubert"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "smart_time #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SmartTime
|
2
|
+
module ActiveRecordExtension
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def smart_time_attributes
|
11
|
+
@smart_time_attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def smart_time(*args)
|
15
|
+
options = args.extract_options!
|
16
|
+
new_attributes = args
|
17
|
+
first_call = !defined?(@smart_time_attributes)
|
18
|
+
@smart_time_attributes ||= {}
|
19
|
+
|
20
|
+
# If no attributes were specified then add all parsable attributes
|
21
|
+
if new_attributes.empty?
|
22
|
+
columns.each do |column|
|
23
|
+
@smart_time_attributes[column.name.to_sym] = options if SmartTime.can_parse?(column.klass)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
new_attributes.each do |attribute|
|
27
|
+
@smart_time_attributes[attribute.to_sym] = options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Override attribute write method
|
32
|
+
unless first_call
|
33
|
+
self.class_eval do
|
34
|
+
def write_attribute(attr_name, value)
|
35
|
+
if self.class.smart_time_attributes[attr_name.to_sym]
|
36
|
+
value = SmartTime.parse(value, column_for_attribute(attr_name).klass, self.class.smart_time_attributes[attr_name.to_sym])
|
37
|
+
end
|
38
|
+
super(attr_name, value)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SmartTime
|
2
|
+
module Parser
|
3
|
+
class SmartDateParser < SmartParser
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def self._default_formats
|
8
|
+
defined?(Date::DATE_FORMATS) ? Date::DATE_FORMATS.values : []
|
9
|
+
end
|
10
|
+
|
11
|
+
def self._parse_with_format(value, format)
|
12
|
+
Date.strptime(value, format) rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SmartTime
|
2
|
+
module Parser
|
3
|
+
class SmartParser
|
4
|
+
|
5
|
+
def self.parse(value, options={})
|
6
|
+
return _parse_with_format(value, options[:format]) if options[:format].is_a?(String)
|
7
|
+
_default_formats.each do |format|
|
8
|
+
result = _parse_with_format(value, format) if format.is_a?(String)
|
9
|
+
return result if result
|
10
|
+
end
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SmartTime
|
2
|
+
module Parser
|
3
|
+
class SmartTimeParser < SmartParser
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def self._default_formats
|
8
|
+
defined?(Time::DATE_FORMATS) ? Time::DATE_FORMATS.values : []
|
9
|
+
end
|
10
|
+
|
11
|
+
def self._parse_with_format(value, format)
|
12
|
+
Time.strptime(value, format) rescue DateTime.strptime(value, format) rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/smart_time.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module SmartTime
|
2
|
+
extend ActiveSupport::Autoload
|
3
|
+
|
4
|
+
module Parser
|
5
|
+
extend ActiveSupport::Autoload
|
6
|
+
|
7
|
+
autoload :SmartParser
|
8
|
+
autoload :SmartDateParser
|
9
|
+
autoload :SmartTimeParser
|
10
|
+
autoload :RubyDateParser
|
11
|
+
autoload :RubyTimeParser
|
12
|
+
end
|
13
|
+
|
14
|
+
autoload :ActiveRecordExtension
|
15
|
+
|
16
|
+
PARSERS_BY_CLASS = {
|
17
|
+
Date => [Parser::SmartDateParser, Parser::RubyDateParser],
|
18
|
+
Time => [Parser::SmartTimeParser, Parser::RubyTimeParser]
|
19
|
+
}
|
20
|
+
|
21
|
+
def self.can_parse?(klass)
|
22
|
+
PARSERS_BY_CLASS.include?(klass)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.parse(value, klass, options)
|
26
|
+
PARSERS_BY_CLASS[klass].each do |parser|
|
27
|
+
result = parser.parse(value, options)
|
28
|
+
return result if result
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::Base.send(:include, SmartTime::ActiveRecordExtension)
|
33
|
+
|
34
|
+
end
|
data/smart_time.gemspec
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{smart_time}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matthew Daubert"]
|
12
|
+
s.date = %q{2011-05-08}
|
13
|
+
s.description = %q{Leverages your app's DATE_FORMATS hash when parsing dates and times or specify your own format on a per-app, per-model, or per-attribute basis. Fails back to the default Ruby implementation.}
|
14
|
+
s.email = %q{mdaubert@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/smart_time.rb",
|
28
|
+
"lib/smart_time/active_record_extension.rb",
|
29
|
+
"lib/smart_time/parser/ruby_date_parser.rb",
|
30
|
+
"lib/smart_time/parser/ruby_time_parser.rb",
|
31
|
+
"lib/smart_time/parser/smart_date_parser.rb",
|
32
|
+
"lib/smart_time/parser/smart_parser.rb",
|
33
|
+
"lib/smart_time/parser/smart_time_parser.rb",
|
34
|
+
"smart_time.gemspec",
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/parser/test_smart_date_parser.rb",
|
37
|
+
"test/parser/test_smart_time_parser.rb",
|
38
|
+
"test/schema.rb",
|
39
|
+
"test/test_active_record_extension.rb",
|
40
|
+
"test/test_smart_time.rb"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/mdaubs/smart_time}
|
43
|
+
s.licenses = ["MIT"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.7}
|
46
|
+
s.summary = %q{Intelligent parsing of date/time attributes}
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
|
54
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
|
58
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
62
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
63
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
66
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
67
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<rails>, [">= 3.0.0"])
|
71
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
72
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
73
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
74
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
|
75
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
76
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rails/all'
|
11
|
+
require 'test/unit'
|
12
|
+
require 'shoulda'
|
13
|
+
require 'mocha'
|
14
|
+
|
15
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
17
|
+
require 'smart_time'
|
18
|
+
|
19
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
20
|
+
require 'schema.rb'
|
21
|
+
|
22
|
+
class Test::Unit::TestCase
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SmartTime
|
4
|
+
module Parser
|
5
|
+
class TestSmartDateParser < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_parse_with_format
|
8
|
+
assert_equal Date.new(2011,5,1), SmartDateParser.parse("05/01/2011", {:format=>"%m/%d/%Y"})
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_parse_invalid_with_format
|
12
|
+
assert_nil SmartDateParser.parse("ABCDEFG", {:format=>"%m/%d/%Y"})
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_parse_with_default_formats
|
16
|
+
assert_equal Date.new(2011,1,5), SmartDateParser.parse("2011-01-05")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_parse_invalid_with_default_formats
|
20
|
+
assert_nil SmartDateParser.parse("ABCDEFG")
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SmartTime
|
4
|
+
module Parser
|
5
|
+
class TestSmartTimeParser < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_parse_with_format
|
8
|
+
assert_equal Time.new(2011,5,1,14,30), SmartTimeParser.parse("05/01/2011 2:30PM", {:format=>"%m/%d/%Y %l:%M%p"})
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_parse_invalid_with_format
|
12
|
+
assert_nil SmartTimeParser.parse("ABCDEFG", {:format=>"%m/%d/%Y"})
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestActiveRecordExtension < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_smart_time_macro_exists
|
6
|
+
assert ActiveRecord::Base.respond_to?(:smart_time), "ActiveRecord::Base doesn't respond to smart_time"
|
7
|
+
end
|
8
|
+
|
9
|
+
context "first test model" do
|
10
|
+
|
11
|
+
class TestModelA < ActiveRecord::Base
|
12
|
+
def self.table_name() "test_models" end
|
13
|
+
smart_time
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_smart_time_write_attribute
|
17
|
+
SmartTime.expects(:parse).with("05/01/2011", Date, {})
|
18
|
+
TestModelA.new.test_date_attribute = "05/01/2011"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context "second test model" do
|
24
|
+
|
25
|
+
class TestModelB < ActiveRecord::Base
|
26
|
+
def self.table_name() "test_models" end
|
27
|
+
smart_time :test_time_attribute
|
28
|
+
smart_time :test_date_attribute, :format => "%m/%d/%Y"
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_smart_time_write_attribute
|
32
|
+
SmartTime.expects(:parse).with("05/01/2011", Time, {})
|
33
|
+
TestModelB.new.test_time_attribute = "05/01/2011"
|
34
|
+
TestModelB.new.test_datetime_attribute = "06/01/2011"
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_smart_conversion
|
38
|
+
m = TestModelB.new
|
39
|
+
m.test_date_attribute = "12/01/2011"
|
40
|
+
assert_equal Date.new(2011,12,1), m.test_date_attribute
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSmartTime < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_active_record_extension_loaded
|
6
|
+
assert ActiveRecord::Base.included_modules.include?(SmartTime::ActiveRecordExtension)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_can_parse
|
10
|
+
assert SmartTime.can_parse?(Date)
|
11
|
+
assert SmartTime.can_parse?(Time)
|
12
|
+
assert !SmartTime.can_parse?(String)
|
13
|
+
assert !SmartTime.can_parse?(Class)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_parse_with_smart_date_parser
|
17
|
+
SmartTime::Parser::SmartDateParser.expects(:parse).with("05/01/2011", {:format => "ABCDEFG"}).returns("ZZZ")
|
18
|
+
assert_equal "ZZZ", SmartTime.parse("05/01/2011", Date, {:format => "ABCDEFG"})
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_parse_with_ruby_date_parser
|
22
|
+
SmartTime::Parser::SmartDateParser.expects(:parse).with("05/01/2011", {:format => "ABCDEFG"}).returns(nil)
|
23
|
+
SmartTime::Parser::RubyDateParser.expects(:parse).with("05/01/2011", {:format => "ABCDEFG"}).returns("ZZZ")
|
24
|
+
assert_equal "ZZZ", SmartTime.parse("05/01/2011", Date, {:format => "ABCDEFG"})
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Matthew Daubert
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-08 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 3.0.0
|
32
|
+
type: :runtime
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: sqlite3
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: shoulda
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
type: :development
|
59
|
+
prerelease: false
|
60
|
+
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: bundler
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 1
|
70
|
+
- 0
|
71
|
+
- 0
|
72
|
+
version: 1.0.0
|
73
|
+
type: :development
|
74
|
+
prerelease: false
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: jeweler
|
78
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ~>
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 1
|
85
|
+
- 6
|
86
|
+
- 0
|
87
|
+
version: 1.6.0
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *id005
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rcov
|
93
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
type: :development
|
102
|
+
prerelease: false
|
103
|
+
version_requirements: *id006
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: mocha
|
106
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
type: :development
|
115
|
+
prerelease: false
|
116
|
+
version_requirements: *id007
|
117
|
+
description: Leverages your app's DATE_FORMATS hash when parsing dates and times or specify your own format on a per-app, per-model, or per-attribute basis. Fails back to the default Ruby implementation.
|
118
|
+
email: mdaubert@gmail.com
|
119
|
+
executables: []
|
120
|
+
|
121
|
+
extensions: []
|
122
|
+
|
123
|
+
extra_rdoc_files:
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.rdoc
|
126
|
+
files:
|
127
|
+
- .document
|
128
|
+
- Gemfile
|
129
|
+
- Gemfile.lock
|
130
|
+
- LICENSE.txt
|
131
|
+
- README.rdoc
|
132
|
+
- Rakefile
|
133
|
+
- VERSION
|
134
|
+
- lib/smart_time.rb
|
135
|
+
- lib/smart_time/active_record_extension.rb
|
136
|
+
- lib/smart_time/parser/ruby_date_parser.rb
|
137
|
+
- lib/smart_time/parser/ruby_time_parser.rb
|
138
|
+
- lib/smart_time/parser/smart_date_parser.rb
|
139
|
+
- lib/smart_time/parser/smart_parser.rb
|
140
|
+
- lib/smart_time/parser/smart_time_parser.rb
|
141
|
+
- smart_time.gemspec
|
142
|
+
- test/helper.rb
|
143
|
+
- test/parser/test_smart_date_parser.rb
|
144
|
+
- test/parser/test_smart_time_parser.rb
|
145
|
+
- test/schema.rb
|
146
|
+
- test/test_active_record_extension.rb
|
147
|
+
- test/test_smart_time.rb
|
148
|
+
has_rdoc: true
|
149
|
+
homepage: http://github.com/mdaubs/smart_time
|
150
|
+
licenses:
|
151
|
+
- MIT
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
hash: 2081921641939309139
|
163
|
+
segments:
|
164
|
+
- 0
|
165
|
+
version: "0"
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
none: false
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
segments:
|
172
|
+
- 0
|
173
|
+
version: "0"
|
174
|
+
requirements: []
|
175
|
+
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 1.3.7
|
178
|
+
signing_key:
|
179
|
+
specification_version: 3
|
180
|
+
summary: Intelligent parsing of date/time attributes
|
181
|
+
test_files: []
|
182
|
+
|