Class: Obst::Notes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/obst/notes.rb

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Notes

Returns a new instance of Notes.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/obst/notes.rb', line 7

def initialize(**opts)
  location = opts[:C] || '.'

  @notes = Enumerator.new do |enum|
    note_klass = Struct.new(:name, :dates, :tags, :refs)
    files = Dir.glob(File.join(location, '*.md')).map{|n| File.basename(n)}.to_set
    file_dates = Hash.new{ |h, k| h[k] = [] }

    GroupByDays.new(**opts).each do |log|
      log.file_changes.each do |name, op|
        file_dates[name] << log.time if files.include?(name)
      end
    end

    file_dates.each_pair do |file, dates|
      lines = File.foreach(File.join(location, file))
      tags = lines.first&.scan(/#(\w+)/)&.flatten || []
      refs = lines.each_with_object([]) do |line, arr|
        line.scan(/\[\[(.+?)\]\]/).flatten.each do |n|
          arr << n if files.include?("#{n}.md")
        end
      end
      enum.yield(note_klass.new(file, dates, tags, refs))
    end
  end
end

Instance Method Details

#each(&block) ⇒ Object



34
35
36
# File 'lib/obst/notes.rb', line 34

def each(&block)
  @notes.each(&block)
end