Sha256: f127f917a0f632a55e8f5c1743b11245a5f39408c0d211f9e2a1ca0f1b655118

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'fast_underscore/version'
require 'fast_underscore/fast_underscore'

module FastUnderscore
  # Depending on `ActiveSupport::VERSION`, `::install` determines the manner in
  # which acronyms are handled, then it redefines the
  # `ActiveSupport::Inflector::underscore` method to use the `FastUnderscore`
  # native extension. It leaves the existing `ActiveSupport` monkeypatch on
  # `String` that allows it to call into the newly redefined `Inflector` method.
  def self.install
    require 'active_support/version'
    gem_version = Gem::Version.new(ActiveSupport::VERSION::STRING)

    if gem_version >= Gem::Version.new('5.2.0')
      require 'fast_underscore/acronym_underscore_regex'
    else
      require 'fast_underscore/acronym_regex'
    end
  end
end

if defined?(ActiveSupport)
  FastUnderscore.install
else
  module ActiveSupport
    module Inflector
      class << self
        prepend(
          Module.new do
            # Hooks into ActiveSupport::Inflector and waits for the #underscore
            # method to be defined. When it is, it automatically redefines it.
            # Using this `prepend` trick to attempt to be a good citizen in the
            # case that someone else has already hooked into `method_added` on
            # `Inflector`.
            def method_added(method)
              method == :underscore ? FastUnderscore.install : super
            end
          end
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fast_underscore-0.3.1 lib/fast_underscore.rb