Sha256: 9101f540bc63eff4c3b121f504e94e11aeebc6defbc6be8b8e71d1bed093e678

Contents?: true

Size: 822 Bytes

Versions: 22

Compression:

Stored size: 822 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(&:camelcase).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?(:ledgers)

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

          return inflection
        end

        str
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.5.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.4.4 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.3.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.2.3 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.2.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.2.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.0.2 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.0.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.0.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.8.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.8.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.7.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.6.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.5.2 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.5.1 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.5.0 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.4.4 lib/ledger_sync/util/string_helpers.rb
ledger_sync-1.4.2 lib/ledger_sync/util/string_helpers.rb