flex_date 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +18 -45
- data/Rakefile +1 -1
- data/lib/; +25 -0
- data/lib/flex_date.rb +25 -0
- data/lib/flex_date/mappings.rb +20 -0
- data/test/flex_date_test.rb +8 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b01b6775edbff002323f99584d2216f6815d9df
|
4
|
+
data.tar.gz: fa01a22760926075bf081001c28487a968f1c794
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48af31940a2e1fa2227722927e21ee3af154d1faa0fe5f9dc575bff0a6934d195c02f497b351cce9768e8745ace03df007f1a6756632b83ad3119c3b31ed1b9f
|
7
|
+
data.tar.gz: 0384c9dcadebb7edc5dc5474d674e451ba11c9df7969f8796e9408de9ce990de78b713d1c2adc3f0f36afdcbb1e6d38c51b33923fb071b31f2a1265b52595f5d
|
data/README.rdoc
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
= FlexDate
|
2
2
|
|
3
|
-
Flexible dates for Ruby.
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
Flexible dates for Ruby, as a gem.
|
4
|
+
Originally written by Alex Reisner, packaged as a gem.
|
5
|
+
https://github.com/alexreisner/flex_date
|
7
6
|
|
8
7
|
== The Problem
|
9
8
|
|
@@ -29,52 +28,28 @@ Where a full date (year, month, and day) is specified, a FlexDate behaves exactl
|
|
29
28
|
|
30
29
|
Comparison operators also (== and <=>) work and I hope to have date arithmetic operators (+ and -) up and running soon.
|
31
30
|
|
31
|
+
== Date parsing
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
To install FlexDate, use the standard Rails plugin installation script:
|
33
|
+
FlexDate also comes with the method `parse` where it will currently create a FlexDate object from a date string that is formatted in the following way:
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
== Integration with Rails/ActiveRecord
|
35
|
+
month day year or month/day/year or month-day-year
|
36
|
+
month year or month/year or month-year
|
37
|
+
year
|
41
38
|
|
42
|
-
The
|
39
|
+
The month can be January, Feb, or 3
|
40
|
+
The day must be an integer representing a day of the month
|
41
|
+
The year must be a 4 digit number representing a year
|
43
42
|
|
44
|
-
|
45
|
-
birthday_m
|
46
|
-
birthday_d
|
47
|
-
|
48
|
-
Then, simply put this in your Person model (you can list multiple attributes if needed):
|
49
|
-
|
50
|
-
multipart_date :birthday
|
51
|
-
|
52
|
-
and you'll be able to do things like this:
|
43
|
+
== Installation
|
53
44
|
|
54
|
-
|
55
|
-
p = Person.new(:birthday_m => 8, :birthday_d => 23)
|
56
|
-
|
57
|
-
# See if their birthday is set.
|
58
|
-
p.birthday?
|
59
|
-
=> true
|
45
|
+
To install FlexDate using Bundler, add it to your Gemfile.
|
60
46
|
|
61
|
-
|
62
|
-
b = p.birthday
|
63
|
-
=> #<FlexDate:0xb78b542c ...>
|
64
|
-
|
65
|
-
# Display their birthday nicely.
|
66
|
-
p.birthday.to_s(:long)
|
67
|
-
=> "August 23"
|
68
|
-
|
69
|
-
# Add the year when you find out.
|
70
|
-
p.birthday_y = 1964
|
71
|
-
p.birthday.to_s(:long)
|
72
|
-
=> "August 23, 1964"
|
73
|
-
|
47
|
+
gem 'flex_date', '0.1.0'
|
74
48
|
|
75
|
-
I've also written another Rails plugin called Informant which provides a full-featured FormBuilder class, and includes a <tt>multipart_date_select</tt> field type which is a good compliment to MultipartDate in your Rails application. Install Informant from my Git repository:
|
76
49
|
|
77
|
-
|
50
|
+
== Integration with Rails/ActiveRecord
|
51
|
+
|
52
|
+
This will be packaged in a different gem called 'flex_date_rails'
|
78
53
|
|
79
54
|
|
80
55
|
== Code Discussion
|
@@ -88,8 +63,6 @@ Any comments or suggestions regarding this design are welcome.
|
|
88
63
|
|
89
64
|
== To-do List
|
90
65
|
|
91
|
-
* <tt>MultipartDate::multipart_date</tt> should also generate a setter method.
|
92
66
|
* Make date arithmetic work (Date minus FlexDate works, but neither FlexDate minus Date or FlexDate minus FlexDate works).
|
93
67
|
|
94
|
-
|
95
|
-
Copyright (c) 2008 Alex Reisner (alex@alexreisner.com), released under the MIT license.
|
68
|
+
released under the MIT license.
|
data/Rakefile
CHANGED
data/lib/;
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mappings
|
2
|
+
def mapping
|
3
|
+
{:month =>
|
4
|
+
{1 => [:january, :jan, 1],
|
5
|
+
2 => [:february, :feb, 2],
|
6
|
+
3 => [:march, :mar, 3],
|
7
|
+
4 => [:april, :apr, 4],
|
8
|
+
5 => [:may, 5],
|
9
|
+
6 => [:june, :jun, 6],
|
10
|
+
7 => [:july, :jul, 7],
|
11
|
+
8 => [:august, :aug, 8],
|
12
|
+
9 => [:september, :sep, 9],
|
13
|
+
10 => [:october, :oct, 10],
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
"october 21 1985"
|
18
|
+
"october 1985"
|
19
|
+
"1985"
|
20
|
+
"oct/21/1985"
|
21
|
+
"10/21/1985"
|
22
|
+
"10-1985"
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/flex_date.rb
CHANGED
@@ -1,6 +1,31 @@
|
|
1
|
+
# Original code written by Alex Reisner
|
2
|
+
# http://www.alexreisner.com/code/ruby/flex_date
|
3
|
+
# Copyright (c) 2008 Alex Reisner (alex@alexreisner.com), released under the MIT license.
|
4
|
+
|
5
|
+
require_relative 'flex_date/mappings.rb'
|
1
6
|
require 'date'
|
2
7
|
class FlexDate
|
8
|
+
extend Mappings
|
3
9
|
include Comparable
|
10
|
+
|
11
|
+
##
|
12
|
+
# parse friendly dates
|
13
|
+
# expects a format of: month day year, month year, or year
|
14
|
+
# can be delimited by spaces, "/" or "-"
|
15
|
+
##
|
16
|
+
def self.parse(str)
|
17
|
+
one, two, three = str.split(/[\s\-\/]/)
|
18
|
+
if(!one.nil? and !two.nil? and !three.nil?)
|
19
|
+
m = parse_month(one)
|
20
|
+
return self.new(three.to_i, m, two.to_i)
|
21
|
+
elsif(!one.nil? and !two.nil?)
|
22
|
+
m = parse_month(one)
|
23
|
+
return self.new(two.to_i, m, nil)
|
24
|
+
elsif(!one.nil?)
|
25
|
+
return self.new(one.to_i)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
4
29
|
|
5
30
|
def <=>(other)
|
6
31
|
spaceship = 0 # start assuming equal
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Mappings
|
2
|
+
MONTHS =
|
3
|
+
{1 => ['january', 'jan', '1'],
|
4
|
+
2 => ['february', 'feb', '2'],
|
5
|
+
3 => ['march', 'mar', '3'],
|
6
|
+
4 => ['april', 'apr', '4'],
|
7
|
+
5 => ['may', '5'],
|
8
|
+
6 => ['june', 'jun', '6'],
|
9
|
+
7 => ['july', 'jul', '7'],
|
10
|
+
8 => ['august', 'aug', '8'],
|
11
|
+
9 => ['september', 'sep', '9'],
|
12
|
+
10 => ['october', 'oct', '10'],
|
13
|
+
11 => ['november', 'nov', '11'],
|
14
|
+
12 => ['december', 'dec', '12']}
|
15
|
+
|
16
|
+
def parse_month(month)
|
17
|
+
MONTHS.select{|k,v| v.include? month.downcase }.keys.first
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/test/flex_date_test.rb
CHANGED
@@ -59,4 +59,12 @@ class FlexDateTest < Test::Unit::TestCase
|
|
59
59
|
e = FlexDate.new(1945,6)
|
60
60
|
assert_equal [e,d,c,b,a], [b,e,a,d,c].sort
|
61
61
|
end
|
62
|
+
|
63
|
+
def test_friendly_date_creation
|
64
|
+
assert_equal FlexDate.parse("October 21 1985"), FlexDate.new(1985,10,21)
|
65
|
+
assert_equal FlexDate.parse("Nov 2016"), FlexDate.new(2016,11)
|
66
|
+
assert_equal FlexDate.parse("2012"), FlexDate.new(2012)
|
67
|
+
assert_equal FlexDate.parse("10-21-1985"), FlexDate.new(1985,10,21)
|
68
|
+
assert_equal FlexDate.parse("10/21/1985"), FlexDate.new(1985,10,21)
|
69
|
+
end
|
62
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flex_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Gregory
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: seangregory@gmail.com
|
@@ -20,7 +20,9 @@ files:
|
|
20
20
|
- MIT-LICENSE
|
21
21
|
- README.rdoc
|
22
22
|
- Rakefile
|
23
|
+
- lib/;
|
23
24
|
- lib/flex_date.rb
|
25
|
+
- lib/flex_date/mappings.rb
|
24
26
|
- test/flex_date_test.rb
|
25
27
|
homepage: https://github.com/skinnyjames/flex_date
|
26
28
|
licenses: []
|