Решение на Седма задача от Петър Нетовски

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

Към профила на Петър Нетовски

Резултати

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

Код

class LazyMode
class Date
attr_accessor :year, :month, :day
def initialize(date)
match_data = /\A(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})\z/.match date
if match_data
@year = match_data[:year].to_i
@month = match_data[:month].to_i
@day = match_data[:day].to_i
end
end
def to_s
"%04d-%02d-%02d" % [@year, @month, @day]
end
end
class Note
attr_accessor :header, :file_name, :body, :status, :tags
attr_accessor :date, :times, :sub_notes
def initialize(header, file_name, *tags)
@header, @file_name, @tags = header, file_name, tags
@body, @status, @sub_notes = "", :topostpone, []
end
class << self
def get_setup_block
Proc.new do
def scheduled(date)
match_data = /(?<date>\d{4}-\d{2}-\d{2})(?<opt>| +\d\w)/.match date
@date = LazyMode::Date.new(match_data[:date])
@times = match_data[:opt]
end
def status(state)
@status = state
end
def body(note_text)
@body = note_text
end
def note(header, *tags, &note_block)
new_note = LazyMode::Note.new(header, @file_name, *tags)
note_setup_block = LazyMode::Note.get_setup_block
new_note.instance_eval(&note_setup_block)
new_note.instance_eval(&note_block)
@sub_notes << new_note.dup
end
end
end
end
end
class File
attr_accessor :name, :notes
def initialize(file_name)
@name = file_name
@notes = []
end
class << self
def get_setup_block
Proc.new do
def note(header, *tags, &note_block)
new_note = LazyMode::Note.new(header, @name, *tags)
note_setup_block = LazyMode::Note.get_setup_block
new_note.instance_eval(&note_setup_block)
new_note.instance_eval(&note_block)
@notes << new_note.dup
end
end
end
end
def get_all_notes(date)
all_notes = []
@notes.each do |note|
all_notes << note
all_notes += note.sub_notes
end
all_notes.reject {|note| note.date.to_s != date.to_s}
end
def daily_agenda(date)
object = Class.new do
def initialize(notes)
@notes = notes
end
def notes
@notes
end
end
object.new(get_all_notes(date))
end
end
class << self
def create_file(file_name, &file_block)
file = LazyMode::File.new(file_name)
file_setup_block = LazyMode::File.get_setup_block
file.instance_eval(&file_setup_block)
file.instance_eval(&file_block)
file.dup
end
end
end

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

.............FFF..FFFFFFFFFFF..

Failures:

  1) 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-ahi0dw/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)>'

  2) 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-ahi0dw/solution.rb:33:in `scheduled'
     # /tmp/d20160107-5693-ahi0dw/spec.rb:166:in `block (5 levels) in <top (required)>'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:72:in `instance_eval'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:72:in `note'
     # /tmp/d20160107-5693-ahi0dw/spec.rb:165:in `block (4 levels) in <top (required)>'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:109:in `instance_eval'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:109:in `create_file'
     # /tmp/d20160107-5693-ahi0dw/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)>'

  3) 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-ahi0dw/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)>'

  4) LazyMode#weekly_agenda returns note scheduled without repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-12-06'))
     NoMethodError:
       undefined method `weekly_agenda' for #<LazyMode::File:0x007fd974170728>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  5) 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 `weekly_agenda' for #<LazyMode::File:0x007fd974165ad0>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  6) 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-ahi0dw/solution.rb:33:in `scheduled'
     # /tmp/d20160107-5693-ahi0dw/spec.rb:286:in `block (5 levels) in <top (required)>'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:72:in `instance_eval'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:72:in `note'
     # /tmp/d20160107-5693-ahi0dw/spec.rb:285:in `block (4 levels) in <top (required)>'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:109:in `instance_eval'
     # /tmp/d20160107-5693-ahi0dw/solution.rb:109:in `create_file'
     # /tmp/d20160107-5693-ahi0dw/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)>'

  7) LazyMode#weekly_agenda returns note scheduled with monthly repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2013-01-10'))
     NoMethodError:
       undefined method `weekly_agenda' for #<LazyMode::File:0x007fd9741f0d60>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  8) 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 `weekly_agenda' for #<LazyMode::File:0x007fd974206318>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  9) LazyMode#weekly_agenda returns nested notes
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-09-05'))
     NoMethodError:
       undefined method `weekly_agenda' for #<LazyMode::File:0x007fd9741fe2a8>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  10) LazyMode#where filters by tag
     Failure/Error: notes = @agenda.where(tag: :important).notes
     NoMethodError:
       undefined method `where' for #<#<Class:0x007fd9741f9f28>:0x007fd9741f9190>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  11) LazyMode#where filters by body text
     Failure/Error: notes = @agenda.where(text: /Very/).notes
     NoMethodError:
       undefined method `where' for #<#<Class:0x007fd9741e81b0>:0x007fd9741e7a80>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  12) LazyMode#where filters by header text
     Failure/Error: notes = @agenda.where(text: /not important/).notes
     NoMethodError:
       undefined method `where' for #<#<Class:0x007fd9741df3a8>:0x007fd9741deb88>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  13) LazyMode#where filters by status
     Failure/Error: notes = @agenda.where(status: :postponed).notes
     NoMethodError:
       undefined method `where' for #<#<Class:0x007fd974112b50>:0x007fd974112330>
     # /tmp/d20160107-5693-ahi0dw/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)>'

  14) LazyMode#where filters by multiple filters
     Failure/Error: notes = @agenda.where(text: /important/, status: :postponed).notes
     NoMethodError:
       undefined method `where' for #<#<Class:0x007fd9741d9958>:0x007fd9741d9278>
     # /tmp/d20160107-5693-ahi0dw/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.01858 seconds
31 examples, 14 failures

Failed examples:

rspec /tmp/d20160107-5693-ahi0dw/spec.rb:141 # LazyMode#daily_agenda returns note scheduled with daily repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:163 # LazyMode#daily_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:186 # LazyMode#daily_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:337 # LazyMode#weekly_agenda does not return note whose start date is in the future
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:348 # LazyMode#weekly_agenda returns nested notes
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:390 # LazyMode#where filters by tag
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:408 # LazyMode#where filters by body text
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:420 # LazyMode#where filters by header text
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-ahi0dw/spec.rb:443 # LazyMode#where filters by multiple filters

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

Петър обнови решението на 21.12.2015 00:18 (преди около 9 години)

+class LazyMode
+ class Date
+ attr_accessor :year, :month, :day
+
+ def initialize(date)
+ match_data = /\A(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})\z/.match date
+ if match_data
+ @year = match_data[:year].to_i
+ @month = match_data[:month].to_i
+ @day = match_data[:day].to_i
+ end
+ end
+
+ def to_s
+ "%04d-%02d-%02d" % [@year, @month, @day]
+ end
+ end
+
+ class Note
+ attr_accessor :header, :file_name, :body, :status, :tags
+ attr_accessor :date, :times, :sub_notes
+
+ def initialize(header, file_name, *tags)
+ @header, @file_name, @tags = header, file_name, tags
+ @body, @status, @sub_notes = "", :topostpone, []
+ end
+
+ class << self
+ def get_setup_block
+ Proc.new do
+ def scheduled(date)
+ match_data = /(?<date>\d{4}-\d{2}-\d{2})(?<opt>| +\d\w)/.match date
+ @date = LazyMode::Date.new(match_data[:date])
+ @times = match_data[:opt]
+ end
+
+ def status(state)
+ @status = state
+ end
+
+ def body(note_text)
+ @body = note_text
+ end
+
+ def note(header, *tags, &note_block)
+ new_note = LazyMode::Note.new(header, @file_name, *tags)
+ note_setup_block = LazyMode::Note.get_setup_block
+ new_note.instance_eval(&note_setup_block)
+ new_note.instance_eval(&note_block)
+ @sub_notes << new_note.dup
+ end
+ end
+ end
+ end
+ end
+
+ class File
+ attr_accessor :name, :notes
+
+ def initialize(file_name)
+ @name = file_name
+ @notes = []
+ end
+
+ class << self
+ def get_setup_block
+ Proc.new do
+ def note(header, *tags, &note_block)
+ new_note = LazyMode::Note.new(header, @name, *tags)
+ note_setup_block = LazyMode::Note.get_setup_block
+ new_note.instance_eval(&note_setup_block)
+ new_note.instance_eval(&note_block)
+ @notes << new_note.dup
+ end
+ end
+ end
+ end
+
+ def get_all_notes(date)
+ all_notes = []
+ @notes.each do |note|
+ all_notes << note
+ all_notes += note.sub_notes
+ end
+
+ all_notes.reject {|note| note.date.to_s != date.to_s}
+ end
+
+ def daily_agenda(date)
+ object = Class.new do
+ def initialize(notes)
+ @notes = notes
+ end
+
+ def notes
+ @notes
+ end
+ end
+
+ object.new(get_all_notes(date))
+ end
+ end
+
+ class << self
+ def create_file(file_name, &file_block)
+ file = LazyMode::File.new(file_name)
+ file_setup_block = LazyMode::File.get_setup_block
+ file.instance_eval(&file_setup_block)
+ file.instance_eval(&file_block)
+ file.dup
+ end
+ end
+end