Решение на Седма задача от Изтрит профил

Обратно към всички решения

Към профила на Изтрит профил

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 31 неуспешни тест(а)

Код

module LazyMode
class LazyMode::Date
def initialize(date)
info = /(\d{4})-(\d{2})-(\d{2})/.match(date)
@year = info[1].to_i
@month = info[2].to_i
@day = info[3].to_i
end
def to_s
"#{@year.to_s.rjust(4, "0")}-\
#{@month.to_s.rjust(2, "0")}-#{@day.to_s.rjust(2, "0")}"
end
end
class LazyMode::Note
attr_accessor :header, :tags, :date, :file_name
def initialize()
@status = :topostpone
@body = ""
@header = ""
@notes = []
@file_name = ""
end
def body(b)
@body = b
end
def get_body()
@body
end
def status(s)
@status = s
end
def get_status()
@status
end
def scheduled(date)
@date = LazyMode::Date.new(date)
@repeat_number, @repeat_interval = 1, 1
rep = date.match(/\+(\d)([w])/)
if rep
@repeat_number = rep[1].to_i
@repeat_interval = rep[2]
end
end
def note(header, *tags, &block)
note = LazyMode::Note.new()
note.instance_eval(&block)
note.header = header
note.tags = tags
@notes.push(note)
note
end
def agenda(target_time, res)
note_time = Time.local(@date.year, @date.month, @date.day).to_i
days_delta = (target_time - note_time)
if days_delta >= 0 &&
days_delta % (@repeat_number * @repeat_interval) == 0
res.notes.push(self)
end
@notes.each { |node| node.agenda(target_time, res) }
end
end
class Result
attr_accessor :notes
def initialize()
@notes = []
end
def where(tag: "", text: "", status: "")
filtered = @notes.filter { |note| note.tags.include? tag }
filtered = filtered.filter { |note| note.get_status.equal? status }
filtered = filtered.filter do |note|
note.get_body =~ text || note.header =~ text
end
end
end
class LazyMode::File
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
note = LazyMode::Note.new()
note.instance_eval(&block)
note.header = header
note.tags = tags
@notes.push(note)
note
end
def weekly_agenda(date)
res = Result.new()
initial_target_time = Time.local(date.year, date.month, date.day).to_i
for i in 0...7
target_time = initial_target_time + i * 60 * 60 * 24
recursive_agenda(target_time, res)
end
res
end
def recursive_agenda(target_time, res)
@notes.each do |note|
note.agenda(target_time, res)
end
end
def daily_agenda(date)
res = Result.new()
target_time = Time.local(date.year, date.month, date.day).to_i
recursive_agenda(target_time, res)
res
end
end
def self.create_file(file_name, &block)
file = LazyMode::File.new(file_name)
file.instance_eval(&block)
file
end
end

Лог от изпълнението

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) LazyMode LazyMode::Date can parse year
     Failure/Error: expect(date.year).to eq(2012)
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3de4b650 @year=2012, @month=10, @day=3>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) LazyMode LazyMode::Date can parse month
     Failure/Error: expect(date.month).to eq(10)
     NoMethodError:
       undefined method `month' for #<LazyMode::Date:0x007fac3de482e8 @year=2012, @month=10, @day=3>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:10:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) LazyMode LazyMode::Date can parse day
     Failure/Error: expect(date.day).to eq(3)
     NoMethodError:
       undefined method `day' for #<LazyMode::Date:0x007fac3de380f0 @year=2012, @month=10, @day=3>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:15:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) LazyMode LazyMode::Note has file_name
     Failure/Error: expect(file.notes.first.file_name).to eq('my_todos')
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3de355a8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:26:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) LazyMode LazyMode::Note has header
     Failure/Error: expect(file.notes.first.header).to eq('todo_list')
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3de333e8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:35:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) LazyMode LazyMode::Note can have nested notes
     Failure/Error: expect(file.notes.first.body).to eq('')
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3de30f80>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:114:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) LazyMode LazyMode::Note #tags has tags
     Failure/Error: expect(file.notes.first.tags).to eq([:important, :wip, :blocker])
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3de0c090>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:45:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) LazyMode LazyMode::Note #tags can have no tags
     Failure/Error: expect(file.notes.first.tags).to eq([])
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3de098b8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:54:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) LazyMode LazyMode::Note #status has status
     Failure/Error: expect(file.notes.first.status).to eq(:postponed)
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3da885e0>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:65:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) LazyMode LazyMode::Note #status can have default status
     Failure/Error: expect(file.notes.first.status).to eq(:topostpone)
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3dad3158>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:74:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) LazyMode LazyMode::Note #body has body
     Failure/Error: expect(file.notes.first.body).to eq('Do not forget to...')
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3dacfcd8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:85:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) LazyMode LazyMode::Note #body can have no body
     Failure/Error: expect(file.notes.first.body).to eq('')
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3dac3348>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:94:in `block (4 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) LazyMode#daily_agenda returns note scheduled without repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3daaeb78 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:130:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) LazyMode#daily_agenda returns note scheduled with daily repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da8f3e0 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:152:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) LazyMode#daily_agenda returns note scheduled with weekly repeater
     Failure/Error: scheduled '2012-12-5 +1w'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160107-5693-1gml0qe/solution.rb:5:in `initialize'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:44:in `new'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:44:in `scheduled'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:166:in `block (5 levels) in <top (required)>'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:100:in `instance_eval'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:100:in `note'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:165:in `block (4 levels) in <top (required)>'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:139:in `instance_eval'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:139:in `create_file'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:164:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) LazyMode#daily_agenda returns note scheduled with monthly repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2013-01-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da62598 @year=2013, @month=1, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:197:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) LazyMode#daily_agenda does not return note whose start date is in the future
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-10-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da5e0b0 @year=2012, @month=10, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:215:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) LazyMode#daily_agenda returns nested notes
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-08-06'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da40358 @year=2012, @month=8, @day=6>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:230:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) LazyMode#weekly_agenda returns note scheduled without repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-12-06'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da25a58 @year=2012, @month=12, @day=6>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:110:in `weekly_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:248:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-12-06'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3da085e8 @year=2012, @month=12, @day=6>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:110:in `weekly_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:270:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) LazyMode#weekly_agenda returns note scheduled with weekly repeater
     Failure/Error: scheduled '2012-12-5 +1w'
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20160107-5693-1gml0qe/solution.rb:5:in `initialize'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:44:in `new'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:44:in `scheduled'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:286:in `block (5 levels) in <top (required)>'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:100:in `instance_eval'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:100:in `note'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:285:in `block (4 levels) in <top (required)>'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:139:in `instance_eval'
     # /tmp/d20160107-5693-1gml0qe/solution.rb:139:in `create_file'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:284:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) LazyMode#weekly_agenda returns note scheduled with monthly repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2013-01-10'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d9dac60 @year=2013, @month=1, @day=10>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:110:in `weekly_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:326:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) LazyMode#weekly_agenda does not return note whose start date is in the future
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-10-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d9d23f8 @year=2012, @month=10, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:110:in `weekly_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:344:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) LazyMode#weekly_agenda returns nested notes
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-09-05'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d9ca4a0 @year=2012, @month=9, @day=5>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:110:in `weekly_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:359:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) LazyMode#where filters by tag
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d9a6ca8 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:387:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) LazyMode#where filters by body text
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d9341a8 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:387:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) LazyMode#where filters by header text
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d921760 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:387:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) LazyMode#where filters by status
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d916ce8 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:387:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) LazyMode#where filters by multiple filters
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `year' for #<LazyMode::Date:0x007fac3d90b5a0 @year=2012, @month=12, @day=12>
     # /tmp/d20160107-5693-1gml0qe/solution.rb:129:in `daily_agenda'
     # /tmp/d20160107-5693-1gml0qe/spec.rb:387:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) LazyMode.create_file can assign name to files
     Failure/Error: expect(file.name).to eq('important_notes_and_todos')
     NoMethodError:
       undefined method `name' for #<LazyMode::File:0x007fac3d904fe8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:460:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  31) LazyMode.create_file can build notes in the block
     Failure/Error: expect(file.notes.size).to eq(3)
     NoMethodError:
       undefined method `notes' for #<LazyMode::File:0x007fac3d8fe0f8>
     # /tmp/d20160107-5693-1gml0qe/spec.rb:477:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0138 seconds
31 examples, 31 failures

Failed examples:

rspec /tmp/d20160107-5693-1gml0qe/spec.rb:3 # LazyMode LazyMode::Date can parse year
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:8 # LazyMode LazyMode::Date can parse month
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:13 # LazyMode LazyMode::Date can parse day
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:20 # LazyMode LazyMode::Note has file_name
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:29 # LazyMode LazyMode::Note has header
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:98 # LazyMode LazyMode::Note can have nested notes
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:39 # LazyMode LazyMode::Note #tags has tags
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:48 # LazyMode LazyMode::Note #tags can have no tags
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:59 # LazyMode LazyMode::Note #status has status
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:68 # LazyMode LazyMode::Note #status can have default status
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:79 # LazyMode LazyMode::Note #body has body
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:88 # LazyMode LazyMode::Note #body can have no body
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:119 # LazyMode#daily_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:141 # LazyMode#daily_agenda returns note scheduled with daily repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:163 # LazyMode#daily_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:186 # LazyMode#daily_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:208 # LazyMode#daily_agenda does not return note whose start date is in the future
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:219 # LazyMode#daily_agenda returns nested notes
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:337 # LazyMode#weekly_agenda does not return note whose start date is in the future
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:348 # LazyMode#weekly_agenda returns nested notes
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:390 # LazyMode#where filters by tag
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:408 # LazyMode#where filters by body text
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:420 # LazyMode#where filters by header text
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:443 # LazyMode#where filters by multiple filters
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:456 # LazyMode.create_file can assign name to files
rspec /tmp/d20160107-5693-1gml0qe/spec.rb:463 # LazyMode.create_file can build notes in the block

История (1 версия и 0 коментара)

Изтрит обнови решението на 21.12.2015 17:04 (преди около 9 години)

+module LazyMode
+ class LazyMode::Date
+ def initialize(date)
+ info = /(\d{4})-(\d{2})-(\d{2})/.match(date)
+ @year = info[1].to_i
+ @month = info[2].to_i
+ @day = info[3].to_i
+ end
+
+ def to_s
+ "#{@year.to_s.rjust(4, "0")}-\
+ #{@month.to_s.rjust(2, "0")}-#{@day.to_s.rjust(2, "0")}"
+ end
+ end
+
+ class LazyMode::Note
+ attr_accessor :header, :tags, :date, :file_name
+
+ def initialize()
+ @status = :topostpone
+ @body = ""
+ @header = ""
+ @notes = []
+ @file_name = ""
+ end
+
+ def body(b)
+ @body = b
+ end
+
+ def get_body()
+ @body
+ end
+
+ def status(s)
+ @status = s
+ end
+
+ def get_status()
+ @status
+ end
+
+ def scheduled(date)
+ @date = LazyMode::Date.new(date)
+ @repeat_number, @repeat_interval = 1, 1
+
+ rep = date.match(/\+(\d)([w])/)
+ if rep
+ @repeat_number = rep[1].to_i
+ @repeat_interval = rep[2]
+ end
+ end
+
+ def note(header, *tags, &block)
+ note = LazyMode::Note.new()
+ note.instance_eval(&block)
+ note.header = header
+ note.tags = tags
+ @notes.push(note)
+
+ note
+ end
+
+ def agenda(target_time, res)
+ note_time = Time.local(@date.year, @date.month, @date.day).to_i
+ days_delta = (target_time - note_time)
+ if days_delta >= 0 &&
+ days_delta % (@repeat_number * @repeat_interval) == 0
+ res.notes.push(self)
+ end
+
+ @notes.each { |node| node.agenda(target_time, res) }
+ end
+ end
+
+ class Result
+ attr_accessor :notes
+
+ def initialize()
+ @notes = []
+ end
+
+ def where(tag: "", text: "", status: "")
+ filtered = @notes.filter { |note| note.tags.include? tag }
+ filtered = filtered.filter { |note| note.get_status.equal? status }
+ filtered = filtered.filter do |note|
+ note.get_body =~ text || note.header =~ text
+ end
+ end
+ end
+
+ class LazyMode::File
+ def initialize(name)
+ @name = name
+ @notes = []
+ end
+
+ def note(header, *tags, &block)
+ note = LazyMode::Note.new()
+ note.instance_eval(&block)
+ note.header = header
+ note.tags = tags
+ @notes.push(note)
+
+ note
+ end
+
+ def weekly_agenda(date)
+ res = Result.new()
+ initial_target_time = Time.local(date.year, date.month, date.day).to_i
+
+ for i in 0...7
+ target_time = initial_target_time + i * 60 * 60 * 24
+
+ recursive_agenda(target_time, res)
+ end
+
+ res
+ end
+
+ def recursive_agenda(target_time, res)
+ @notes.each do |note|
+ note.agenda(target_time, res)
+ end
+ end
+
+ def daily_agenda(date)
+ res = Result.new()
+ target_time = Time.local(date.year, date.month, date.day).to_i
+
+ recursive_agenda(target_time, res)
+
+ res
+ end
+ end
+
+ def self.create_file(file_name, &block)
+ file = LazyMode::File.new(file_name)
+ file.instance_eval(&block)
+
+ file
+ end
+end