Sha256: e03a36dbc0c6daee030a24b6a58a8dd685e89c14c446550fc57360365edbe8b8

Contents?: true

Size: 825 Bytes

Versions: 5

Compression:

Stored size: 825 Bytes

Contents

# frozen_string_literal: true

module LedgerSync
  module Util
    module StringHelpers
      module_function

      def camelcase(str)
        str.to_s.split('/').map { |e| inflect(e.split('_').collect(&:capitalize).join) }.join('::')
      end

      def underscore!(str)
        str.gsub!(/::/, '/')
        str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
        str.tr!('-', '_')
        str.downcase!
      end

      def underscore(str)
        str.dup.tap(&method(:underscore!))
      end

      def inflect(str)
        return str unless LedgerSync.respond_to?(:adaptors)

        LedgerSync.adaptors.inflections.each do |inflection|
          next unless inflection.downcase == str.downcase

          return inflection
        end

        str
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.3.4 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.3.3 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.3.2 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.3.1 lib/ledger_sync/util/string_helpers.rb