Sha256: 53fb6bac8b5fe4304a3f0bda073613f28a66245ee2d9e37980d534bfaeb6cf40

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

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)
      super text, author, source, subject, tags
      each_pair { |key, value| self[key] = nil if value and value.empty? }
    end

    def tagged_with?(tag)
      tags and tags.include?(tag)
    end

    def to_h
      super.map { |key, val| [key.to_s, val] }.to_h.keep_if { |_, value| value }
    end

    def to_s
      spaced   = text.gsub("\n", "\n\n")
      wrapped  = LovelyRufus.wrap(spaced, width: 80)
      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 < 0 ? 0 : indent
    end

    def meta
      stem = [author, subject].compact.join(' ')
      stem.empty? ? "#{source}" : [stem, source].compact.join(', ')
    end

    def meta_for(text)
      meta.empty? ? '' : "\n#{' ' * indent_size_for(text)}[#{meta}]"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
signore-0.3.3 lib/signore/signature.rb
signore-0.3.2 lib/signore/signature.rb
signore-0.3.1 lib/signore/signature.rb