Sha256: 51959792362d82ef7b50bc9f9fcba4d76d35171cb3cabf655d339999cca29f09

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Refinements
  module Strings
    refine String do
      def blank?
        empty? || /\A[[:space:]]*\z/.match?(self)
      end

      def present?
        !blank?
      end

      # Convert string to CamelCase
      def to_camel_case(uppercase_first_letter = false)
        string = self
        string = if uppercase_first_letter
                   string.sub(/^[a-z\d]*/, &:capitalize)
                 else
                   string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
                 end
        string.gsub(%r{(?:_|(/))([a-z\d]*)}) do
          "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}"
        end.gsub('/', '::')
      end

      # Convert string to snake_case
      def to_snake_case
        gsub(/::/, '/')
          .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
          .gsub(/([a-z\d])([A-Z])/, '\1_\2')
          .tr('-', '_')
          .downcase
      end

      # Convert to class symbol
      def to_class_sym
        to_camel_case(true).to_s.to_sym
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
business-central-2.0.0 lib/refinements/strings.rb