Amélioration substantielle du code.
This commit is contained in:
parent
136c0aafc2
commit
ff641f692c
2 changed files with 67 additions and 23 deletions
40
task.rb
40
task.rb
|
@ -23,7 +23,7 @@ class Task
|
|||
# tags
|
||||
@tags=tags
|
||||
end
|
||||
def initialize(description)
|
||||
def initialize(raw_input)
|
||||
@notes=[]
|
||||
@contacts=[]
|
||||
@contexts=[]
|
||||
|
@ -31,14 +31,16 @@ class Task
|
|||
@tags=[]
|
||||
@priority=0
|
||||
@dates=TaskTime.new()
|
||||
from_s(raw_input)
|
||||
end
|
||||
def to_s
|
||||
return @description +
|
||||
@contexts.map { |x| x.to_s }.join(" ") +
|
||||
@projects.map { |x| '['+x.to_s+']' }.join(" ") +
|
||||
@contacts.map { |x| x.to_s }.join(" ") +
|
||||
@dates.to_s +
|
||||
'{' + @tags.map{ |x| x.to_s }.join(", ") + '}'
|
||||
res=@description
|
||||
if (@contexts): res+=' ' + @contexts.map { |x| x.to_s }.join(" ") end
|
||||
if (@projects): res+=' ' + @projects.map { |x| '['+x.to_s+']' }.join(" ") end
|
||||
if (@contacts): res+=' ' + @contacts.map { |x| x.to_s }.join(" ") end
|
||||
if (@dates): res+=' ' + @dates.to_s end
|
||||
if (@tags): res+=' ' + '{' + @tags.map{ |x| x.to_s }.join(", ") + '}' end
|
||||
return res
|
||||
end
|
||||
|
||||
# -- constant class variable for each part
|
||||
|
@ -57,20 +59,22 @@ class Task
|
|||
|
||||
def from_s( raw_input )
|
||||
|
||||
@contexts=raw.scan(@@ContextsRegExp).map{ |x| x[0] }
|
||||
@projects=raw.scan(@@ProjectsRegExp).map{ |x| x[0] }
|
||||
@contacts=raw.scan(@@ContactsRegExp).map{ |x| x[1] }
|
||||
@notes =raw.scan( @@NotesRegExp).map{ |x| x[0] }
|
||||
# @contexts=raw.scan(/ @(\w+|"[^"]*")/).map{ |x| x[0] }
|
||||
# @projects=raw.scan(/\[(\w+|"[^"]*")\]/).map{ |x| x[0] }
|
||||
# @contacts=raw.scan(/ (c|contact):(\w+|"[^"]*")/).map{ |x| x[1] }
|
||||
# @notes=raw.scan(/\[(\w+|"[^"]*")\]/).map{ |x| x[1] }
|
||||
@contexts=raw_input.scan(@@ContextsRegExp).map{ |x| x[0] }
|
||||
@projects=raw_input.scan(@@ProjectsRegExp).map{ |x| x[0] }
|
||||
@contacts=raw_input.scan(@@ContactsRegExp).map{ |x| x[1] }
|
||||
@notes =raw_input.scan( @@NotesRegExp).map{ |x| x[0] }
|
||||
|
||||
# somehow special for the priority
|
||||
@priority= raw.scan( /!/ ).length - raw.scan( /\?/ ).length
|
||||
@priority= raw_input.scan( /!/ ).length - raw_input.scan( /\?/ ).length
|
||||
|
||||
@description=raw_input.gsub(
|
||||
Regexp.union(@@ContextsRegExp, @@ProjectsRegExp, @@ContactsRegExp, @@NotesRegExp, /!/, /\?/, @date.dateRegexp),"")
|
||||
@dates =TaskTime.from_s(raw_input)
|
||||
Regexp.union(@@ContextsRegExp, @@ProjectsRegExp,
|
||||
@@ContactsRegExp, @@NotesRegExp,
|
||||
/!/, /\?/, @dates.regexp),"")
|
||||
@dates=TaskTime.new(raw_input)
|
||||
end
|
||||
end
|
||||
|
||||
current = Task.new("Coucou");
|
||||
print current.to_s
|
||||
print "\n"
|
||||
|
|
50
taskTime.rb
50
taskTime.rb
|
@ -1,19 +1,59 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# -- pour la lecture de la date à partir du language naturel
|
||||
require 'rubygems'
|
||||
require 'chronic'
|
||||
|
||||
class TaskTime
|
||||
@creation_date # creation date of the task
|
||||
@done_date # date at which the task was done
|
||||
@start_date # date at with the task began (hour,minute,second)
|
||||
@done_date # date at which the task was done
|
||||
@due_date # due date for the task
|
||||
@duration # time spend for finish that task
|
||||
# can be set by the user to be
|
||||
# different than (@done_date - @start_date)
|
||||
@max_duration # maximal duration for that task
|
||||
@min_duration # minimal duration for that task
|
||||
def initialize()
|
||||
@creation_date=Time.now
|
||||
|
||||
# -- Expressions regulières --
|
||||
# Regular Expressions for that class
|
||||
@@StdTokenRegExp=/(\w+|"[^"]*")/
|
||||
# Notes
|
||||
@@Time=Regexp.new(%{#(#{@@StdTokenRegExp.inspect}(,|->))?#{@@StdTokenRegExp.inspect}})
|
||||
|
||||
def to_s
|
||||
res=%{created:#{@creation_date}}
|
||||
if ( @start_date ) : res+=%{,started:#{@start_date}} end
|
||||
if ( @done_date ) : res+=%{,done:#{@done_date}} end
|
||||
if ( @due_date ) : res+=%{,due:#{@due_date}} end
|
||||
if ( @duration ) : res+=%{,duration:#{@duration}} end
|
||||
if ( @max_duration ): res+=%{,max_duration:#{@duration}} end
|
||||
if ( @min_duration ): res+=%{,min_duration:#{@duration}} end
|
||||
return res
|
||||
end
|
||||
def taskTimeRegExp
|
||||
return Regexp.new(" #[^ ]*")
|
||||
|
||||
def from_s( raw_input )
|
||||
scanned_input=raw_input.scan( @@Time )
|
||||
str_of_start_date = scanned_input.map{ |x| x[4] }
|
||||
@start_date = Chronic.parse(str_of_start_date)
|
||||
str_of_due_date = scanned_input.map{ |x| x[1] }
|
||||
@due_date = Chronic.parse(str_of_due_date)
|
||||
end
|
||||
|
||||
def initialize ( raw_input=nil )
|
||||
@creation_date=Time.now
|
||||
@start_date = nil
|
||||
@done_date = nil
|
||||
@due_date = nil
|
||||
@duration = nil
|
||||
@max_duration = nil
|
||||
@min_duration = nil
|
||||
if raw_input:
|
||||
from_s raw_input
|
||||
end
|
||||
end
|
||||
|
||||
def regexp
|
||||
return @@Time
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue