lib/signore/signature.rb in signore-0.4.2 vs lib/signore/signature.rb in signore-0.5.0

- old
+ new

@@ -1,21 +1,28 @@ +# frozen_string_literal: true + require 'lovely_rufus' module Signore Signature = Struct.new(*%i(text author source subject tags)) do - def initialize(author: nil, source: nil, subject: nil, tags: nil, text: nil) + def initialize(author: '', source: '', subject: '', tags: [], text: '') super text, author, source, subject, tags - each_pair { |key, value| self[key] = nil if value and value.empty? } end + def empty? + to_s.empty? + end + undef text if defined?(:text) def text self[:text] or '' end def to_h - super.map { |key, val| [key.to_s, val] }.to_h.keep_if { |_, value| value } + super.map { |key, val| [key.to_s, val] }.to_h.keep_if do |_, value| + value and not value.empty? + end end def to_s spaced = text.gsub("\n", "\n\n") wrapped = LovelyRufus.wrap(spaced, width: 80) @@ -25,15 +32,15 @@ private def indent_size_for(text) indent = text.split("\n").map(&:size).max - meta.size - 2 - indent < 0 ? 0 : indent + indent.negative? ? 0 : indent end def meta - stem = [author, subject].compact.join(' ') - stem.empty? ? "#{source}" : [stem, source].compact.join(', ') + stem = [author, subject].reject(&:empty?).join(' ') + [stem, source].reject(&:empty?).join(', ') end def meta_for(text) meta.empty? ? '' : "\n#{' ' * indent_size_for(text)}[#{meta}]" end