Георги обнови решението на 20.12.2015 04:27 (преди около 9 години)
+class LazyMode
+ def self.create_file(file_name, &block)
+ file = File.new(file_name)
+ file.instance_eval(&block)
+ file
+ end
+
+
+ class Date
+ @@to_days_table = {"d" => 1, "w" => 7, "m" => 30}
+
+ def initialize(date_string)
+ @year, @month, @day, @repetition = date_string.split(/[-, +]/, 4)
+ @date_string = date_string[0..9]
+ end
+
+ def year
+ @year.to_i
+ end
+
+ def month
+ @month.to_i
+ end
+
+ def day
+ @day.to_i
+ end
+
+ def to_s
+ @date_string
+ end
+
+ def period
+ return nil if @repetition == nil
+ jump = @repetition.scan(/\d*/).join("").to_i
+ type = @repetition.match(/[dwm]/).to_s
+
+ jump * @@to_days_table[type]
+ end
+ def -(date)
+ days_now = (year - 1) * 360 + (month - 1) * 30 + day
+ days_date = (date.year - 1) * 360 + (date.month - 1) * 30 + date.day
+
+ days_date - days_now
+ end
+ def ===(date)
+ difference = self - date
+ return false if difference < 0
+
+ period ? difference % period == 0 : difference == 0
+ end
+ end
+
+
+
+
+ class File
+ attr_accessor :name, :notes
+
+ def initialize(name)
+ @name = name
+ @notes = []
+ end
+
+ def note(header, *tags, &block)
+ new_note = Note.new(self, header)
+ new_note.tags = tags
+ new_note.instance_eval(&block)
+ @notes.push(new_note)
+ new_note
+ end
+
+ def daily_agenda(date)
+ dummy_file = File.new("daily_agenda_'#{date.to_s}'")
+ dummy_file.notes = notes.map { |note| note.dup }
+ dummy_file.notes.delete_if { |note| !(note.date === date) }
+ dummy_file.notes.each { |note| note.date = date }
+
+ dummy_file
+ end
+
+
+ def weekly_agenda(date)
+ dummy_file = File.new("daily_agenda_'#{date.to_s}'")
+ dummy_file.notes = notes.select do |note|
+ (date - note.date).between?(0,6)
+ end
+
+ dummy_file
+ end
+
+ def filter_notes(tag: nil, text: nil, status: nil)
+
+ filtered = notes.reject { |note| !note.tags.include?(tag) and tag }
+ filtered = filtered.reject { |note| note.status != status and status }
+ filtered = filtered.reject do |note|
+ (note.body =~ text) == nil and (note.header =~ text) == nil and text
+ end
+ filtered
+ end
+
+ def where(tag: nil, text: nil, status: nil)
+ dummy_file = File.new("filter")
+ dummy_file.notes = filter_notes(tag: tag, text: text, status: status)
+ dummy_file
+ end
+ end
+
+
+
+ class Note
+ attr_accessor :tags, :file, :header, :date
+ def initialize(file, header)
+ @file = file
+ @header = header
+ @status = :topostpone
+ @body = ""
+ end
+ def body(new_body = nil)
+ @body = new_body if new_body
+ @body
+ end
+ def file_name
+ @file.name
+ end
+ def status(new_status = nil)
+ @status = new_status if new_status
+ @status
+ end
+ def note(header, *tags, &block)
+ new_note = Note.new(@file, header)
+ new_note.tags = tags
+ new_note.instance_eval(&block)
+ @file.notes.push(new_note)
+ new_note
+ end
+
+ def scheduled(date)
+ @date = Date.new(date)
+ end
+ end
+end