delocalize 0.2.3 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +2 -2
- data/VERSION +1 -1
- data/lib/delocalize/localized_date_time_parser.rb +17 -13
- data/lib/delocalize/rails_ext/active_record.rb +3 -1
- data/test/delocalize_test.rb +52 -16
- metadata +26 -6
data/README.markdown
CHANGED
@@ -12,7 +12,7 @@ You can use delocalize either as a gem (preferred) or as a Rails plugin.
|
|
12
12
|
|
13
13
|
To use the gem version, put the following gem requirement in your `Gemfile`:
|
14
14
|
|
15
|
-
gem "delocalize"
|
15
|
+
gem "delocalize"
|
16
16
|
|
17
17
|
To install it as a plugin, fire up your terminal, go to your Rails app and type:
|
18
18
|
|
@@ -131,6 +131,6 @@ People who have contributed to delocalize (in no particular order):
|
|
131
131
|
* Implement AM/PM support
|
132
132
|
* Cleanup, cleanup, cleanup ...
|
133
133
|
|
134
|
-
Copyright (c) 2009-
|
134
|
+
Copyright (c) 2009-2011 Clemens Kofler <clemens@railway.at>
|
135
135
|
<http://www.railway.at/>
|
136
136
|
Released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
@@ -3,6 +3,22 @@
|
|
3
3
|
# * proper documentation (comments)
|
4
4
|
module Delocalize
|
5
5
|
class LocalizedDateTimeParser
|
6
|
+
# extend/change this according to your needs by merging your custom regexps
|
7
|
+
REGEXPS = {
|
8
|
+
'%B' => "(#{Date::MONTHNAMES.compact.join('|')})", # long month name
|
9
|
+
'%b' => "(#{Date::ABBR_MONTHNAMES.compact.join('|')})", # short month name
|
10
|
+
'%m' => "(\\d{2})", # numeric month
|
11
|
+
'%A' => "(#{Date::DAYNAMES.join('|')})", # full day name
|
12
|
+
'%a' => "(#{Date::ABBR_DAYNAMES.join('|')})", # short day name
|
13
|
+
'%Y' => "(\\d{4})", # long year
|
14
|
+
'%y' => "(\\d{2})", # short year
|
15
|
+
'%e' => "(\\s\\d|\\d{2})", # short day
|
16
|
+
'%d' => "(\\d{2})", # full day
|
17
|
+
'%H' => "(\\d{2})", # hour (24)
|
18
|
+
'%M' => "(\\d{2})", # minute
|
19
|
+
'%S' => "(\\d{2})" # second
|
20
|
+
}
|
21
|
+
|
6
22
|
class << self
|
7
23
|
def parse(datetime, type)
|
8
24
|
return unless datetime
|
@@ -49,19 +65,7 @@ module Delocalize
|
|
49
65
|
end
|
50
66
|
|
51
67
|
def apply_regex(format)
|
52
|
-
#
|
53
|
-
format.gsub('%B', "(#{Date::MONTHNAMES.compact.join('|')})"). # long month name
|
54
|
-
gsub('%b', "(#{Date::ABBR_MONTHNAMES.compact.join('|')})"). # short month name
|
55
|
-
gsub('%m', "(\\d{2})"). # numeric month
|
56
|
-
gsub('%A', "(#{Date::DAYNAMES.join('|')})"). # full day name
|
57
|
-
gsub('%a', "(#{Date::ABBR_DAYNAMES.join('|')})"). # short day name
|
58
|
-
gsub('%Y', "(\\d{4})"). # long year
|
59
|
-
gsub('%y', "(\\d{2})"). # short year
|
60
|
-
gsub('%e', "(\\s?\\d{1,2})"). # short day
|
61
|
-
gsub('%d', "(\\d{2})"). # full day
|
62
|
-
gsub('%H', "(\\d{2})"). # hour (24)
|
63
|
-
gsub('%M', "(\\d{2})"). # minute
|
64
|
-
gsub('%S', "(\\d{2})") # second
|
68
|
+
format.gsub(/(#{REGEXPS.keys.join('|')})/) { |s| REGEXPS[$1] }
|
65
69
|
end
|
66
70
|
end
|
67
71
|
end
|
@@ -54,8 +54,10 @@ ActiveRecord::Base.class_eval do
|
|
54
54
|
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
55
55
|
# be typecast back to 0 (''.to_i => 0)
|
56
56
|
value = nil
|
57
|
-
|
57
|
+
elsif column.number?
|
58
58
|
value = column.type_cast(convert_number_column_value_with_localization(value))
|
59
|
+
else
|
60
|
+
value = column.type_cast(value)
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
data/test/delocalize_test.rb
CHANGED
@@ -50,11 +50,21 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
|
|
50
50
|
assert_equal time, @product.published_at
|
51
51
|
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
53
|
+
# TODO can I somehow do this smarter? or should I use another zone w/o DST?
|
54
|
+
if Time.current.dst?
|
55
|
+
test "delocalizes localized time (DST)" do
|
56
|
+
now = Time.current
|
57
|
+
time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
|
58
|
+
@product.cant_think_of_a_sensible_time_field = '09:00 Uhr'
|
59
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
60
|
+
end
|
61
|
+
else
|
62
|
+
test "delocalizes localized time (non-DST)" do
|
63
|
+
now = Time.current
|
64
|
+
time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
|
65
|
+
@product.cant_think_of_a_sensible_time_field = '08:00 Uhr'
|
66
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
67
|
+
end
|
58
68
|
end
|
59
69
|
|
60
70
|
test "invalid dates should be delocalized to nil" do
|
@@ -64,20 +74,39 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
|
|
64
74
|
assert_equal date, @product.released_on_before_type_cast
|
65
75
|
end
|
66
76
|
|
67
|
-
|
68
|
-
|
77
|
+
# TODO can I somehow do this smarter? or should I use another zone w/o DST?
|
78
|
+
if Time.current.dst?
|
79
|
+
test "uses default parse if format isn't found (DST)" do
|
80
|
+
date = Date.civil(2009, 10, 19)
|
69
81
|
|
70
|
-
|
71
|
-
|
82
|
+
@product.released_on = '2009/10/19'
|
83
|
+
assert_equal date, @product.released_on
|
72
84
|
|
73
|
-
|
74
|
-
|
75
|
-
|
85
|
+
time = Time.gm(2009, 3, 1, 11, 0, 0).in_time_zone
|
86
|
+
@product.published_at = '2009/03/01 12:00'
|
87
|
+
assert_equal time, @product.published_at
|
88
|
+
|
89
|
+
now = Time.current
|
90
|
+
time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
|
91
|
+
@product.cant_think_of_a_sensible_time_field = '09:00'
|
92
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
93
|
+
end
|
94
|
+
else
|
95
|
+
test "uses default parse if format isn't found (non-DST)" do
|
96
|
+
date = Date.civil(2009, 10, 19)
|
97
|
+
|
98
|
+
@product.released_on = '2009/10/19'
|
99
|
+
assert_equal date, @product.released_on
|
76
100
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
101
|
+
time = Time.gm(2009, 3, 1, 11, 0, 0).in_time_zone
|
102
|
+
@product.published_at = '2009/03/01 12:00'
|
103
|
+
assert_equal time, @product.published_at
|
104
|
+
|
105
|
+
now = Time.current
|
106
|
+
time = Time.gm(now.year, now.month, now.day, 7, 0, 0).in_time_zone
|
107
|
+
@product.cant_think_of_a_sensible_time_field = '08:00'
|
108
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
109
|
+
end
|
81
110
|
end
|
82
111
|
|
83
112
|
test "should return nil if the input is empty or invalid" do
|
@@ -141,6 +170,13 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
|
|
141
170
|
assert @product.weight_changed?
|
142
171
|
end
|
143
172
|
|
173
|
+
test "attributes that didn't change shouldn't be marked dirty" do
|
174
|
+
@product.name = "Good cookies, Really good"
|
175
|
+
@product.save
|
176
|
+
@product.name = "Good cookies, Really good"
|
177
|
+
assert !@product.name_changed?
|
178
|
+
end
|
179
|
+
|
144
180
|
test "should remember the value before type cast" do
|
145
181
|
@product.price = "asd"
|
146
182
|
assert_equal @product.price, 0
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delocalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
9
|
+
- 5
|
10
|
+
version: 0.2.5
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Clemens Kofler
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-03-22 00:00:00 +01:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -48,33 +49,52 @@ files:
|
|
48
49
|
- tasks/documentation.rb
|
49
50
|
- tasks/testing.rb
|
50
51
|
- README.markdown
|
52
|
+
- test/delocalize_test.rb
|
53
|
+
- test/rails2_app/app/controllers/application_controller.rb
|
54
|
+
- test/rails2_app/config/boot.rb
|
55
|
+
- test/rails2_app/config/environment.rb
|
56
|
+
- test/rails2_app/config/environments/test.rb
|
57
|
+
- test/rails2_app/config/initializers/new_rails_defaults.rb
|
58
|
+
- test/rails2_app/config/initializers/session_store.rb
|
59
|
+
- test/rails2_app/config/routes.rb
|
60
|
+
- test/rails3_app/app/controllers/application_controller.rb
|
61
|
+
- test/rails3_app/app/helpers/application_helper.rb
|
62
|
+
- test/rails3_app/config/application.rb
|
63
|
+
- test/rails3_app/config/boot.rb
|
64
|
+
- test/rails3_app/config/environment.rb
|
65
|
+
- test/rails3_app/config/environments/test.rb
|
66
|
+
- test/test_helper.rb
|
51
67
|
has_rdoc: true
|
52
68
|
homepage: http://github.com/clemens/delocalize
|
53
69
|
licenses: []
|
54
70
|
|
55
71
|
post_install_message:
|
56
|
-
rdoc_options:
|
57
|
-
|
72
|
+
rdoc_options: []
|
73
|
+
|
58
74
|
require_paths:
|
59
75
|
- lib
|
60
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
61
78
|
requirements:
|
62
79
|
- - ">="
|
63
80
|
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
64
82
|
segments:
|
65
83
|
- 0
|
66
84
|
version: "0"
|
67
85
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
68
87
|
requirements:
|
69
88
|
- - ">="
|
70
89
|
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
71
91
|
segments:
|
72
92
|
- 0
|
73
93
|
version: "0"
|
74
94
|
requirements: []
|
75
95
|
|
76
96
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.3.
|
97
|
+
rubygems_version: 1.3.7
|
78
98
|
signing_key:
|
79
99
|
specification_version: 3
|
80
100
|
summary: Localized date/time and number parsing
|