Николай обнови решението на 21.12.2015 12:32 (преди около 9 години)
+## src/lazy_mode/agenda.rb
+module LazyMode
+ class Agenda
+ attr_accessor :notes, :date
+
+ def initialize (notes, date = nil)
+ @notes = notes.map {|note| NoteProxy.new(date, note)}
+ @date = date
+ end
+
+ def where( * args)
+ raise NotImplementedError, "soon™"
+ end
+
+ class NoteProxy
+ attr_reader :date, :note
+
+ def initialize(date = nil, note)
+ @date = date || note.date
+ @note = note
+ end
+
+ def method_missing(name, *args, &block)
+ @note.send(name, *args, &block)
+ end
+ end
+ end
+end
+
+# src/lazy_mode/date.rb
+module LazyMode
+ class Date
+ @@repeater_values = {
+ "d" => 1,
+ "w" => 7,
+ "m" => 30,
+ }
+ attr_reader :day, :month, :year
+
+ def self.date_hash(number)
+ hash = {}
+ {year: 360, month: 30, day: 1}.each_pair do |type, value|
+ hash[type] = number / value
+ number -= hash[type] * value
+ end
+ hash
+ end
+
+ def self.build_from_number(number)
+ raise "No negative dates" if number < 0
+ hash = date_hash(number)
+ date_string = "#{hash[:year].to_s.rjust(4, '0')}-"
+ date_string += "#{hash[:month].to_s.rjust(2, '0')}-"
+ date_string += "#{hash[:day].to_s.rjust(2, '0')}"
+ new(date_string)
+ end
+
+
+ def initialize(date)
+ match_object = date.match(/(\d{4,4})-(\d\d)-(\d\d)\s?\+?(\d+[dwm])?/)
+ if match_object
+ @year = match_object[1].to_i
+ @month = match_object[2].to_i
+ @day = match_object[3].to_i
+ @repeater = match_object[4]
+ end
+ end
+
+ def to_s
+ year_string = @year.to_s.rjust(4, '0')
+ month_string = @month.to_s.rjust(2, '0')
+ day_string = @day.to_s.rjust(2, '0')
+ "#{year_string}-#{month_string}-#{day_string}"
+ end
+
+ def to_i
+ 30 * 12 * @year + 30 * @month + @day
+ end
+
+ def ==(other)
+ check_equality(other) || other.check_equality(self)
+ end
+
+ def check_equality(other_date)
+ own_number, other_number = to_i, other_date.to_i
+ return own_number == other_number unless @repeater
+ return false if own_number > other_number
+ number = @repeater.match(/(\d+)/)[0].to_i
+ number * @@repeater_values[@repeater.match(/\d+(\w)/)[1]]
+ (other_number - own_number) % number == 0
+ end
+
+ def successor
+ self.class.build_from_number(to_i + 1)
+ end
+ end
+end
+
+# src/lazy_mode/file.rb
+module LazyMode
+ class File
+ attr_reader :name, :notes
+
+ def initialize(name, notes)
+ @name = name
+ @notes = notes
+ end
+
+ def daily_agenda(date)
+ agenda_notes = notes_recursively.select {|note| note.date == date}
+ LazyMode::Agenda.new(agenda_notes, date)
+ end
+
+ # def weekly_agenda(date)
+ # agenda_notes = []
+ # parse_date = date.dup
+ # 7.times do
+ # agenda_notes << notes_recursively
+ # parse_date = parse_date.successor
+ # end
+ # LazyMode::Agenda.new(agenda_notes.flatten.uniq, date)
+ # end
+
+
+ def notes_recursively
+ @notes.map { |note| note.notes_recursively }.flatten
+ end
+ end
+end
+
+# src/lazy_mode/lazy_mode.rb
+module LazyMode
+ class << self
+ def create_file(name, &block)
+ file_builder = FileBuilder.new(name)
+ file_builder.instance_eval(&block)
+ file_builder.build
+ end
+ end
+
+
+ class FileBuilder
+ attr_accessor :file_name
+ include Notable
+
+ def initialize(file_name)
+ @file_name = file_name
+ @notes = []
+ end
+
+ def build
+ File.new(@file_name, @notes)
+ end
+ end
+end
+
+# src/lazy_mode/note.rb
+module LazyMode
+ class Note
+ attr_accessor :header, :file_name, :body, :status, :tags, :date, :notes
+
+ def initialize(options = {})
+ @header = options[:header]
+ @file_name = options[:file_name]
+ @date, @body = options[:date], options[:body]
+ @status = options[:status] || :topostpone
+ @tags = options[:tags] || []
+ @notes = options[:notes] || []
+ end
+
+ def notes_recursively
+ @notes.map { |note| note.notes_recursively } + [self]
+ end
+ end
+end
+
+# src/lazy_mode/note_builder.rb
+module LazyMode
+ class NoteBuilder
+ # include LazyMode::Notable
+
+ def initialize(header, tags, file_name)
+ @header = header
+ @tags = tags
+ @file_name = file_name
+ @notes = []
+ end
+
+ def build
+ Note.new({header: @header, file_name: @file_name, tags: @tags,
+ date: @date, status: @status, body: @body, notes: @notes})
+ end
+
+ # Don't forget to change Notable#note too
+ def note(header, *tags, &block)
+ note_builder = NoteBuilder.new(header,tags, @file_name)
+ note_builder.instance_eval(&block)
+ @notes << note_builder.build
+ end
+
+ def scheduled(date_string)
+ @date = Date.new(date_string)
+ end
+
+ def status(status)
+ @status = status
+ end
+
+ def body(body)
+ @body = body
+ end
+ end
+end
+
+# src/lazy_mode/notable.rb
+module LazyMode
+ module Notable
+ attr_accessor :notes
+
+ # Don't forget to change NoteBuilder#note, too
+ # Had to do the terrible thing with the splat *, because of skeptic
+ def note(header, * tags, &block)
+ note_builder = LazyMode::NoteBuilder.new(header, tags, @file_name)
+ note_builder.instance_eval(&block)
+ @notes << note_builder.build
+ end
+ end
+end