date_parity 0.1.0 → 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 319cfda09f4da45e78be9a16425f7ddf0f64c424
4
+ data.tar.gz: ba3b74b9b3823fd2ed1c54e2d947f4c3978aeca6
5
+ SHA512:
6
+ metadata.gz: a5273b43f425ab5245c77a353ba6c6a2b454baa02051e1c4bdff4fd2d28c3ec9d9739d7475c2125ed27510c473103d43240594c071e72a2495a18acf8e809460
7
+ data.tar.gz: 22f946528a54d412b00a851099eb649bac2f53e767ab0111b41b4a2679e75b975e80025b642810e6a087432f489cdfb361c73bf622380bc4e4260c346df95cf9
data/Gemfile CHANGED
@@ -3,4 +3,3 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in date_parity.gemspec
4
4
  gemspec
5
5
 
6
- gem "debugger"
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # Date Parity
2
2
 
3
- Date formatting and parsing per request in Rails with parity between date.to\_s.and string.to\_date.
3
+ Date formatting and parsing per request in Rails with parity between date.to\_s.and string.to\_date. Provides a seamless mechanism for allowing custom date format per request, implemented similar to Time.zone.
4
4
 
5
- Rails has some great Date formatting and parsing options available. But, it still requires a lot of helping code to consistently parse and display dates in a particular format . For example, Date.DATE\_FORMATS makes it easy to configure and reuse common formats for your application. But, other then the :default it require an explicit field.to\_s(:format) to be applied. Additionally, nothing happens automatically when parsing those dates back from user edits. This generally require some explicit steps per field in your model or controller.
5
+ Rails has great Date formatting and parsing options available. But, it requires a lot of helping code to consistently parse and display dates in a particular format per request. Rails makes this easy for Time.zone parsing, but Date parsing generally requires additional manual parsing code. For example, Date.DATE\_FORMATS makes it easy to configure and reuse common formats for your application. But, other then the :default it require an explicit field.to\_s(:format) to be applied. More importantly, nothing automatically happens when parsing those dates back from user edits.
6
6
 
7
7
  Rails also provides the ability to localize your Date formats. But, that requires decisions made for all users in a particular locale.
8
8
 
9
- Often, we need to allow users, companies, organizations, etc. the ability to choose preferred date formats. Both of the Rails standard approaches using Date.DATE\_FORMATS or localization fall short at making that easy; especially when parsing user form posted date strings. You end up writing a lot helpers around date fields, and code to normalize posted date values.
9
+ Often, we need to allow users, companies, organizations, etc. the ability to choose preferred date formats. Both of the Rails standard approaches using Date.DATE\_FORMATS or localization fall short at making that easy; especially when parsing user form posted date strings. You end up writing a lot helpers around date fields, and code to normalize posted date values. This generally require explicit steps per field in your model or controller to manually parse; i.e. Date.strptime("1/15/2013", "%m/%d/%Y")
10
10
 
11
11
  This is where date\_parity helps. It is implemented by setting a `Date.format` string, similar to a per user `Time.zone` in Rails. This can be set per request (i.e. per user) in a controller's before\_action or around\_action.
12
12
 
@@ -80,9 +80,9 @@ Then in any view assuming @model\_started\_on is a date field, and Date.format w
80
80
  #=> '12/31/2012'
81
81
  ```
82
82
 
83
- And then you get parity when a form with this field is posted. Rails will attempt to convert the posted string value of "12/31/2012" using Date.\parse, which DateParity overrides and respects your Date.format.
83
+ And then you get parity when a form with this field is posted. Rails will attempt to convert the posted string value of "12/31/2012" using Date.\parse, which DateParity overrides and respects your Date.format.
84
84
 
85
- Without the date\_parity gem, @model.start\_on would have ended up nil because "12/31/2012" is an invalid argument for ruby Date.\parse. However, when date\_parity is included, Date will use the format string set on Date.format to correctly parse "12/31/2012", as December 31, 2012, when Date.format = "%m/%d/%Y".
85
+ Without the date\_parity gem, @model.start\_on would have ended up nil because "12/31/2012" is an invalid argument for ruby Date.parse. However, when date\_parity is included, Date will use the format string set on Date.format to correctly parse "12/31/2012", as December 31, 2012, when Date.format = "%m/%d/%Y".
86
86
 
87
87
  Note: Rails text\_field form helper uses ```@model.attribute_before_type_cast``` by default. When using date\_parity on plain text fields, you will probably want to set your value to explicitly use @model.attribute if it exists. Otherwise, you won't see the formatted date field: For example:
88
88
  ```ruby
data/date_parity.gemspec CHANGED
@@ -6,15 +6,17 @@ Gem::Specification.new do |gem|
6
6
  gem.version = DateParity::VERSION
7
7
 
8
8
  gem.author = 'Scott Stewart'
9
- gem.email = 'scottpstewart@gamil.com'
10
- gem.description = 'Rails Date formatting and parsing with full parity between date.to_s.and string.to_date per request.'
11
- gem.summary = 'Rails Date formatting and parsing with full parity between date.to_s.and string.to_date per request.'
12
- gem.homepage = ""
9
+ gem.email = 'scott@scopestar.com'
10
+ gem.description = 'Rails Date formatting and parsing per request via Date.format.'
11
+ gem.summary = 'Simple Date formatting for Rails applications that provides full parity between date.to_s.and string.to_date per request. Automatically parse dates to and from your db with custom formats set per page request in a similar fashion to Time.zone settings.'
12
+ gem.homepage = "https://github.com/scott-stewart/date_parity"
13
+ gem.license = 'MIT'
13
14
 
14
- gem.add_dependency 'activesupport', '>= 4.0.0'
15
+ gem.add_runtime_dependency 'activesupport', '~> 4.0', '>= 4.0.0'
15
16
 
16
- gem.add_development_dependency 'rake'
17
+ gem.add_development_dependency 'rake'
17
18
  gem.add_development_dependency 'minitest'
19
+
18
20
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
21
  gem.files = `git ls-files`.split($\)
20
22
  end
@@ -7,16 +7,27 @@ class Date
7
7
  Thread.current[:date_format] = format
8
8
  end
9
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 so normal ::Date._parse call can succeed.
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
+
10
19
  def parse_with_date_parity_format(string, comp=false)
11
20
  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.
21
+ # Normalize date by first respecting custom date_parity Date.format,
22
+ # then formatting back to iso/db string so normal ::Date.parse call can succeed.
14
23
  parse_without_date_parity_format(Date.strptime(string, Date.format).to_s(:db), comp)
15
- #rescue
16
- #parse_without_date_parity_format(string, comp)
24
+ rescue
25
+ parse_without_date_parity_format(string, comp)
17
26
  end
18
27
 
28
+ alias_method_chain :_parse, :date_parity_format
19
29
  alias_method_chain :parse, :date_parity_format
30
+
20
31
  end
21
32
 
22
33
  end
@@ -1,3 +1,3 @@
1
1
  module DateParity
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'date_parity/test_helper'
2
- require 'debugger'
3
2
 
4
3
  class DateParityTest < MiniTest::Unit::TestCase
5
4
 
@@ -95,7 +94,7 @@ class DateParityTest < MiniTest::Unit::TestCase
95
94
  end
96
95
  end
97
96
 
98
- def full_date_parity
97
+ def test_full_date_parity
99
98
  Date.format = "%m/%d/%Y"
100
99
  date = Date.parse_without_date_parity_format("2012-12-31")
101
100
  assert date, date.to_s.to_date
@@ -103,4 +102,12 @@ class DateParityTest < MiniTest::Unit::TestCase
103
102
  assert "12/31/2012", "12/31/2012".to_date.to_s
104
103
  end
105
104
 
105
+ def test__parse_also_respects_data_format
106
+ Date.format = "%m/%d/%Y"
107
+ date_parts = Date._parse_without_date_parity_format("1/25/2014")
108
+ assert date_parts[:year], 2014
109
+ assert date_parts[:mon], 1
110
+ assert date_parts[:mday], 25
111
+ end
112
+
106
113
  end
metadata CHANGED
@@ -1,67 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_parity
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Scott Stewart
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-30 00:00:00.000000000 Z
11
+ date: 2014-01-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- version_requirements: !ruby/object:Gem::Requirement
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - '>='
18
21
  - !ruby/object:Gem::Version
19
22
  version: 4.0.0
20
- none: false
21
- name: activesupport
22
23
  type: :runtime
23
24
  prerelease: false
24
- requirement: !ruby/object:Gem::Requirement
25
+ version_requirements: !ruby/object:Gem::Requirement
25
26
  requirements:
26
- - - ! '>='
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - '>='
27
31
  - !ruby/object:Gem::Version
28
32
  version: 4.0.0
29
- none: false
30
33
  - !ruby/object:Gem::Dependency
31
- version_requirements: !ruby/object:Gem::Requirement
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
32
36
  requirements:
33
- - - ! '>='
37
+ - - '>='
34
38
  - !ruby/object:Gem::Version
35
39
  version: '0'
36
- none: false
37
- name: rake
38
40
  type: :development
39
41
  prerelease: false
40
- requirement: !ruby/object:Gem::Requirement
42
+ version_requirements: !ruby/object:Gem::Requirement
41
43
  requirements:
42
- - - ! '>='
44
+ - - '>='
43
45
  - !ruby/object:Gem::Version
44
46
  version: '0'
45
- none: false
46
47
  - !ruby/object:Gem::Dependency
47
- version_requirements: !ruby/object:Gem::Requirement
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
48
50
  requirements:
49
- - - ! '>='
51
+ - - '>='
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
- none: false
53
- name: minitest
54
54
  type: :development
55
55
  prerelease: false
56
- requirement: !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ! '>='
58
+ - - '>='
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
- none: false
62
- description: Rails Date formatting and parsing with full parity between date.to_s.and
63
- string.to_date per request.
64
- email: scottpstewart@gamil.com
61
+ description: Rails Date formatting and parsing per request via Date.format.
62
+ email: scott@scopestar.com
65
63
  executables: []
66
64
  extensions: []
67
65
  extra_rdoc_files: []
@@ -77,31 +75,33 @@ files:
77
75
  - lib/date_parity/version.rb
78
76
  - test/date_parity_test.rb
79
77
  - test/lib/date_parity/test_helper.rb
80
- homepage: ''
81
- licenses: []
78
+ homepage: https://github.com/scott-stewart/date_parity
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
82
  post_install_message:
83
83
  rdoc_options: []
84
84
  require_paths:
85
85
  - lib
86
86
  required_ruby_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ! '>='
88
+ - - '>='
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
- none: false
92
91
  required_rubygems_version: !ruby/object:Gem::Requirement
93
92
  requirements:
94
- - - ! '>='
93
+ - - '>='
95
94
  - !ruby/object:Gem::Version
96
95
  version: '0'
97
- none: false
98
96
  requirements: []
99
97
  rubyforge_project:
100
- rubygems_version: 1.8.23
98
+ rubygems_version: 2.2.0
101
99
  signing_key:
102
- specification_version: 3
103
- summary: Rails Date formatting and parsing with full parity between date.to_s.and
104
- string.to_date per request.
100
+ specification_version: 4
101
+ summary: Simple Date formatting for Rails applications that provides full parity between
102
+ date.to_s.and string.to_date per request. Automatically parse dates to and from
103
+ your db with custom formats set per page request in a similar fashion to Time.zone
104
+ settings.
105
105
  test_files:
106
106
  - test/date_parity_test.rb
107
107
  - test/lib/date_parity/test_helper.rb