Sha256: 3560dd184269a91533218b8c14a16fc1b34099ab6860cda2befd5eb2a2c4f736

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module IronBank
  # A few helper methods for interacting with Zuora
  #
  module Utils
    module_function

    # Inspired from ActiveSupport
    def underscore(camel_cased_word)
      return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/

      word = camel_cased_word.to_s.gsub(/::/, '/')
      word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
      word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
      word.tr!('-', '_')
      word.downcase
    end

    def camelize(term, type: :upper)
      # Preserve custom field term postfix, in the '__c' or the '__NS' format
      custom_field   = (term.to_s =~ /__c$/i)
      netsuite_field = (term.to_s =~ /__NS$/i)
      lower_camelize = type == :lower

      output = term.to_s.dup.tap do |copy|
        copy.gsub!(/^_*/, '')
        copy.gsub!(/__(NS|c)$/i, '') if custom_field || netsuite_field

        lower_camelize ? lower_camelize(copy) : upper_camelize(copy)
      end

      if custom_field
        "#{output}__c"
      elsif netsuite_field
        "#{output}__NS"
      else
        output
      end
    end

    def kebab(term)
      underscore(term).tr!('_', '-')
    end

    def lower_camelize(term)
      term.gsub!(/(^|_)(.)/) do
        match = Regexp.last_match
        char  = match[2]

        match.begin(2).zero? ? char : char.upcase
      end
    end

    def upper_camelize(term)
      term.gsub!(/(^|_)(.)/) { Regexp.last_match(2).upcase }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
iron_bank-2.2.0 lib/iron_bank/utils.rb
iron_bank-2.1.0 lib/iron_bank/utils.rb
iron_bank-2.0.2 lib/iron_bank/utils.rb
iron_bank-2.0.1 lib/iron_bank/utils.rb
iron_bank-2.0.0 lib/iron_bank/utils.rb
iron_bank-1.0.4 lib/iron_bank/utils.rb
iron_bank-1.0.3 lib/iron_bank/utils.rb
iron_bank-1.0.2 lib/iron_bank/utils.rb
iron_bank-1.0.1 lib/iron_bank/utils.rb
iron_bank-1.0.0 lib/iron_bank/utils.rb
iron_bank-0.7.1 lib/iron_bank/utils.rb