Решение на Седма задача от Димитър Ангелов

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

Към профила на Димитър Ангелов

Резултати

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

Код

class LazyMode
@files = {}
class Date
def initialize (date)
@date = date
end
def year
@date[0..3]
end
def month
@date[5..6]
end
def day
@date[8..9]
end
def to_s
@date
end
end
class Note
def initialize (header, *tags, file_name)
@header = header
@tags = tags
@date, @body = ""
@file_name = file_name
@status = :topostpone
@sub_notes = {}
end
attr_reader :date, :file_name
def header
@header
end
def tags
@tags
end
def scheduled (schedule)
@date = Date.new(schedule[0..9])
end
def body(body_string = "")
if body_string == "" then @body
else @body = body_string
end
end
def status(new_status = "")
if new_status == "" then @status
else @status = new_status
end
end
def note (header, *tags, &block)
sub_note = Note.new(header, *tags, @name)
sub_note.instance_eval &block if block_given?
@sub_notes[header] = sub_note
sub_note
end
end
class File
class Agenda
def initialize (date, notes)
@date = date
@notes = notes
end
def notes
@notes.select {|header, note| note.date == @date}
end
end
def initialize (name)
@name = name
@notes = {}
end
attr_reader :name
def notes
@notes.values
end
def note (header, *tags, &block)
new_note = Note.new(header, *tags, @name)
new_note.instance_eval &block if block_given?
@notes[header] = new_note
new_note
end
def daily_agenda (date)
Agenda.new date, @notes
end
def weekly_agenda(date)
end
end
def self.create_file (name, &block)
file = File.new(name)
file.instance_eval &block
@files[name] = file
file
end
end

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

FFF..F.....FFFFF.FFFFFFFFFFFF..

Failures:

  1) LazyMode LazyMode::Date can parse year
     Failure/Error: expect(date.year).to eq(2012)
       
       expected: 2012
            got: "2012"
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/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)
       
       expected: 10
            got: "10"
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/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)
       
       expected: 3
            got: "03"
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/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 can have nested notes
     Failure/Error: expect(file.notes.first.body).to eq('')
       
       expected: ""
            got: nil
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/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)>'

  5) LazyMode LazyMode::Note #body can have no body
     Failure/Error: expect(file.notes.first.body).to eq('')
       
       expected: ""
            got: nil
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/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)>'

  6) LazyMode#daily_agenda returns note scheduled without repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/spec.rb:131: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#daily_agenda returns note scheduled with daily repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/spec.rb:153: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)>'

  8) LazyMode#daily_agenda returns note scheduled with weekly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/spec.rb:175: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)>'

  9) LazyMode#daily_agenda returns note scheduled with monthly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/spec.rb:198: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)>'

  10) LazyMode#daily_agenda returns nested notes
     Failure/Error: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 0
       
       (compared using ==)
     # /tmp/d20160107-5693-ubio8j/spec.rb:232: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)>'

  11) LazyMode#weekly_agenda returns note scheduled without repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:249: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)>'

  12) LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
     Failure/Error: expect(agenda.notes.size).to eq(2)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:271: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)>'

  13) LazyMode#weekly_agenda returns note scheduled with weekly repeater
     Failure/Error: expect(agenda.notes.size).to eq(2)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:295: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#weekly_agenda returns note scheduled with monthly repeater
     Failure/Error: expect(agenda.notes.size).to eq(1)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:327: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#weekly_agenda does not return note whose start date is in the future
     Failure/Error: expect(agenda.notes.size).to eq(0)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:345: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#weekly_agenda returns nested notes
     Failure/Error: expect(agenda.notes.size).to eq(1)
     NoMethodError:
       undefined method `notes' for nil:NilClass
     # /tmp/d20160107-5693-ubio8j/spec.rb:361: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#where filters by tag
     Failure/Error: notes = @agenda.where(tag: :important).notes
     NoMethodError:
       undefined method `where' for #<LazyMode::File::Agenda:0x007f7997fa0920>
     # /tmp/d20160107-5693-ubio8j/spec.rb:391: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#where filters by body text
     Failure/Error: notes = @agenda.where(text: /Very/).notes
     NoMethodError:
       undefined method `where' for #<LazyMode::File::Agenda:0x007f7997f99698>
     # /tmp/d20160107-5693-ubio8j/spec.rb:409: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#where filters by header text
     Failure/Error: notes = @agenda.where(text: /not important/).notes
     NoMethodError:
       undefined method `where' for #<LazyMode::File::Agenda:0x007f7997f91498>
     # /tmp/d20160107-5693-ubio8j/spec.rb:421: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#where filters by status
     Failure/Error: notes = @agenda.where(status: :postponed).notes
     NoMethodError:
       undefined method `where' for #<LazyMode::File::Agenda:0x007f7997f86700>
     # /tmp/d20160107-5693-ubio8j/spec.rb:433: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#where filters by multiple filters
     Failure/Error: notes = @agenda.where(text: /important/, status: :postponed).notes
     NoMethodError:
       undefined method `where' for #<LazyMode::File::Agenda:0x007f7997f7e7f8>
     # /tmp/d20160107-5693-ubio8j/spec.rb:444: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.01709 seconds
31 examples, 21 failures

Failed examples:

rspec /tmp/d20160107-5693-ubio8j/spec.rb:3 # LazyMode LazyMode::Date can parse year
rspec /tmp/d20160107-5693-ubio8j/spec.rb:8 # LazyMode LazyMode::Date can parse month
rspec /tmp/d20160107-5693-ubio8j/spec.rb:13 # LazyMode LazyMode::Date can parse day
rspec /tmp/d20160107-5693-ubio8j/spec.rb:98 # LazyMode LazyMode::Note can have nested notes
rspec /tmp/d20160107-5693-ubio8j/spec.rb:88 # LazyMode LazyMode::Note #body can have no body
rspec /tmp/d20160107-5693-ubio8j/spec.rb:119 # LazyMode#daily_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:141 # LazyMode#daily_agenda returns note scheduled with daily repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:163 # LazyMode#daily_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:186 # LazyMode#daily_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:219 # LazyMode#daily_agenda returns nested notes
rspec /tmp/d20160107-5693-ubio8j/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-ubio8j/spec.rb:337 # LazyMode#weekly_agenda does not return note whose start date is in the future
rspec /tmp/d20160107-5693-ubio8j/spec.rb:348 # LazyMode#weekly_agenda returns nested notes
rspec /tmp/d20160107-5693-ubio8j/spec.rb:390 # LazyMode#where filters by tag
rspec /tmp/d20160107-5693-ubio8j/spec.rb:408 # LazyMode#where filters by body text
rspec /tmp/d20160107-5693-ubio8j/spec.rb:420 # LazyMode#where filters by header text
rspec /tmp/d20160107-5693-ubio8j/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-ubio8j/spec.rb:443 # LazyMode#where filters by multiple filters

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

Димитър обнови решението на 21.12.2015 16:42 (преди около 9 години)

+class LazyMode
+ @files = {}
+
+ class Date
+ def initialize (date)
+ @date = date
+ end
+
+ def year
+ @date[0..3]
+ end
+
+ def month
+ @date[5..6]
+ end
+
+ def day
+ @date[8..9]
+ end
+
+ def to_s
+ @date
+ end
+ end
+
+ class Note
+ def initialize (header, *tags, file_name)
+ @header = header
+ @tags = tags
+ @date, @body = ""
+ @file_name = file_name
+ @status = :topostpone
+ @sub_notes = {}
+ end
+
+ attr_reader :date, :file_name
+
+ def header
+ @header
+ end
+
+ def tags
+ @tags
+ end
+
+ def scheduled (schedule)
+ @date = Date.new(schedule[0..9])
+ end
+
+ def body(body_string = "")
+ if body_string == "" then @body
+ else @body = body_string
+ end
+ end
+
+ def status(new_status = "")
+ if new_status == "" then @status
+ else @status = new_status
+ end
+ end
+
+ def note (header, *tags, &block)
+ sub_note = Note.new(header, *tags, @name)
+ sub_note.instance_eval &block if block_given?
+ @sub_notes[header] = sub_note
+ sub_note
+ end
+ end
+
+ class File
+ class Agenda
+ def initialize (date, notes)
+ @date = date
+ @notes = notes
+ end
+
+ def notes
+ @notes.select {|header, note| note.date == @date}
+ end
+ end
+
+ def initialize (name)
+ @name = name
+ @notes = {}
+ end
+
+ attr_reader :name
+
+ def notes
+ @notes.values
+ end
+
+ def note (header, *tags, &block)
+ new_note = Note.new(header, *tags, @name)
+ new_note.instance_eval &block if block_given?
+ @notes[header] = new_note
+ new_note
+ end
+
+ def daily_agenda (date)
+ Agenda.new date, @notes
+ end
+
+ def weekly_agenda(date)
+ end
+ end
+
+ def self.create_file (name, &block)
+ file = File.new(name)
+ file.instance_eval &block
+ @files[name] = file
+ file
+ end
+end