Permalink
Browse files

Introduce due attribute on Todo

  • Loading branch information...
Bèr Kessels
Bèr Kessels committed Mar 14, 2013
1 parent 66beb54 commit a2f982e7555a467ede6ac9584bbf4a18540d7dbd
Showing with 19 additions and 0 deletions.
  1. +1 −0 lib/todotxt/regex.rb
  2. +5 −0 lib/todotxt/todo.rb
  3. +1 −0 spec/spec_helper.rb
  4. +11 −0 spec/todo_spec.rb
  5. +1 −0 todotxt.gemspec
View
@@ -2,5 +2,6 @@ module Todotxt
PRIORITY_REGEX = /^\(([A-Z])\) /
PROJECT_REGEX = /(\+\w+)/
CONTEXT_REGEX = /(@\w+)/
+ DATE_REGEX = /^(\([A-Z]\) )?(x )?((\d{4}-)(\d{1,2}-)(\d{1,2}))\s?/
DONE_REGEX = /^(\([A-Z]\) )?x /
end
View
@@ -24,6 +24,11 @@ def create_from_text text
@done = !text.scan(DONE_REGEX).empty?
end
+ def due
+ date = Chronic.parse(text.scan(DATE_REGEX).flatten[2])
+ date.nil? ? nil : date.to_date
+ end
+
def do
unless done
@text = "x #{text}".strip
View
@@ -1,2 +1,3 @@
+require 'chronic'
require 'simplecov'
SimpleCov.start
View
@@ -28,6 +28,17 @@
todo.done.should eql true
end
+ it "parses a due date" do
+ todo = Todotxt::Todo.new "(A) x 2012-12-12 an item +project1 +project2 @context1 @context2"
+ todo.due.should eql Chronic.parse("12 December 2012").to_date
+
+ todo = Todotxt::Todo.new "2012-1-2 an item +project1 +project2 @context1 @context2"
+ todo.due.should eql Chronic.parse("2 January 2012").to_date
+
+ todo = Todotxt::Todo.new "42 folders"
+ todo.due.should be_nil
+ end
+
it "stores line number when creating an item" do
todo = Todotxt::Todo.new "an item", "2"
View
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_dependency "thor", "~> 0.15.4"
s.add_dependency "rainbow", "~> 1.1.4"
s.add_dependency "parseconfig", "~> 1.0.2"
+ s.add_dependency "chronic", "~> 0.9.1"
s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 2.11.0"

0 comments on commit a2f982e

Please sign in to comment.