kronic 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/HISTORY +4 -0
  2. data/lib/kronic.rb +11 -11
  3. data/spec/kronic_spec.rb +7 -5
  4. metadata +6 -32
data/HISTORY CHANGED
@@ -1,3 +1,7 @@
1
+ 0.2.1
2
+ * Removed dependency on active_support, no external dependencies!
3
+ * Dates in the future with a year specified are parsed correctly
4
+
1
5
  0.2.0
2
6
  * Support "tomorrow"
3
7
  * Support "this monday"
data/lib/kronic.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext'
1
+ require 'date'
2
2
 
3
3
  class Kronic
4
4
  # Public: Converts a human readable day (Today, yesterday) to a Date.
@@ -44,20 +44,20 @@ class Kronic
44
44
 
45
45
  # Examples
46
46
  #
47
- # month_from_name("January") # => 1
48
- # month_from_name("Jan") # => 1
47
+ # month_from_name("january") # => 1
48
+ # month_from_name("jan") # => 1
49
49
  def month_from_name(month)
50
- return nil unless month
50
+ f = lambda {|months| months.compact.map {|x| x.downcase }.index(month) }
51
51
 
52
- human_month = month.downcase.humanize
53
- Date::MONTHNAMES.index(human_month) || Date::ABBR_MONTHNAMES.index(human_month)
52
+ month = f[Date::MONTHNAMES] || f[Date::ABBR_MONTHNAMES]
53
+ month ? month + 1 : nil
54
54
  end
55
55
 
56
56
  # Parse "Today", "Tomorrow" and "Yesterday"
57
57
  def parse_nearby_days(string, today)
58
- return today if string == 'today'
59
- return today - 1.day if string == 'yesterday'
60
- return today + 1.day if string == 'tomorrow'
58
+ return today if string == 'today'
59
+ return today - 1 if string == 'yesterday'
60
+ return today + 1 if string == 'tomorrow'
61
61
  end
62
62
 
63
63
  # Parse "Last Monday", "This Monday"
@@ -66,7 +66,7 @@ class Kronic
66
66
 
67
67
  if %w(last this).include?(tokens[0])
68
68
  days = (1..7).map {|x|
69
- today + (tokens[0] == 'last' ? -x.days : x.days)
69
+ today + (tokens[0] == 'last' ? -x : x)
70
70
  }.inject({}) {|a, x|
71
71
  a.update(x.strftime("%A").downcase => x)
72
72
  }
@@ -91,7 +91,7 @@ class Kronic
91
91
  return nil unless day && month && year
92
92
 
93
93
  result = Date.new(year, month, day)
94
- result -= 1.year if result > today
94
+ result = result << 12 if result > today && !tokens[2]
95
95
  result
96
96
  end
97
97
  end
data/spec/kronic_spec.rb CHANGED
@@ -19,7 +19,8 @@ describe Kronic do
19
19
  :last_monday => Date.new(2010, 9, 13),
20
20
  :next_monday => Date.new(2010, 9, 20),
21
21
  :sep_4 => Date.new(2010, 9, 4),
22
- :sep_20 => Date.new(2009, 9, 20)
22
+ :sep_20 => Date.new(2009, 9, 20),
23
+ :sep_28 => Date.new(2010, 9, 28)
23
24
  }.fetch(key)
24
25
  end
25
26
 
@@ -31,14 +32,15 @@ describe Kronic do
31
32
  should_parse(:today, date(:today))
32
33
  should_parse('today', date(:today))
33
34
  should_parse(' Today', date(:today))
34
- should_parse('Yesterday', date(:today) - 1.day)
35
- should_parse('Tomorrow', date(:today) + 1.day)
35
+ should_parse('Yesterday', date(:today) - 1)
36
+ should_parse('Tomorrow', date(:today) + 1)
36
37
  should_parse('Last Monday', date(:last_monday))
37
38
  should_parse('This Monday', date(:next_monday))
38
39
  should_parse('4 Sep', date(:sep_4))
39
40
  should_parse('4 Sep', date(:sep_4))
40
41
  should_parse('4 September', date(:sep_4))
41
42
  should_parse('20 Sep', date(:sep_20))
43
+ should_parse('28 Sep 2010', date(:sep_28))
42
44
  should_parse('14 Sep 2008', Date.new(2008, 9, 14))
43
45
  should_parse('bogus', nil)
44
46
  should_parse('14', nil)
@@ -46,8 +48,8 @@ describe Kronic do
46
48
  should_parse('14 June oen', nil)
47
49
 
48
50
  should_format('Today', date(:today))
49
- should_format('Yesterday', date(:today) - 1.day)
50
- should_format('Tomorrow', date(:today) + 1.day)
51
+ should_format('Yesterday', date(:today) - 1)
52
+ should_format('Tomorrow', date(:today) + 1)
51
53
  should_format('Last Monday', date(:last_monday))
52
54
  should_format('This Monday', date(:next_monday))
53
55
  should_format('14 September 2008', Date.new(2008, 9, 14))
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Xavier Shay
@@ -17,36 +17,10 @@ cert_chain: []
17
17
  date: 2010-09-20 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: activesupport
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
31
- type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: i18n
35
- prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
44
- type: :runtime
45
- version_requirements: *id002
46
20
  - !ruby/object:Gem::Dependency
47
21
  name: rspec
48
22
  prerelease: false
49
- requirement: &id003 !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
50
24
  none: false
51
25
  requirements:
52
26
  - - ">="
@@ -59,11 +33,11 @@ dependencies:
59
33
  - 16
60
34
  version: 2.0.0.beta.16
61
35
  type: :development
62
- version_requirements: *id003
36
+ version_requirements: *id001
63
37
  - !ruby/object:Gem::Dependency
64
38
  name: timecop
65
39
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
40
+ requirement: &id002 !ruby/object:Gem::Requirement
67
41
  none: false
68
42
  requirements:
69
43
  - - ">="
@@ -72,7 +46,7 @@ dependencies:
72
46
  - 0
73
47
  version: "0"
74
48
  type: :development
75
- version_requirements: *id004
49
+ version_requirements: *id002
76
50
  description:
77
51
  email:
78
52
  - hello@xaviershay.com