Method: Faun::ForumThread#initialize

Defined in:
lib/faun.rb

#initialize(id, name, path) ⇒ ForumThread

Returns a new instance of ForumThread.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/faun.rb', line 183

def initialize(id, name, path)
  @id = id
  @name = name

  comments = {}
  Dir.each_child(path) do |filename|
    file = File.join(path, filename)
    next if File.directory?(file) or filename.start_with?('.') or not filename.end_with?('.md')

    id, author, date_string = filename.scan(/^(\d{3})\.(\w+)\.(20\d\d-\d\d-\d\d\.\d\d-\d\d)\.md$/).first
    id = id.to_i
    datetime = DateTime.strptime(date_string, "%Y-%m-%d.%H-%M")

    content = nil
    Async do
      begin
        File.open(file, "r:UTF-8") do |file|
          generic = Async::IO::Stream.new(file)
          content = generic.read.force_encoding("UTF-8")
        end
      rescue
        content = ""
      end
    end.wait

    comments[id] = Comment.new(id, author, datetime, content)
  end
  @items = comments.sort_by { |id, _| id }.to_h
end