class LearningDiary
collection class for paragraphs synonym 'Topic' 'Learning Diary'
Attributes
The credit arrays are arrays of elements of type [start_time, end_time, duration], one for each tag in all the paragraphs that earns credit
Paragraphs is an array representing the parsed DSL from one file
The credit arrays are arrays of elements of type [start_time, end_time, duration], one for each tag in all the paragraphs that earns credit
Public Class Methods
@param [String] file name of a text file containing text in docfolio DSL
# File lib/docfolio/learning_diary.rb, line 18 def initialize(file) initialize_vars parse(file) calculate_credits end
Public Instance Methods
Implements [] by indexing the @paragraphs instance variable
# File lib/docfolio/learning_diary.rb, line 31 def [](index) @paragraphs[index] end
@return [Integer] The sum of the impact and standard credits in minutes
# File lib/docfolio/learning_diary.rb, line 46 def credits_total impact_credits_total + standard_credits_total end
Implements Enuemerable module by iterating through each paragraph in the learning diary
# File lib/docfolio/learning_diary.rb, line 26 def each(&block) @paragraphs.each { |p| block.call(p) } end
@return [Integer] The sum of the impact credits in minutes
# File lib/docfolio/learning_diary.rb, line 41 def impact_credits_total impact_credits.reduce(0) { |a, e| a + e[2] } end
# File lib/docfolio/learning_diary.rb, line 50 def significant_event? paragraphs.each { |p| return true if p.significant_event? } false end
@return [Integer] The sum of the standard credits in minutes
# File lib/docfolio/learning_diary.rb, line 36 def standard_credits_total standard_credits.reduce(0) { |a, e| a + e[2] } end
Private Instance Methods
if the prev/current times are of the form:
19:00-nil [[:LP, “By entering information 19:35-nil
then make it…
19:00-19:35 [[:LP, “By entering information 19:35-nil
# File lib/docfolio/learning_diary.rb, line 113 def assume_last_end_time_from_st_time(previous_paragraph, current_paragraph) if ( previous_paragraph.end_time.nil? ) && ( ! current_paragraph.start_time.nil? ) && ( ! previous_paragraph.start_time.nil? ) && ( previous_paragraph.start_time < current_paragraph.start_time ) previous_paragraph.end_time = current_paragraph.start_time end end
@param [Symbol] is_creditable Name of the function to call on the
paragraphs to assertain if the paragraph times are to be counted towards a particular type of credit
@param [Array] credits Array of elements of type
[start_time, end_time, duration], one for each tag in all the paragraphs that earns standard credit
@return [Array] Array of elements of type
[start_time, end_time, duration], one for each tag in all the paragraphs that earns credit of type credits
# File lib/docfolio/learning_diary.rb, line 137 def calc_credits(is_creditable, credits) @paragraphs.each do |p| next unless p.public_send(is_creditable) start = p.start_time finish = p.end_time duration = p.duration credits << [start, finish, duration] unless duration == 0 end credits.uniq! end
@return [Array] Array of elements of type
[start_time, end_time, duration], one for each tag in all the paragraphs that earns impact credit
# File lib/docfolio/learning_diary.rb, line 161 def calc_impact_credits calc_credits(:impact_creditable?, impact_credits) end
@todo Deal with edge case where the start and end times of different elements overlap to avoid claiming credit for the same moments in time more than once @return [Array] Array of elements of type
[start_time, end_time, duration], one for each tag in all the paragraphs that earns standard credit
# File lib/docfolio/learning_diary.rb, line 154 def calc_standard_credits calc_credits(:creditable?, standard_credits) end
# File lib/docfolio/learning_diary.rb, line 65 def calculate_credits calc_standard_credits calc_impact_credits end
# File lib/docfolio/learning_diary.rb, line 57 def initialize_vars Paragraph.reset @console_view = DiaryConsoleView.new @paragraphs = [] @standard_credits = [] @impact_credits = [] end
if the prev/current times are of the form:
19:00-nil [[:LP, “By entering information 19:00-19:35
then make it…
19:00-19:35 [[:LP, “By entering information 19:35-nil
# File lib/docfolio/learning_diary.rb, line 94 def move_end_time_back(previous_paragraph, current_paragraph) if previous_paragraph.end_time.nil? && ( ! current_paragraph.end_time.nil? ) && ( previous_paragraph.start_time == current_paragraph.start_time ) previous_paragraph.end_time = current_paragraph.end_time current_paragraph.start_time = current_paragraph.end_time current_paragraph.end_time = nil end end
# File lib/docfolio/learning_diary.rb, line 70 def parse(file) # read the whole txt file in one go f = File.read(file, encoding: 'UTF-8') # iterates through each paragraph, extracting tagged content and time info f.split(/\n/).each do |p| p.strip! next if p == '' # ignore if paragraph empty previous_paragraph = @paragraphs.last current_paragraph = Paragraph.new(p) @paragraphs << current_paragraph post_process(previous_paragraph, current_paragraph) end end
# File lib/docfolio/learning_diary.rb, line 122 def post_process(previous_paragraph, current_paragraph) return if previous_paragraph.nil? move_end_time_back(previous_paragraph, current_paragraph) assume_last_end_time_from_st_time(previous_paragraph, current_paragraph) end