Sha256: e16435a41a1593f21152ea3fcba8a6638e9da56750bbc6d3e584141c842033f1

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

begin
  require 'active_support/core_ext/hash/keys'
  require 'active_support/core_ext/hash/deep_merge'
  require 'active_support/core_ext/hash/slice'
rescue LoadError
  class Hash
    def stringify_keys
      keys.each do |key|
        self[key.to_s] = delete(key)
      end
      self
    end if !{}.respond_to?(:stringify_keys)

    def symbolize_keys
      keys.each do |key|
        self[(key.to_sym rescue key) || key] = delete(key)
      end
      self
    end if !{}.respond_to?(:symbolize_keys)

    def deep_symbolize_keys
      keys.each do |key|
        value = delete(key)
        self[(key.to_sym rescue key) || key] = value

        value.deep_symbolize_keys if value.is_a? Hash
      end
      self
    end if !{}.respond_to?(:deep_symbolize_keys)

    def slice(*keys)
      keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
      keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
    end
  end
end

begin
  require 'active_support/core_ext/string/inflections'
rescue LoadError
  class String
    def constantize
      names = self.split('::')
      names.shift if names.empty? || names.first.empty?

      constant = Object
      names.each do |name|
        constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
      end
      constant
    end

    def camelize
      string = self
      string = string.sub(/^[a-z\d]*/) { $&.capitalize }
      string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
      string.gsub!(/\//, '::')
      string
    end

    def underscore
      camel_cased_word = self
      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!
      word
    end
  end if !"".respond_to?(:constantize)
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
announce-0.2.3 lib/announce/core_ext.rb
announce-0.2.2 lib/announce/core_ext.rb
announce-0.2.1 lib/announce/core_ext.rb
announce-0.2.0 lib/announce/core_ext.rb