Class: Paragraph

Inherits:
Object
  • Object
show all
Includes:
Enumerable, MyTime, Tags
Defined in:
lib/docfolio/paragraph.rb

Overview

Used by LearningDiary Initialized with plain text, will parse and hold tagged content Can keep track of time, section and id information from previous paragraphs using class instance variables

Constant Summary

Constants included from Tags

Tags::ALL_TAGS, Tags::CREDITABLE, Tags::SECTIONS, Tags::SPECIAL, Tags::TAGS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tags

#extract_date, #extract_tags, #extract_time_object

Methods included from MyTime

#process_times

Constructor Details

#initialize(p) ⇒ Paragraph

Returns a new instance of Paragraph.

Parameters:

  • p (String)

    a single paragraph from a text file.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/docfolio/paragraph.rb', line 119

def initialize(p)
  # preparation
  initialize_vars

  # Extract the date and time from a paragraph if it contains date and time 
  # info. Removes the date and time from the paragraph puts whats left into 
  # rest_of_str. Puts the to hour, to min, from hour and from min into the 
  # time array. Puts the date into Paragraph.date as a Time object.
  #
  # Paragraph.date is a class instance variable that holds the date to apply
  # to this and subsequent paragraphs. It is initialized to nil when the 
  # program starts and reset to nil when reset is called (which it is called 
  # by the LearningDiary when initializing to parse a new file, called by
  # the Collater when iterating through each text file)
  #
  # The extract_date function is from the Tag module
  rest_of_str, time_array, Paragraph.date = extract_date(p, Paragraph.date)

  # if a date or time has been found (and extracted)
  if rest_of_str != p
    # transer class start and end times to those of this paragraph, reset 
    # section to :NOTE
    note_time

    # Takes the current class instance times and dates and newly extracted 
    # paragraph dates from this paragraph, follows a set of rules to 
    # determine what the class instant times and dates should become
    assign_class_dates process_times(time_array, class_dates)
    
    # tranfser class start and end times to those of this paragraph, reset 
    # section to :NOTE
    note_time
  end

  # if a new date or time has not been found then return
  # @todo should this be in an else statement? 
  return if rest_of_str == ''

  tags_extracted?(rest_of_str) ? note_time : tag_section(rest_of_str)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(n, *args, &block) ⇒ Object (private)



252
253
254
255
256
257
258
# File 'lib/docfolio/paragraph.rb', line 252

def method_missing(n, *args, &block)
  if args[0].nil? # tag getter
    ALL_TAGS.include?(n) ? content(n) : super(n, *args, &block)
  else # section setter
    SECTIONS.include?(n) ? tag_it(n, args[0]) : super(n, *args, &block)
  end
end

Class Attribute Details

.dateObject

class starttime and class end time



115
116
117
# File 'lib/docfolio/paragraph.rb', line 115

def date
  @date
end

.etObject

class starttime and class end time



115
116
117
# File 'lib/docfolio/paragraph.rb', line 115

def et
  @et
end

.idObject

class starttime and class end time



115
116
117
# File 'lib/docfolio/paragraph.rb', line 115

def id
  @id
end

.sectionObject

class starttime and class end time



115
116
117
# File 'lib/docfolio/paragraph.rb', line 115

def section
  @section
end

.stObject

class starttime and class end time



115
116
117
# File 'lib/docfolio/paragraph.rb', line 115

def st
  @st
end

Instance Attribute Details

#end_timeObject

instance start time and instance end time



107
108
109
# File 'lib/docfolio/paragraph.rb', line 107

def end_time
  @end_time
end

#idObject (readonly)

Returns the value of attribute id.



104
105
106
# File 'lib/docfolio/paragraph.rb', line 104

def id
  @id
end

#start_timeObject

instance start time and instance end time



107
108
109
# File 'lib/docfolio/paragraph.rb', line 107

def start_time
  @start_time
end

#tagsObject (readonly)

Returns the value of attribute tags.



104
105
106
# File 'lib/docfolio/paragraph.rb', line 104

def tags
  @tags
end

Class Method Details

.resetObject

resets the class variables so that a new file can be parsed is called by LearningDiary when preparing to parse a new txt file



169
170
171
172
# File 'lib/docfolio/paragraph.rb', line 169

def self.reset
  Paragraph.date = Paragraph.st = Paragraph.et = nil
  Paragraph.section = Paragraph.id = 0 # :TITLE
end

Instance Method Details

#[](index) ⇒ Object



188
189
190
# File 'lib/docfolio/paragraph.rb', line 188

def [](index)
  tags[index]
end

#creditable?Boolean

true is the paragraph contains a tag that can earn credit

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/docfolio/paragraph.rb', line 193

def creditable?
  @tags.each { |t| return true if CREDITABLE.include?(t[0]) }
  false
end

#each(&block) ⇒ Object

each on paragraph iterates through the tags



184
185
186
# File 'lib/docfolio/paragraph.rb', line 184

def each(&block)
  @tags.each { |t| block.call(t) }
end

#impact_creditable?Boolean

true is the paragraph contains a tag that can earn impact credit

Returns:

  • (Boolean)


199
200
201
202
# File 'lib/docfolio/paragraph.rb', line 199

def impact_creditable?
  @tags.each { |t| return true if t[0] == :I }
  false
end

#initialize_varsObject



175
176
177
178
179
180
181
# File 'lib/docfolio/paragraph.rb', line 175

def initialize_vars
  @date_specified = @end_time_specified = @start_time_specified = false
  @start_time = @end_time = nil
  @tags = []
  @id = Paragraph.id
  Paragraph.id += 1
end

#latest_timeObject



209
210
211
212
213
# File 'lib/docfolio/paragraph.rb', line 209

def latest_time
  return @end_time unless @end_time.nil?
  return @start_time unless @start_time.nil?
  nil
end

#periodObject



204
205
206
207
# File 'lib/docfolio/paragraph.rb', line 204

def period
  return 0 if @end_time.nil? || @start_time.nil?
  (@end_time - @start_time).to_i / 60
end

#tag?(tag) ⇒ Boolean

returns true if any tags are of type tag

Parameters:

  • tag (Array)

    An array of tags

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/docfolio/paragraph.rb', line 162

def tag?(tag)
  @tags.each { |t| return true if t[0] == tag }
  false
end