Sha256: da07f2b37e7805ea79319ed535221f98a515f567c40ad1efb771bbbb330efbf0

Contents?: true

Size: 724 Bytes

Versions: 6

Compression:

Stored size: 724 Bytes

Contents

module Truck
  module StringInflections
    refine String do
      def to_camel_case
        str = "_#{self}"
        str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase }
        str.gsub!('/', '::')
        str
      end

      def to_snake_case
        str = gsub '::', '/'
        # Convert FOOBar => FooBar
        str.gsub! %r{[[:upper:]]{2,}} do |uppercase|
          bit = uppercase[0]
          bit << uppercase[1...-1].downcase
          bit << uppercase[-1]
          bit
        end
        str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel|
          bit = camel[0]
          bit << '_'
          bit << camel[1..-1].downcase
        end
        str.downcase!
        str
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
truck-0.8.6 lib/truck/string_inflections.rb
truck-0.8.4 lib/truck/string_inflections.rb
truck-0.8.3 lib/truck/string_inflections.rb
truck-0.8.2 lib/truck/string_inflections.rb
truck-0.8.1 lib/truck/string_inflections.rb
truck-0.8.0 lib/truck/string_inflections.rb