flexible_date 0.1.0

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.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +55 -0
  3. data/lib/flexible_date.rb +38 -0
  4. metadata +90 -0
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Rodrigo Manhães
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,55 @@
1
+ = flexible_date
2
+
3
+ Make ActiveRecord understand any date format you want.
4
+
5
+ == How to use
6
+
7
+ Simply do, within your model:
8
+
9
+ flexible_date :my_date_field, :format => "%d/%m/%Y"
10
+
11
+ The format follows the same pattern used by strptime and strftime. This will
12
+ generate a my_date_field_flex "property" in which you can enter the date as
13
+ string in the specified format and then the correct date will be stored in
14
+ my_date_field.
15
+
16
+ Talk is cheap, let's see the code:
17
+
18
+ class Event < ActiveRecord::Base
19
+ flexible_date :start_date, :end_date, :format => "%d/%m/%Y"
20
+ end
21
+
22
+ event = Event.new
23
+ event.start_date_flex = "31/01/2011"
24
+ event.end_date_flex = "28/02/2011"
25
+ event.start_date.should == Date.new(2011, 01, 31)
26
+ event.end_date.should == Date.new(2011, 02, 28)
27
+
28
+
29
+ And values assigned to regular fields are returned by _flex fields in the
30
+ specified format:
31
+
32
+ event = Event.new :start_date => '2011-01-31',
33
+ :end_date => Date.new(2011, 02, 28)
34
+ event.start_date_flex.should == "31/01/2011"
35
+ event.end_date_flex.should == "28/02/2011"
36
+
37
+
38
+ == How to install
39
+
40
+ flexible_date will become a gem very soon.
41
+
42
+
43
+ == Do you know a better/standard solution?
44
+
45
+ My intention is to allow users typing dates directly to text fields in their
46
+ own local format. I didn't find a solution for this problem in Rails itself.
47
+ If you have a better solution, please let me know.
48
+
49
+ No, default select fields, separated by day, month and year are not a solution.
50
+
51
+
52
+ == Copyright
53
+
54
+ Copyright (c) 2011 Rodrigo Manhães. See LICENSE.txt for further details.
55
+
@@ -0,0 +1,38 @@
1
+ module FlexibleDate
2
+ def flexible_date(*params)
3
+ options, fields = params.pop, params
4
+ format = options[:format]
5
+ fields.each do |field|
6
+ unless methods.include?(:flexible_date_validations)
7
+ validate :flexible_date_validations
8
+
9
+ define_method :flexible_date_validations do
10
+ if @flexible_date_errors
11
+ @flexible_date_errors.each do |field, message|
12
+ errors.add(field, message)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ define_method "#{field}_flex" do
19
+ date = self.send("#{field}")
20
+ date.try(:strftime, format)
21
+ end
22
+
23
+ define_method "#{field}_flex=" do |value|
24
+ begin
25
+ self.send("#{field}=", Date.strptime(value, format))
26
+ rescue ArgumentError
27
+ self.send("#{field}=", nil)
28
+ @flexible_date_errors ||= {}
29
+ @flexible_date_errors["#{field}".to_sym] = 'invalid'
30
+ @flexible_date_errors["#{field}_flex".to_sym] = 'invalid'
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ ActiveRecord::Base.extend(FlexibleDate)
38
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flexible_date
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Rodrigo Manh\xC3\xA3es"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-29 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3-ruby
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.3.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.5.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: Make ActiveRecord understand any date format you want.
50
+ email: rmanhaes@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/flexible_date.rb
59
+ - README.rdoc
60
+ - LICENSE.txt
61
+ has_rdoc: true
62
+ homepage: https://github.com/rodrigomanhaes/flexible_date
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.5.0
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Make possible enter date fields in any format into your ActiveRecord models
89
+ test_files: []
90
+