Решение на Седма задача от Анджелин Неделчев

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

Към профила на Анджелин Неделчев

Резултати

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

Код

module LazyMode
def self.create_file(name, &block)
file = File.new(name)
file.instance_eval &block
file
end
class Date
YEAR_COUNT = 4
MONTH_COUNT = 2
DAY_COUNT = 2
attr_reader :year
attr_reader :month
attr_reader :day
def initialize(date_string)
unless date_string =~ /^\d+-\d+-\d+$/
raise ArgumentError, 'invalid date format'
end
split = date_string.split('-')
@year = '0' * (YEAR_COUNT - split[0].size) + split[0]
@month = '0' * (MONTH_COUNT - split[1].size) + split[1]
@day = '0' * (DAY_COUNT - split[2].size) + split[2]
end
def to_s
"%s-%s-%s" % [@year, @month, @day]
end
def add(period)
match = period.match(/(\d+)(\w+)/)
puts match
@days += period_to_days(match[0], match[1])
end
def period_to_days(amount, type)
case type
when 'w' then amount * 7
when 'd' then amount
when 'm' then amount * 30
end
end
end
class Note
attr_reader :header
attr_reader :file_name
attr_reader :tags
attr_reader :period
def initialize(header, file_name, *tags)
@header = header
@file_name = file_name
@tags = tags
@status = :topostpone
@body = ''
@notes = []
end
def scheduled(date = nil)
return @scheduled unless date
split = date.split(' ')
@period = split[1] if split.size > 1
@scheduled = Date.new(split[0])
end
def status(status = nil)
return @status unless status
@status = status
end
def body(body = nil)
return @body unless body
@body = body
end
def note(header, *tags, &block)
@notes << Note.new(header, @name, *tags)
@notes.last.instance_eval &block
end
end
class File
attr_reader :name
attr_reader :notes
def initialize(name)
@name = name
@notes = []
end
def note(header, *tags, &block)
@notes << Note.new(header, @name, *tags)
@notes.last.instance_eval &block
end
# def daily_agenda(target_date)
# agenda = []
# agenda_iteration(@notes, agenda, target_date)
# return Class.new do
# def note
# agenda
# end
# end
# end
# def agenda_iteration(notes, agenda, target_date)
# notes.each do |note|
# agenda << note if note.scheduled == target_date
# loop
# periodic = note.scheduled.add(note.period) if note.period
# break unless periodic
# break if periodic > target_date
# if periodic == target_date
# agenda << note
# end
# end
# agenda_iteration(note.notes, agenda, target_date)
# end
# end
end
end

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

FFF.........FFFFFFFFFFFFFFFFF..

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

  5) LazyMode#daily_agenda returns note scheduled with daily repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba4018b90>
     # /tmp/d20160107-5693-th7szj/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)>'

  6) LazyMode#daily_agenda returns note scheduled with weekly repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3ff5500>
     # /tmp/d20160107-5693-th7szj/spec.rb:174: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 monthly repeater
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2013-01-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3feb488>
     # /tmp/d20160107-5693-th7szj/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)>'

  8) 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 `daily_agenda' for #<LazyMode::File:0x007feba3fcd460>
     # /tmp/d20160107-5693-th7szj/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)>'

  9) LazyMode#daily_agenda returns nested notes
     Failure/Error: agenda = file.daily_agenda(LazyMode::Date.new('2012-08-06'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3fc9658>
     # /tmp/d20160107-5693-th7szj/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)>'

  10) 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:0x007feba3f8bda8>
     # /tmp/d20160107-5693-th7szj/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)>'

  11) 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:0x007feba3f74298>
     # /tmp/d20160107-5693-th7szj/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)>'

  12) LazyMode#weekly_agenda returns note scheduled with weekly repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2012-12-10'))
     NoMethodError:
       undefined method `weekly_agenda' for #<LazyMode::File:0x007feba3f5f8e8>
     # /tmp/d20160107-5693-th7szj/spec.rb:294: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 monthly repeater
     Failure/Error: agenda = file.weekly_agenda(LazyMode::Date.new('2013-01-10'))
     NoMethodError:
       undefined method `weekly_agenda' for #<LazyMode::File:0x007feba3f56db0>
     # /tmp/d20160107-5693-th7szj/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)>'

  14) 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:0x007feba3f4cc98>
     # /tmp/d20160107-5693-th7szj/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)>'

  15) 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:0x007feba3ec3010>
     # /tmp/d20160107-5693-th7szj/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)>'

  16) LazyMode#where filters by tag
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3eaf8d0>
     # /tmp/d20160107-5693-th7szj/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)>'

  17) LazyMode#where filters by body text
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3ea00d8>
     # /tmp/d20160107-5693-th7szj/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)>'

  18) LazyMode#where filters by header text
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3e945f8>
     # /tmp/d20160107-5693-th7szj/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)>'

  19) LazyMode#where filters by status
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3e7b878>
     # /tmp/d20160107-5693-th7szj/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)>'

  20) LazyMode#where filters by multiple filters
     Failure/Error: @agenda = file.daily_agenda(LazyMode::Date.new('2012-12-12'))
     NoMethodError:
       undefined method `daily_agenda' for #<LazyMode::File:0x007feba3e60c80>
     # /tmp/d20160107-5693-th7szj/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)>'

Finished in 0.01466 seconds
31 examples, 20 failures

Failed examples:

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

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

Анджелин обнови решението на 21.12.2015 13:01 (преди около 9 години)

+module LazyMode
+
+ def self.create_file(name, &block)
+ file = File.new(name)
+ file.instance_eval &block
+ file
+ end
+
+ class Date
+ YEAR_COUNT = 4
+ MONTH_COUNT = 2
+ DAY_COUNT = 2
+
+ attr_reader :year
+ attr_reader :month
+ attr_reader :day
+
+ def initialize(date_string)
+ unless date_string =~ /^\d+-\d+-\d+$/
+ raise ArgumentError, 'invalid date format'
+ end
+
+ split = date_string.split('-')
+
+ @year = '0' * (YEAR_COUNT - split[0].size) + split[0]
+ @month = '0' * (MONTH_COUNT - split[1].size) + split[1]
+ @day = '0' * (DAY_COUNT - split[2].size) + split[2]
+ end
+
+ def to_s
+ "%s-%s-%s" % [@year, @month, @day]
+ end
+
+ def add(period)
+ match = period.match(/(\d+)(\w+)/)
+ puts match
+ @days += period_to_days(match[0], match[1])
+ end
+
+ def period_to_days(amount, type)
+ case type
+ when 'w' then amount * 7
+ when 'd' then amount
+ when 'm' then amount * 30
+ end
+ end
+ end
+
+ class Note
+ attr_reader :header
+ attr_reader :file_name
+ attr_reader :tags
+ attr_reader :period
+
+ def initialize(header, file_name, *tags)
+ @header = header
+ @file_name = file_name
+ @tags = tags
+ @status = :topostpone
+ @body = ''
+ @notes = []
+ end
+
+ def scheduled(date = nil)
+ return @scheduled unless date
+
+ split = date.split(' ')
+ @period = split[1] if split.size > 1
+ @scheduled = Date.new(split[0])
+ end
+
+ def status(status = nil)
+ return @status unless status
+ @status = status
+ end
+
+ def body(body = nil)
+ return @body unless body
+ @body = body
+ end
+
+ def note(header, *tags, &block)
+ @notes << Note.new(header, @name, *tags)
+ @notes.last.instance_eval &block
+ end
+ end
+
+ class File
+ attr_reader :name
+ attr_reader :notes
+
+ def initialize(name)
+ @name = name
+ @notes = []
+ end
+
+ def note(header, *tags, &block)
+ @notes << Note.new(header, @name, *tags)
+ @notes.last.instance_eval &block
+ end
+
+ # def daily_agenda(target_date)
+ # agenda = []
+ # agenda_iteration(@notes, agenda, target_date)
+
+ # return Class.new do
+ # def note
+ # agenda
+ # end
+ # end
+ # end
+
+ # def agenda_iteration(notes, agenda, target_date)
+ # notes.each do |note|
+ # agenda << note if note.scheduled == target_date
+
+ # loop
+ # periodic = note.scheduled.add(note.period) if note.period
+ # break unless periodic
+ # break if periodic > target_date
+ # if periodic == target_date
+ # agenda << note
+ # end
+ # end
+
+ # agenda_iteration(note.notes, agenda, target_date)
+ # end
+ # end
+
+ end
+end