Sha256: b98e4eb7d86d42a848b16ed56e84a90aa23461a9bc0e17ee52c0db4fe32586af

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

module PactBroker
  module StringRefinements
    refine NilClass do
      def blank?
        true
      end

      def present?
        false
      end
    end

    refine Numeric do
      def blank?
        false
      end
    end

    refine String do
      def present?
        !blank?
      end

      def not_blank?
        !blank?
      end

      def blank?
        self.strip.size == 0
      end

      # ripped from rubyworks/facets, thank you
      def snakecase
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
        .gsub(/([a-z\d])([A-Z])/,'\1_\2')
        .tr("-", "_")
        .gsub(/\s/, "_")
        .gsub(/__+/, "_")
        .downcase
      end

      # ripped from rubyworks/facets, thank you
      def camelcase(*separators)
        case separators.first
        when Symbol, TrueClass, FalseClass, NilClass
          first_letter = separators.shift
        end

        separators = ["_", "\\s"] if separators.empty?

        str = self.dup

        separators.each do |s|
          str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
        end

        case first_letter
        when :upper, true
          str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
        when :lower, false
          str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
        end

        str
      end

      # Adopt from https://stackoverflow.com/questions/1451384/how-can-i-center-truncate-a-string
      def ellipsisize(minimum_length: 20, edge_length: 10)
        return self if self.length < minimum_length || self.length <= edge_length * 2

        edge = "." * edge_length
        mid_length = self.length - edge_length * 2
        gsub(/(#{edge}).{#{mid_length},}(#{edge})/, '\1...\2')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pact_broker-2.102.1 lib/pact_broker/string_refinements.rb
pact_broker-2.102.0 lib/pact_broker/string_refinements.rb