lib/signore/signature.rb in signore-0.6.0 vs lib/signore/signature.rb in signore-0.7.0

- old
+ new

@@ -1,32 +1,30 @@ -# frozen_string_literal: true - require 'lovely_rufus' module Signore - Signature = Struct.new(:text, :author, :source, :subject, :tags) do + class Signature < Struct.new(:text, :author, :source, :subject, :tags) + class << self + def from_h(hash) + new(hash.map { |key, value| [key.to_sym, value] }.to_h) + end + end + def initialize(author: '', source: '', subject: '', tags: [], text: '') super text, author, source, subject, tags 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 do |_, value| - value and not value.empty? - end + super.map { |key, val| { key.to_s => val } }.reduce({}, :merge) + .compact.reject { |_, val| val.empty? } end def to_s spaced = text.gsub("\n", "\n\n") - wrapped = LovelyRufus.wrap(spaced, width: 80) + wrapped = LovelyRufus.wrap(spaced, width: 50) squeezed = wrapped.gsub("\n\n", "\n").chomp squeezed + meta_for(squeezed) end private