Решение на Седма задача от Десислава Цветкова

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

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

Резултати

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

Код

class LazyMode
class Date
include Comparable
attr_accessor :year, :month, :day
def initialize(date)
@year, @month, @day = date.split('-').map(&:to_i)
end
def add_zeroes(date, number)
"0" * (number - date.to_s.length) + date.to_s
end
def to_s
"#{add_zeroes(@year, 4)}-#{add_zeroes(@month, 2)}-#{add_zeroes(@day, 2)}"
end
def +(other_date)
new_date = Date.new("0-0-0")
new_date.day += @day + other_date.day
new_date.month += @month + other_date.month
new_date.year += @year + other_date.year
new_date.normalize_date
new_date
end
def <=>(other_date)
comparison = @year <=> other_date.year
return comparison if comparison != 0
comparison = @month <=> other_date.month
return comparison if comparison != 0
@day <=> other_date.day
end
def ==(other_date)
(self <=> other_date) == 0
end
def normalize_date
overflow, @day = normalize(@day, 30)
@month += overflow
overflow, @month = normalize(@month, 12)
@year += overflow
self
end
def normalize(d_m_y, size)
overflow = d_m_y == size ? 0 : d_m_y / size
d_m_y = d_m_y % size if d_m_y != size
[overflow, d_m_y]
end
end
class ScheduleNote
attr_accessor :interval, :date
include Enumerable
SPANS = {d:1, w:7, m:30}
def initialize(date, interval = nil)
@date = Date.new(date)
@interval = make_interval(interval)
end
def next
@date += @interval
end
def make_interval(interval)
return nil if interval.nil?
days = SPANS[interval[-1].to_sym] * interval[0...-1].to_i
date = Date.new("0-0-#{days}").normalize_date
end
end
class Note
attr_accessor :file_name, :sub_notes, :tags, :schedule_note
attr_reader :header
def initialize(header, *tags)
@header = header
@tags = *tags
@file_name = ""
@body = ""
@status = :topostpone
@sub_notes = []
end
def date
@schedule_note.date
end
def body(text_body = nil)
if text_body.nil?
@body
else
@body = text_body
end
end
def status(symbol_status = nil)
if symbol_status.nil?
@status
else
@status = symbol_status
end
end
def scheduled(schedule)
@schedule_note = ScheduleNote.new(*schedule.split(" "))
end
def note(header, *tags, &block)
new_note = Note.new(header, *tags)
@sub_notes << new_note
new_note.instance_eval(&block)
end
def to_s
"#{@header}-#{@tags}-#{file_name} #{@body}, #{@status},
#{schedule_note.date}"
end
end
class File
attr_reader :notes, :name
def initialize(name, notes)
@name = name
@notes = []
iterate_notes(notes)
end
def iterate_notes(notes)
notes.each do |note|
sub_notes = note.sub_notes
note.sub_notes = []
note.file_name = @name
@notes << note
iterate_notes(sub_notes)
end
end
def daily_agenda(date)
Agenda.new(:day, date, @notes)
end
def weekly_agenda(date)
Agenda.new(:week, date, @notes)
end
end
class Agenda
attr_reader :notes
TYPES = {day: Date.new("0-0-0"),
week: Date.new("0-0-7"),
month: Date.new("0-1-0")}
def initialize(type, beginning_date, notes)
@type = type
@range = [beginning_date, beginning_date + TYPES[type]]
@notes = []
@notes = select_notes(notes)
end
def select_notes(notes)
notes.each do |note|
add_and_iterate_note(note)
end
@notes.select do |note|
date = note.schedule_note.date
date >= @range.first && date <= @range.last
end
end
def add_and_iterate_note(note)
return if note.schedule_note.nil?
if note.schedule_note.interval.nil?
@notes << note
elsif not note.schedule_note.interval.nil?
iterate_note(note)
end
end
def iterate_note(note)
date = note.schedule_note.date
while date <= @range.last
@notes << Note.new(note.header, note.tags)
@notes.last.schedule_note = ScheduleNote.new(date.to_s)
@notes.last.schedule_note.interval = note.schedule_note.interval
date = @notes.last.schedule_note.next
end
end
def where(tag: nil, status: nil, text: nil)
Agenda.new(@type, @range.first, @notes).select_tag(tag)
.select_status(status)
.select_text(text)
end
def select_tag(tag)
return self if tag.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_status(status)
return self if status.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_text(text)
return self if text.nil?
@notes.select! do |note|
text.match(note.header) or text.match(note.body)
end
self
end
end
class << self
def create_file(name, &block)
new_note = Note.new("")
new_note.instance_eval(&block)
new_file = File.new(name, new_note.sub_notes)
end
end
end

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

.............FFF..FFFF.....FF..

Failures:

  1) LazyMode#daily_agenda returns note scheduled with daily repeater
     Failure/Error: expect(note.tags).to eq([])
       
       expected: []
            got: [[]]
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/spec.rb:157: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: expect(note.tags).to eq([])
       
       expected: []
            got: [[]]
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/spec.rb:179: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(note.tags).to eq([])
       
       expected: []
            got: [[]]
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/spec.rb:202: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: expect(agenda.notes.size).to eq(1)
       
       expected: 1
            got: 2
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/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)>'

  5) LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
     Failure/Error: expect(note.tags).to eq([])
       
       expected: []
            got: [[]]
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/spec.rb:275:in `block (4 levels) in <top (required)>'
     # /tmp/d20160107-5693-vtp95w/spec.rb:272:in `each'
     # /tmp/d20160107-5693-vtp95w/spec.rb:272: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: expect(agenda.notes.size).to eq(2)
       
       expected: 2
            got: 1
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/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)>'

  7) LazyMode#weekly_agenda returns note scheduled with monthly repeater
     Failure/Error: expect(note.tags).to eq([])
       
       expected: []
            got: [[]]
       
       (compared using ==)
     # /tmp/d20160107-5693-vtp95w/spec.rb:331: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#where filters by status
     Failure/Error: notes = @agenda.where(status: :postponed).notes
     NameError:
       undefined local variable or method `tag' for #<LazyMode::Agenda:0x007fd64b4f1d60>
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `block in select_status'
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `select!'
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `select_status'
     # /tmp/d20160107-5693-vtp95w/solution.rb:199:in `where'
     # /tmp/d20160107-5693-vtp95w/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)>'

  9) LazyMode#where filters by multiple filters
     Failure/Error: notes = @agenda.where(text: /important/, status: :postponed).notes
     NameError:
       undefined local variable or method `tag' for #<LazyMode::Agenda:0x007fd64b4ee250>
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `block in select_status'
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `select!'
     # /tmp/d20160107-5693-vtp95w/solution.rb:211:in `select_status'
     # /tmp/d20160107-5693-vtp95w/solution.rb:199:in `where'
     # /tmp/d20160107-5693-vtp95w/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.02157 seconds
31 examples, 9 failures

Failed examples:

rspec /tmp/d20160107-5693-vtp95w/spec.rb:141 # LazyMode#daily_agenda returns note scheduled with daily repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:163 # LazyMode#daily_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:186 # LazyMode#daily_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:237 # LazyMode#weekly_agenda returns note scheduled without repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:259 # LazyMode#weekly_agenda returns multiple notes with different dates when scheduled with daily repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:283 # LazyMode#weekly_agenda returns note scheduled with weekly repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:315 # LazyMode#weekly_agenda returns note scheduled with monthly repeater
rspec /tmp/d20160107-5693-vtp95w/spec.rb:432 # LazyMode#where filters by status
rspec /tmp/d20160107-5693-vtp95w/spec.rb:443 # LazyMode#where filters by multiple filters

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

Десислава обнови решението на 20.12.2015 00:23 (преди над 8 години)

+class LazyMode
+
+ class Date
+ include Comparable
+ attr_accessor :year, :month, :day
+
+ def initialize(date)
+ @year, @month, @day = date.split('-').map(&:to_i)
+ end
+
+ def add_zeroes(date, number)
+ "0" * (number - date.to_s.length) + date.to_s
+ end
+
+ def to_s
+ "#{add_zeroes(@year, 4)}-#{add_zeroes(@month, 2)}-#{add_zeroes(@day, 2)}"
+ end
+
+ def +(other_date)
+ new_date = Date.new("0-0-0")
+ new_date.day += @day + other_date.day
+ new_date.month += @month + other_date.month
+ new_date.year += @year + other_date.year
+ new_date.normalize_date
+ new_date
+ end
+
+ def <=>(other_date)
+ comparison = @year <=> other_date.year
+ return comparison if comparison != 0
+ comparison = @month <=> other_date.month
+ return comparison if comparison != 0
+ @day <=> other_date.day
+ end
+
+ def ==(other_date)
+ (self <=> other_date) == 0
+ end
+
+ def normalize_date
+ overflow, @day = normalize(@day, 30)
+ @month += overflow
+ overflow, @month = normalize(@month, 12)
+ @year += overflow
+ self
+ end
+
+ def normalize(unit, number)
+ overflow = unit == number ? unit : unit / number
+ unit = unit % number if unit != number
+ [overflow, unit]
+ end
+ end
+
+ class ScheduleNote
+ attr_accessor :interval, :date
+ include Enumerable
+ SPANS = {d:1, w:7, m:20}
+
+ def initialize(date, interval = nil)
+ @date = Date.new(date)
+ @interval = make_interval(interval)
+ end
+
+ def next
+ @date += @interval
+ end
+
+ def make_interval(interval)
+ return nil if interval.nil?
+ days = SPANS[interval[-1].to_sym] * interval[0...-1].to_i
+ date = Date.new("0-0-#{days}").normalize_date
+ end
+ end
+
+ class Note
+ attr_accessor :file_name, :sub_notes, :tags, :schedule_note
+ attr_reader :header
+
+ def initialize(header, *tags)
+ @header = header
+ @tags = *tags
+ @file_name = ""
+ @body = ""
+ @status = :topostpone
+ @sub_notes = []
+ end
+
+ def date
+ @schedule_note.date
+ end
+
+ def body(text_body = nil)
+ if text_body.nil?
+ @body
+ else
+ @body = text_body
+ end
+ end
+
+ def status(symbol_status = nil)
+ if symbol_status.nil?
+ @status
+ else
+ @status = symbol_status
+ end
+ end
+
+ def scheduled(schedule)
+ @schedule_note = ScheduleNote.new(*schedule.split(" "))
+ end
+
+
+ def note(header, *tags, &block)
+ new_note = Note.new(header, *tags)
+ @sub_notes << new_note
+ new_note.instance_eval(&block)
+ end
+
+ def to_s
+ "#{@header}-#{@tags}-#{file_name} #{@body}, #{@status},
+ #{schedule_note.date}"
+ end
+ end
+
+ class File
+ attr_reader :notes, :name
+ def initialize(name, notes)
+ @name = name
+ @notes = []
+ iterate_notes(notes)
+ end
+
+ def iterate_notes(notes)
+ notes.each do |note|
+ sub_notes = note.sub_notes
+ note.sub_notes = []
+ note.file_name = @name
+ @notes << note
+ iterate_notes(sub_notes)
+ end
+ end
+
+ def daily_agenda(date)
+ Agenda.new(:day, date, @notes)
+ end
+
+ def weekly_agenda(date)
+ Agenda.new(:week, date, @notes)
+ end
+ end
+
+ class Agenda
+ attr_reader :notes
+ TYPES = {day: Date.new("0-0-0"),
+ week: Date.new("0-0-7"),
+ month: Date.new("0-1-0")}
+
+ def initialize(type, beginning_date, notes)
+ @type = type
+ @range = [beginning_date, beginning_date + TYPES[type]]
+ @notes = []
+ @notes = select_notes(notes)
+ end
+
+ def select_notes(notes)
+ notes.each do |note|
+ add_and_iterate_note(note)
+ end
+ @notes.select do |note|
+ date = note.schedule_note.date
+ date >= @range.first && date <= @range.last
+ end
+ end
+
+ def add_and_iterate_note(note)
+ if note.schedule_note.interval.nil?
+ @notes << note
+ elsif not note.schedule_note.interval.nil?
+ iterate_note(note)
+ end
+ end
+
+ def iterate_note(note)
+ date = note.schedule_note.date
+ while date <= @range.last
+ @notes << Note.new(note.header, note.tags)
+ @notes.last.schedule_note = ScheduleNote.new(date.to_s)
+ @notes.last.schedule_note.interval = note.schedule_note.interval
+ date = @notes.last.schedule_note.next
+ end
+ end
+
+ def where(tag: nil, status: nil, text: nil)
+ Agenda.new(@type, @range.first, @notes).select_tag(tag)
+ .select_status(status)
+ .select_text(text)
+ end
+
+ def select_tag(tag)
+ return self if tag.nil?
+ @notes.select!{ |note| note.tags.include?(tag) }
+ self
+ end
+
+ def select_status(status)
+ return self if status.nil?
+ @notes.select!{ |note| note.tags.include?(tag) }
+ self
+ end
+
+ def select_text(text)
+ return self if text.nil?
+ @notes.select! do |note|
+ text.match(note.header) or text.match(note.body)
+ end
+ self
+ end
+ end
+
+ class << self
+ def create_file(name, &block)
+ new_note = Note.new("")
+ new_note.instance_eval(&block)
+ new_file = File.new(name, new_note.sub_notes)
+ end
+ end
+end

Десислава обнови решението на 21.12.2015 14:40 (преди над 8 години)

class LazyMode
class Date
include Comparable
attr_accessor :year, :month, :day
def initialize(date)
@year, @month, @day = date.split('-').map(&:to_i)
end
def add_zeroes(date, number)
"0" * (number - date.to_s.length) + date.to_s
end
def to_s
"#{add_zeroes(@year, 4)}-#{add_zeroes(@month, 2)}-#{add_zeroes(@day, 2)}"
end
def +(other_date)
new_date = Date.new("0-0-0")
new_date.day += @day + other_date.day
new_date.month += @month + other_date.month
+
new_date.year += @year + other_date.year
new_date.normalize_date
+
new_date
end
def <=>(other_date)
comparison = @year <=> other_date.year
return comparison if comparison != 0
comparison = @month <=> other_date.month
return comparison if comparison != 0
@day <=> other_date.day
end
def ==(other_date)
(self <=> other_date) == 0
end
def normalize_date
overflow, @day = normalize(@day, 30)
@month += overflow
overflow, @month = normalize(@month, 12)
@year += overflow
self
end
- def normalize(unit, number)
- overflow = unit == number ? unit : unit / number
- unit = unit % number if unit != number
- [overflow, unit]
+ def normalize(d_m_y, size)
+ overflow = d_m_y == size ? 0 : d_m_y / size
+ d_m_y = d_m_y % size if d_m_y != size
+ [overflow, d_m_y]
end
end
class ScheduleNote
attr_accessor :interval, :date
include Enumerable
- SPANS = {d:1, w:7, m:20}
+ SPANS = {d:1, w:7, m:30}
def initialize(date, interval = nil)
@date = Date.new(date)
@interval = make_interval(interval)
end
def next
@date += @interval
end
def make_interval(interval)
return nil if interval.nil?
days = SPANS[interval[-1].to_sym] * interval[0...-1].to_i
date = Date.new("0-0-#{days}").normalize_date
end
end
class Note
attr_accessor :file_name, :sub_notes, :tags, :schedule_note
attr_reader :header
def initialize(header, *tags)
@header = header
@tags = *tags
@file_name = ""
@body = ""
@status = :topostpone
@sub_notes = []
end
def date
@schedule_note.date
end
def body(text_body = nil)
if text_body.nil?
@body
else
@body = text_body
end
end
def status(symbol_status = nil)
if symbol_status.nil?
@status
else
@status = symbol_status
end
end
def scheduled(schedule)
@schedule_note = ScheduleNote.new(*schedule.split(" "))
end
def note(header, *tags, &block)
new_note = Note.new(header, *tags)
@sub_notes << new_note
new_note.instance_eval(&block)
end
def to_s
"#{@header}-#{@tags}-#{file_name} #{@body}, #{@status},
#{schedule_note.date}"
end
end
class File
attr_reader :notes, :name
def initialize(name, notes)
@name = name
@notes = []
iterate_notes(notes)
end
def iterate_notes(notes)
notes.each do |note|
sub_notes = note.sub_notes
note.sub_notes = []
note.file_name = @name
@notes << note
iterate_notes(sub_notes)
end
end
def daily_agenda(date)
Agenda.new(:day, date, @notes)
end
def weekly_agenda(date)
Agenda.new(:week, date, @notes)
end
end
class Agenda
attr_reader :notes
TYPES = {day: Date.new("0-0-0"),
week: Date.new("0-0-7"),
month: Date.new("0-1-0")}
def initialize(type, beginning_date, notes)
@type = type
@range = [beginning_date, beginning_date + TYPES[type]]
@notes = []
@notes = select_notes(notes)
end
def select_notes(notes)
notes.each do |note|
add_and_iterate_note(note)
end
@notes.select do |note|
date = note.schedule_note.date
date >= @range.first && date <= @range.last
end
end
def add_and_iterate_note(note)
if note.schedule_note.interval.nil?
@notes << note
elsif not note.schedule_note.interval.nil?
iterate_note(note)
end
end
def iterate_note(note)
date = note.schedule_note.date
while date <= @range.last
@notes << Note.new(note.header, note.tags)
@notes.last.schedule_note = ScheduleNote.new(date.to_s)
@notes.last.schedule_note.interval = note.schedule_note.interval
date = @notes.last.schedule_note.next
end
end
def where(tag: nil, status: nil, text: nil)
Agenda.new(@type, @range.first, @notes).select_tag(tag)
.select_status(status)
.select_text(text)
end
def select_tag(tag)
return self if tag.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_status(status)
return self if status.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_text(text)
return self if text.nil?
@notes.select! do |note|
text.match(note.header) or text.match(note.body)
end
self
end
end
class << self
def create_file(name, &block)
new_note = Note.new("")
new_note.instance_eval(&block)
new_file = File.new(name, new_note.sub_notes)
end
end
-end
+end

Десислава обнови решението на 21.12.2015 15:13 (преди над 8 години)

class LazyMode
class Date
include Comparable
attr_accessor :year, :month, :day
def initialize(date)
@year, @month, @day = date.split('-').map(&:to_i)
end
def add_zeroes(date, number)
"0" * (number - date.to_s.length) + date.to_s
end
def to_s
"#{add_zeroes(@year, 4)}-#{add_zeroes(@month, 2)}-#{add_zeroes(@day, 2)}"
end
def +(other_date)
new_date = Date.new("0-0-0")
new_date.day += @day + other_date.day
new_date.month += @month + other_date.month
new_date.year += @year + other_date.year
new_date.normalize_date
new_date
end
def <=>(other_date)
comparison = @year <=> other_date.year
return comparison if comparison != 0
comparison = @month <=> other_date.month
return comparison if comparison != 0
@day <=> other_date.day
end
def ==(other_date)
(self <=> other_date) == 0
end
def normalize_date
overflow, @day = normalize(@day, 30)
@month += overflow
overflow, @month = normalize(@month, 12)
@year += overflow
self
end
def normalize(d_m_y, size)
overflow = d_m_y == size ? 0 : d_m_y / size
d_m_y = d_m_y % size if d_m_y != size
[overflow, d_m_y]
end
end
class ScheduleNote
attr_accessor :interval, :date
include Enumerable
SPANS = {d:1, w:7, m:30}
def initialize(date, interval = nil)
@date = Date.new(date)
@interval = make_interval(interval)
end
def next
@date += @interval
end
def make_interval(interval)
return nil if interval.nil?
days = SPANS[interval[-1].to_sym] * interval[0...-1].to_i
date = Date.new("0-0-#{days}").normalize_date
end
end
class Note
attr_accessor :file_name, :sub_notes, :tags, :schedule_note
attr_reader :header
def initialize(header, *tags)
@header = header
@tags = *tags
@file_name = ""
@body = ""
@status = :topostpone
@sub_notes = []
end
def date
@schedule_note.date
end
def body(text_body = nil)
if text_body.nil?
@body
else
@body = text_body
end
end
def status(symbol_status = nil)
if symbol_status.nil?
@status
else
@status = symbol_status
end
end
def scheduled(schedule)
@schedule_note = ScheduleNote.new(*schedule.split(" "))
end
def note(header, *tags, &block)
new_note = Note.new(header, *tags)
@sub_notes << new_note
new_note.instance_eval(&block)
end
def to_s
"#{@header}-#{@tags}-#{file_name} #{@body}, #{@status},
#{schedule_note.date}"
end
end
class File
attr_reader :notes, :name
def initialize(name, notes)
@name = name
@notes = []
iterate_notes(notes)
end
def iterate_notes(notes)
notes.each do |note|
sub_notes = note.sub_notes
note.sub_notes = []
note.file_name = @name
@notes << note
iterate_notes(sub_notes)
end
end
def daily_agenda(date)
Agenda.new(:day, date, @notes)
end
def weekly_agenda(date)
Agenda.new(:week, date, @notes)
end
end
class Agenda
attr_reader :notes
TYPES = {day: Date.new("0-0-0"),
week: Date.new("0-0-7"),
month: Date.new("0-1-0")}
def initialize(type, beginning_date, notes)
@type = type
@range = [beginning_date, beginning_date + TYPES[type]]
@notes = []
@notes = select_notes(notes)
end
def select_notes(notes)
notes.each do |note|
add_and_iterate_note(note)
end
@notes.select do |note|
date = note.schedule_note.date
date >= @range.first && date <= @range.last
end
end
def add_and_iterate_note(note)
+ return if note.schedule_note.nil?
if note.schedule_note.interval.nil?
@notes << note
elsif not note.schedule_note.interval.nil?
iterate_note(note)
end
end
def iterate_note(note)
date = note.schedule_note.date
while date <= @range.last
@notes << Note.new(note.header, note.tags)
@notes.last.schedule_note = ScheduleNote.new(date.to_s)
@notes.last.schedule_note.interval = note.schedule_note.interval
date = @notes.last.schedule_note.next
end
end
def where(tag: nil, status: nil, text: nil)
Agenda.new(@type, @range.first, @notes).select_tag(tag)
.select_status(status)
.select_text(text)
end
def select_tag(tag)
return self if tag.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_status(status)
return self if status.nil?
@notes.select!{ |note| note.tags.include?(tag) }
self
end
def select_text(text)
return self if text.nil?
@notes.select! do |note|
text.match(note.header) or text.match(note.body)
end
self
end
end
class << self
def create_file(name, &block)
new_note = Note.new("")
new_note.instance_eval(&block)
new_file = File.new(name, new_note.sub_notes)
end
end
-end
+end