Sha256: ae3989a49feb8d66b1828b73acc200bb4b1158e752e671d92cb329a2f95b91d0

Contents?: true

Size: 681 Bytes

Versions: 5

Compression:

Stored size: 681 Bytes

Contents

# frozen_string_literal: true

module WSDirector
  module Ext
    # Extend Object through refinements
    module Formatting
      refine ::Object do
        def truncate(*) = itself
      end

      refine ::String do
        def truncate(limit)
          return self if size <= limit

          "#{self[0..(limit - 3)]}..."
        end
      end

      refine ::Hash do
        def truncate(limit)
          str = to_json

          str.truncate(limit)
        end
      end

      refine ::Float do
        def duration
          if self > 1
            "#{truncate(2)}s"
          else
            "#{(self * 1000).to_i}ms"
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wsdirector-core-1.1.0 lib/wsdirector/ext/formatting.rb
wsdirector-core-1.0.3 lib/wsdirector/ext/formatting.rb
wsdirector-core-1.0.2 lib/wsdirector/ext/formatting.rb
wsdirector-core-1.0.1 lib/wsdirector/ext/formatting.rb
wsdirector-core-1.0.0 lib/wsdirector/ext/formatting.rb