kronic 0.1 → 0.1.1

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/HISTORY CHANGED
@@ -1,2 +1,5 @@
1
+ 0.1.1 - 19 September 2010
2
+ * Kronic.parse will explode in fewer scenarios!
3
+
1
4
  0.1 - 19 September 2010
2
5
  * Initial release!
data/README.rdoc CHANGED
@@ -14,10 +14,11 @@ Oh yeah, Kronic is less than 60 lines of code.
14
14
 
15
15
  gem install kronic
16
16
 
17
+ require 'kronic'
17
18
  Kronic.parse("Today") # => Date.today
18
19
  Kronic.format(Date.today) # => "Today"
19
20
 
20
- See the specs for all the supported formats (there are not many).
21
+ Supported formats: Today, yesterday, last thursday, 14 Sep, 14 June 2010
21
22
 
22
23
  == Future
23
24
 
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ desc 'Default: run specs.'
2
+ task :default => :spec
3
+
4
+ # RSpec provided helper doesn't like me, for now just run it myself
5
+ desc "Run specs"
6
+ task :spec do
7
+ exec("rspec spec/*_spec.rb")
8
+ end
data/lib/kronic.rb CHANGED
@@ -3,8 +3,25 @@ require 'active_support/duration'
3
3
  require 'active_support/time_with_zone'
4
4
 
5
5
  class Kronic
6
+ # Converts a human readable day (Today, yesterday) to a date in the past.
7
+ # Supported inputs include Today, yesterday, last thursday, 14 Sep, 14
8
+ # June 2010, all case-insensitive.
9
+ #
10
+ # Will call #to_s on the input, so can process Symbols or whatever other
11
+ # object you wish to throw at it.
6
12
  def self.parse(string)
7
- string = string.downcase
13
+ # TODO: You could totally clean this method up, if you wanted
14
+ def self.month_from_name(month)
15
+ months = (1..12).map {|x|
16
+ Date.new(2010, x, 1)
17
+ }.inject({}) {|a, x|
18
+ a.update(x.strftime("%B").downcase => x.month)
19
+ }
20
+
21
+ months[month] || months.detect {|name, number| name.starts_with?(month) }.try(:last)
22
+ end
23
+
24
+ string = string.to_s.downcase.strip
8
25
  today = Date.today
9
26
 
10
27
  return Date.today if string == 'today'
@@ -12,6 +29,7 @@ class Kronic
12
29
 
13
30
  tokens = string.split(/\s+/)
14
31
 
32
+ # Last X
15
33
  if tokens[0] == 'last'
16
34
  days = (1..7).map {|x|
17
35
  (Date.today - x.days)
@@ -21,13 +39,17 @@ class Kronic
21
39
  return days[tokens[1]]
22
40
  end
23
41
 
42
+ # 14 Sep, 14 September, 14 September 2010
24
43
  if tokens[0] =~ /^[0-9]+$/
25
-
26
44
  day = tokens[0].to_i
27
45
  month = month_from_name(tokens[1])
28
- year = tokens[2] ? tokens[2].to_i : today.year
46
+ year = if tokens[2]
47
+ tokens[2] =~ /^[0-9]+$/ ? tokens[2].to_i : nil
48
+ else
49
+ today.year
50
+ end
29
51
 
30
- return nil unless month
52
+ return nil unless day && month && year
31
53
 
32
54
  result = Date.new(year, month, day)
33
55
  result -= 1.year if result > Date.today
@@ -37,16 +59,7 @@ class Kronic
37
59
  nil
38
60
  end
39
61
 
40
- def self.month_from_name(month)
41
- months = (1..12).map {|x|
42
- Date.new(2010, x, 1)
43
- }.inject({}) {|a, x|
44
- a.update(x.strftime("%B").downcase => x.month)
45
- }
46
-
47
- months[month] || months.detect {|name, number| name.starts_with?(month) }.last
48
- end
49
-
62
+ # Converts a date to a human readable string.
50
63
  def self.format(date, opts = {})
51
64
  case ((opts[:today] || Date.today).to_date - date.to_date).to_i
52
65
  when 0 then "Today"
data/spec/kronic_spec.rb CHANGED
@@ -17,7 +17,8 @@ describe Kronic do
17
17
  {
18
18
  :today => Date.new(2010, 9, 19),
19
19
  :monday => Date.new(2010, 9, 13),
20
- :sep_4 => Date.new(2010, 9, 4)
20
+ :sep_4 => Date.new(2010, 9, 4),
21
+ :sep_20 => Date.new(2009, 9, 20)
21
22
  }.fetch(key)
22
23
  end
23
24
 
@@ -26,13 +27,20 @@ describe Kronic do
26
27
  end
27
28
 
28
29
  should_parse('Today', date(:today))
30
+ should_parse(:today, date(:today))
29
31
  should_parse('today', date(:today))
32
+ should_parse(' Today', date(:today))
30
33
  should_parse('Yesterday', date(:today) - 1.day)
31
34
  should_parse('Last Monday', date(:monday))
32
35
  should_parse('4 Sep', date(:sep_4))
36
+ should_parse('4 Sep', date(:sep_4))
33
37
  should_parse('4 September', date(:sep_4))
38
+ should_parse('20 Sep', date(:sep_20))
34
39
  should_parse('14 Sep 2008', Date.new(2008, 9, 14))
35
40
  should_parse('bogus', nil)
41
+ should_parse('14', nil)
42
+ should_parse('14 bogus in', nil)
43
+ should_parse('14 September oen', nil)
36
44
 
37
45
  should_format('Today', date(:today))
38
46
  should_format('Yesterday', date(:today) - 1.day)
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- version: "0.1"
8
+ - 1
9
+ version: 0.1.1
9
10
  platform: ruby
10
11
  authors:
11
12
  - Xavier Shay
@@ -44,6 +45,7 @@ files:
44
45
  - lib/kronic.rb
45
46
  - README.rdoc
46
47
  - HISTORY
48
+ - Rakefile
47
49
  has_rdoc: true
48
50
  homepage: http://github.com/xaviershay/kronic
49
51
  licenses: []