Sha256: e590056feeedb0f89bb5f6f9420b7bfacc089db4620c995bc13dc9320791fade

Contents?: true

Size: 1.2 KB

Versions: 12

Compression:

Stored size: 1.2 KB

Contents

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

    refine Numeric do
      def blank?
        false
      end
    end

    refine String do
      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
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pact_broker-2.93.4 lib/pact_broker/string_refinements.rb
pact_broker-2.93.3 lib/pact_broker/string_refinements.rb
pact_broker-2.93.2 lib/pact_broker/string_refinements.rb
pact_broker-2.93.1 lib/pact_broker/string_refinements.rb
pact_broker-2.93.0 lib/pact_broker/string_refinements.rb
pact_broker-2.92.0 lib/pact_broker/string_refinements.rb
pact_broker-2.91.0 lib/pact_broker/string_refinements.rb
pact_broker-2.90.0 lib/pact_broker/string_refinements.rb
pact_broker-2.89.1 lib/pact_broker/string_refinements.rb
pact_broker-2.89.0 lib/pact_broker/string_refinements.rb
pact_broker-2.88.0 lib/pact_broker/string_refinements.rb
pact_broker-2.87.0 lib/pact_broker/string_refinements.rb