Sha256: c4f5187222c06f166c25d74356480715aeeb0eb18c703193b722d18c11f8b0fa

Contents?: true

Size: 632 Bytes

Versions: 4

Compression:

Stored size: 632 Bytes

Contents

# frozen_string_literal: true

module ActiveDelivery
  module Ext
    # Add simple safe_constantize method to String
    module StringConstantize
      refine String do
        def safe_constantize
          names = split("::")

          return nil if names.empty?

          # Remove the first blank element in case of '::ClassName' notation.
          names.shift if names.size > 1 && names.first.empty?

          names.inject(Object) do |constant, name|
            break if constant.nil?
            constant.const_get(name, false) if constant.const_defined?(name, false)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_delivery-1.2.0 lib/active_delivery/ext/string_constantize.rb
active_delivery-1.1.0 lib/active_delivery/ext/string_constantize.rb
active_delivery-1.0.0 lib/active_delivery/ext/string_constantize.rb
active_delivery-1.0.0.rc2 lib/active_delivery/ext/string_constantize.rb