lib/signore/signature.rb in signore-0.1.2 vs lib/signore/signature.rb in signore-0.2.0

- old
+ new

@@ -1,34 +1,39 @@ -module Signore class Signature < Struct.new :text, :author, :source, :subject, :tags +module Signore Signature = Struct.new(*%i[text author source subject tags]) do + class << self + undef :[] + end + def self.[](author: nil, source: nil, subject: nil, tags: nil, text: nil) + new text, author, source, subject, tags + end + def tagged_with? tag tags and tags.include? tag end def to_s - wrapped = LovelyRufus::Wrapper.new(text.gsub("\n", "\n\n")).wrapped(80).gsub("\n\n", "\n") - wrapped + meta_for(wrapped) + wrapped = LovelyRufus::TextWrapper.wrap text.gsub("\n", "\n\n"), width: 80 + squeezed = wrapped.gsub("\n\n", "\n").chomp + squeezed + meta_for(squeezed) end private + def indent_size_for text + indent = text_width(text) - meta.size - 2 + indent < 0 ? 0 : indent + end + def meta - case - when author && subject && source then "#{author} #{subject}, #{source}" - when author && subject then "#{author} #{subject}" - when author && source then "#{author}, #{source}" - when author then "#{author}" - when source then "#{source}" - when subject then "#{subject}" - end + stem = [author, subject].compact.join ' ' + stem.empty? ? "#{source}" : [stem, source].compact.join(', ') end - def meta_for wrapped - return '' unless meta - - indent = wrapped.split("\n").map(&:size).max - meta.size - 2 - spaces = indent > 0 ? ' ' * indent : '' - - "\n#{spaces}[#{meta}]" + def meta_for text + meta.empty? ? '' : "\n#{' ' * indent_size_for(text)}[#{meta}]" end + def text_width text + text.split("\n").map(&:size).max + end end end