Sha256: 97a3606302973a1fef671b357e4096948726a0fe651e404472e0a1f2bbd70679

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Zesty
  module Refinements
    module CamelCase
      refine Hash do

        def to_camel_case(data = self)
          case data
          when Array
            data.map { |value| to_camel_case(value) }
          when Hash
            data.map { |key, value| [camel_case_key(key), to_camel_case(value)] }.to_h
          else
            data
          end
        end

        private

        def camel_case_key(key)
          case key
          when Symbol
            camel_case(key.to_s).to_sym
          when String
            camel_case(key).to_sym
          else
            key
          end
        end

        def camel_case(string)
          @acronyms ||= { 'id' => 'ID', 'zuid' => 'ZUID' }
          @acronym_regex ||= /#{@acronyms.values.join("|")}/
          @acronyms_camelize_regex ||= /^(?:#{@acronym_regex}(?=\b|[A-Z_])|\w)/
          @acronyms_underscore_regex ||= /(?:(?<=([A-Za-z\d]))|\b)(#{@acronym_regex})(?=\b|[^a-z])/

          result = string.sub(/^[a-z\d]*/) { |match| @acronyms[match] || match }

          if !result.start_with?("_") # e.g. ignore "_meta", "_meta_title", etc.
            result = result.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{@acronyms[$2] || $2.capitalize}" }
          end

          result
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zesty-0.3.0 lib/zesty/refinements/camel_case.rb
zesty-0.2.1 lib/zesty/refinements/camel_case.rb
zesty-0.2.0 lib/zesty/refinements/camel_case.rb
zesty-0.1.0 lib/zesty/refinements/camel_case.rb