Sha256: 702e40f28b29cadab449a91736eab04c30e8c101b5bc9ea98a19aa3852b4d41f

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module RubyJard
  module Decorators
    ##
    # A light decorator for a string. String should be escaped, and cut off.
    class StringDecorator
      def initialize(generic_decorator)
        @generic_decorator = generic_decorator
      end

      def match?(variable)
        RubyJard::Reflection.call_is_a?(variable, String)
      end

      # rubocop:disable Lint/UnusedMethodArgument
      def decorate_multiline(variable, first_line_limit:, line_limit:, lines:, depth: 0)
        [
          decorate_singleline(variable, line_limit: first_line_limit)
        ]
      end

      def decorate_singleline(variable, line_limit:, depth: 0)
        inspection = variable.inspect[1..-1].chomp!('"')
        str =
          if inspection.length < line_limit - 2
            inspection
          else
            inspection[0..line_limit - 4] + '…'
          end
        [

          RubyJard::Span.new(content: '"', styles: :string),
          RubyJard::Span.new(content: str, styles: :string),
          RubyJard::Span.new(content: '"', styles: :string)
        ]
      end
      # rubocop:enable Lint/UnusedMethodArgument
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_jard-0.3.1 lib/ruby_jard/decorators/string_decorator.rb
ruby_jard-0.3.0 lib/ruby_jard/decorators/string_decorator.rb