Sha256: 5709929991fb2cf7c4c89157b9f732980975df6936dd969c090ab14a0228fee9

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Dhis2
  module Case
    SUPPORTER_CASE_CHANGES = %i(underscore camelize).freeze

    def self.deep_change(obj, type)
      raise CaseError, deep_change_error(type) unless SUPPORTER_CASE_CHANGES.include?(type)
      case obj
      when Array then obj.map { |v| deep_change(v, type) }
      when Hash
        obj.each_with_object({}) do |(k, v), new_hash|
          new_key = type == :underscore ? underscore(k.to_s) : camelize(k.to_s, false)
          new_hash[new_key] = deep_change(v, type)
        end
      else obj
      end
    end

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

    def self.underscore(camel_cased_word)
      return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
      camel_cased_word.to_s.gsub(/::/, "/")
                      .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
                      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
                      .tr("-", "_")
                      .downcase
    end

    def deep_change_error(type)
      "unsupported case changes #{type} vs #{SUPPORTER_CASE_CHANGES}"
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dhis2-3.3.1 lib/dhis2/case.rb
dhis2-3.2.1 lib/dhis2/case.rb
dhis2-3.0.7 lib/dhis2/case.rb
dhis2-3.0.5 lib/dhis2/case.rb
dhis2-3.0.4 lib/dhis2/case.rb
dhis2-3.0.3 lib/dhis2/case.rb
dhis2-3.0.2 lib/dhis2/case.rb
dhis2-3.0.0 lib/dhis2/case.rb