Sha256: 8812430e291ad54063c857494ada1ed4fa63d1f2efed91f7cd26fb4845aaec47
Contents?: true
Size: 1.31 KB
Versions: 1
Compression:
Stored size: 1.31 KB
Contents
module Ncn class Draft attr_reader :ids, :repository def initialize(ids:, repository:) @ids = ids @repository = repository end def open_with dump yield file_name load FileUtils.rm_rf(file_name) end private def dump FileUtils.mkdir_p(File.dirname(file_name)) File.write(file_name, exported_notes) end def load updated_note_ids = [] read_fragments.each do |fragment| id, tags, body = parse_fragment(fragment) updated_note_ids << id repository.update(id, body: body, tags: tags) end (ids - updated_note_ids).each { repository.drop(_1) } end def read_fragments File.read(file_name).split(/^--\s+(?=\d+),?/)[1..] || [] end def parse_fragment(fragment) fragment_tagline, body = fragment.split("\n", 2) id, *tags = fragment_tagline.split(",").map(&:strip) [Integer(id), tags, body] end def file_name @file_name ||= File.join(repository.path, "edit", "#{ids.sort.join("_")}.md") end def exported_notes notes.map { "#{tagline(_1)}\n#{_1.body}" }.join("\n\n") + "\n" end def tagline(note) "-- #{[note.id].concat(note.uniq_tags).join(", ")}".strip end def notes ids.map { repository.find(_1) } end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ncn-0.1.0 | lib/ncn/draft.rb |