date_parity 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # DateParity
2
2
 
3
- Consistent date formatting per request; creating parity between date.to\_s and
4
- string.to\_date.
3
+ Consistent date formatting and parsing for Rails applications. Creates parity between date.to\_s and
4
+ string.to\_date in Rails applications.
5
5
 
6
- Provides a Date.format syntax and usage, similiar to Time.zone in rails. Can be
7
- set per request (i.e. per user).
6
+ This is implemented with a "Date.format" syntax and usage, similar to "Time.zone" in rails. This can be
7
+ set per request (i.e. per user) in a controller before or around filter.
8
8
 
9
9
  ## Installation
10
10
 
@@ -24,22 +24,39 @@ Or install it yourself as:
24
24
 
25
25
  In Rails controller before\_filter:
26
26
  Date.format = '%m/%d/%Y'
27
- or
27
+
28
+ or from a persisted field that stores the desired format:
28
29
  Date.format = current\_user.date\_format
29
30
 
30
- In any view assuming @resourc\_created\_on is a date::
31
- @resource.start\_on
32
- will render: 12/31/2012
31
+ In any view assuming @model\_created\_on is a date, and Date.format
32
+ was set as above:
33
+ <%=@modele.created\_on %>
34
+ => 12/31/2012
33
35
 
34
36
  And then you get parity when a form with this field is posted.
35
37
  Rails will attempt to convert the posted string value of "12/31/2012"
36
- using "12/31/2012".to\_date in the respective model before validation.
37
- Under normal circumstances, @resource.start\_on will end up being nil
38
- because "12/31/2012".to\_date is invalid for Ruby Date.\_parse.
39
-
40
- When date\_parity is required, Date will use the format string stored on Date.formt
41
- to correctly parse "12/31/2012". This is accomplished by overriding String.to\_date
42
- to use : Date.strptime(self, Date.format).to\_s(:db)
38
+ using Date.\_parse, which DateParity overrides and respects your Date.format.
39
+ Without DateParity gem, @model.start\_on would have ended up nil because
40
+ "12/31/2012" is invalid argument Ruby Date.\_parse. However, when date\_parity is
41
+ included, Date will use the format string stored on Date.format
42
+ (if it exist) to correctly parse "12/31/2012", as December 31, 2012, when
43
+ Date.format = "%m/%d/%Y"
44
+
45
+ Note: Rails text\_field form helper uses @model.attribute\before\_type\_cast by default.
46
+ When using date\_parity on plain text fields, you will probably want to set your value
47
+ to explicitly use @model.attribute if it exists. Otherwise, you won't see the formatted
48
+ date field:
49
+ For example:
50
+ <%= f.text_field :created_on, :value => @user.created\_on || @user.created\_on\_before\_type\_cast %>
51
+
52
+ Date.format can be set to any date format that is parsable by strptime.
53
+ For example, the following are all valid:<br>
54
+ Date.format = "%m/%d/%Y"<br>
55
+ Date.format = "%d/%m/%Y"<br>
56
+ Date.format = '%Y-%m-%d'<br>
57
+ Date.format = '%d.%m.%Y'<br>
58
+ Date.format = '%Y.%m.%d'<br>
59
+ Date.format = '%Y/%m/%d'<br>
43
60
 
44
61
  ## Contributing
45
62
 
@@ -6,7 +6,19 @@ class Date
6
6
  def format=(format)
7
7
  Thread.current[:date_format] = format
8
8
  end
9
+
10
+ def _parse_with_date_parity_format(string, comp=false)
11
+ return _parse_without_date_parity_format(string, comp) if string.blank? or Date.format.blank?
12
+ # normalize date by first respecting custom date_parity Date.format,
13
+ # then formatting back to iso/db string for normal _parse call.
14
+ _parse_without_date_parity_format(Date.strptime(string, Date.format).to_s(:db), comp)
15
+ rescue
16
+ _parse_without_date_parity_format(string, comp)
17
+ end
18
+
19
+ alias_method_chain :_parse, :date_parity_format
9
20
  end
21
+
10
22
  end
11
23
 
12
24
 
@@ -1,3 +1,3 @@
1
1
  module DateParity
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/date_parity.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  require 'date'
2
2
  require 'active_support/core_ext/date/conversions'
3
3
  require 'active_support/core_ext/string/conversions'
4
-
5
4
  require "date_parity/version"
6
- require 'date_parity/core_ext/string'
7
5
  require 'date_parity/core_ext/date'
8
6
 
9
7
  # ActiveSupport provides an implementation of Date.to_s that uses
@@ -25,7 +25,31 @@ class DateParityTest < MiniTest::Unit::TestCase
25
25
 
26
26
  end
27
27
 
28
+ def test_date_parse
29
+ date_hash = { :year => 2012, :mon => 8, :mday => 12 }
30
+
31
+ Date.format = "%m/%d/%Y"
32
+ assert_equal date_hash, Date._parse("08/12/2012")
33
+
34
+ Date.format = "%d/%m/%Y"
35
+ assert_equal date_hash, Date._parse("12/08/2012")
36
+
37
+ Date.format = '%Y-%m-%d'
38
+ assert_equal date_hash, Date._parse("2012-08-12")
39
+
40
+ Date.format = '%d.%m.%Y'
41
+ assert_equal date_hash, Date._parse("12.08.2012")
42
+
43
+ Date.format = '%Y.%m.%d'
44
+ assert_equal date_hash, Date._parse("2012.08.12")
45
+
46
+ Date.format = '%Y/%m/%d'
47
+ assert_equal date_hash, Date._parse("2012/08/12")
48
+ end
49
+
28
50
  def test_string_to_date_respects_format
51
+ # This works because Rails ActiveSupport provides
52
+ # string.to_date, which uses Date._parse
29
53
  Date.format = "%m/%d/%Y"
30
54
  assert_equal Date.parse("2012-08-12"), "08/12/2012".to_date
31
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_parity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70139748526520 !ruby/object:Gem::Requirement
16
+ requirement: &70261701192120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70139748526520
24
+ version_requirements: *70261701192120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70139748526100 !ruby/object:Gem::Requirement
27
+ requirement: &70261701191700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70139748526100
35
+ version_requirements: *70261701191700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70139748525640 !ruby/object:Gem::Requirement
38
+ requirement: &70261701191240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70139748525640
46
+ version_requirements: *70261701191240
47
47
  description: Consistent date formatting done simple
48
48
  email: scottpstewart@gamil.com
49
49
  executables: []
@@ -58,7 +58,6 @@ files:
58
58
  - date_parity.gemspec
59
59
  - lib/date_parity.rb
60
60
  - lib/date_parity/core_ext/date.rb
61
- - lib/date_parity/core_ext/string.rb
62
61
  - lib/date_parity/version.rb
63
62
  - test/date_parity_test.rb
64
63
  - test/lib/date_parity/test_helper.rb
@@ -76,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
75
  version: '0'
77
76
  segments:
78
77
  - 0
79
- hash: -3237689646668649695
78
+ hash: 2984063008747520348
80
79
  required_rubygems_version: !ruby/object:Gem::Requirement
81
80
  none: false
82
81
  requirements:
@@ -85,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
84
  version: '0'
86
85
  segments:
87
86
  - 0
88
- hash: -3237689646668649695
87
+ hash: 2984063008747520348
89
88
  requirements: []
90
89
  rubyforge_project:
91
90
  rubygems_version: 1.8.11
@@ -1,13 +0,0 @@
1
- class String
2
-
3
- def to_date_from_formatted_string
4
- return nil if self.blank?
5
- Date.strptime(self, Date.format)
6
- rescue
7
- to_rails_date
8
- end
9
-
10
- alias_method :to_rails_date, :to_date
11
- alias_method :to_date, :to_date_from_formatted_string
12
-
13
- end