Sha256: aa7f71ff97cffd9b8058e13a4cccd84622386e9f3a175a1c0bd356a32abc8ea1

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'lovely_rufus'

module Signore
  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

    def to_h
      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: 50)
      squeezed = wrapped.gsub("\n\n", "\n").chomp
      squeezed + meta_for(squeezed)
    end

    private

    def indent_size_for(text)
      indent = text.split("\n").map(&:size).max - meta.size - 2
      indent.negative? ? 0 : indent
    end

    def meta
      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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signore-0.7.0 lib/signore/signature.rb