lib/sculd/plan.rb in sculd-0.1.3 vs lib/sculd/plan.rb in sculd-0.2.0

- old
+ new

@@ -1,93 +1,87 @@ #! /usr/bin/env ruby # coding: utf-8 -#require "sculd/plan/deadline.rb" -#require "sculd/plan/reminder.rb" -#require "sculd/plan/schedule.rb" -#require "sculd/plan/todo.rb" - # # # class Sculd::Plan - class NotDefinedError < Exception; end - class WeekdayMismatchError < Exception; end - class NotWeekdayError < Exception; end - class NotNumberError < Exception; end + class NotDefinedError < Exception; end + class WeekdayMismatchError < Exception; end + class NotWeekdayError < Exception; end + class NotNumberError < Exception; end - attr_reader :description, :flag_time, :datetime, :option + attr_reader :description, :flag_time, :datetime, :option - REMINDER_PRIORITY = 10000 - DEADLINE_PRIORITY = 20000 + REMINDER_PRIORITY = 10000 + DEADLINE_PRIORITY = 20000 - # Parse and return date, type, option. - def self.parse(str, io = $stdout) - #/\[([\d\- :]*)\](.)(\S*)/ =~ str #OK - /\[([^\]]*)\](.)(\S*)\s+(.*)/ =~ str #OK - result = {} + # Parse and return date, type, option. + def self.parse(str, io = $stdout) + #/\[([\d\- :]*)\](.)(\S*)/ =~ str #OK + /\[([^\]]*)\](.)(\S*)\s+(.*)/ =~ str #OK + result = {} - date, type, option, description = $1, $2, $3, $4 + date, type, option, description = $1, $2, $3, $4 - datetime = DateTime::parse date - if /\((.+)\)/ =~ date - unless self.wday($1) == datetime.wday - io.puts "ERROR:" - io.puts "#{datetime} is #{Sculd::Manager::WEEKDAYS[datetime.wday]}," - io.puts "but string contains #{date}." - raise WeekdayMismatchError - end - end - result[:datetime] = datetime - result[:flag_time] = date.include?(":") + datetime = DateTime::parse date + if /\((.+)\)/ =~ date + weekday = self.wday($1) + unless weekday == datetime.wday + raise WeekdayMismatchError, + "#{datetime} is #{Sculd::Manager::DEFAULT_WEEKDAYS[datetime.wday]}, but string contains #{date}." + end + end + result[:datetime] = datetime + result[:flag_time] = date.include?(":") - result[:type] = type + result[:type] = type - unless option.empty? - raise NotNumberError unless option =~ /^[0-9]+$/ - result[:option] = option.to_i - end + unless option.empty? + raise NotNumberError unless option =~ /^[0-9]+$/ + result[:option] = option.to_i + end - result[:description] = description + result[:description] = description - return result - end + return result + end - def self.wday(str) - case str - when /^Su/i - return 0 - when /^M/i - return 1 - when /^Tu/i - return 2 - when /^W/i - return 3 - when /^Th/i - return 4 - when /^F/i - return 5 - when /^Sa/i - return 6 - else - raise NotWeekdayError - end + def self.wday(str) + case str + when /^Su/i , '日' + return 0 + when /^M/i , '月' + return 1 + when /^Tu/i , '火' + return 2 + when /^W/i , '水' + return 3 + when /^Th/i , '木' + return 4 + when /^F/i , '金' + return 5 + when /^Sa/i , '土' + return 6 + else + raise NotWeekdayError, "Unknown format string: #{str}" end + end - # - def initialize(datetime, flag_time, option, description) - @datetime = datetime - @flag_time = flag_time - @option = option - @description = description - end + # + def initialize(datetime, flag_time, option, description) + @datetime = datetime + @flag_time = flag_time + @option = option + @description = description + end - # return priority of task calculated by equation defined in subclass. - def priority - raise NotDefinedError - end + # return priority of task calculated by equation defined in subclass. + def priority + raise NotDefinedError + end - def event_dates - raise NotDefinedError - end + def event_dates + raise NotDefinedError + end end