Sha256: 0ef5e78f7c9f4ca1e75891666a6a1ea097ca419485d35843a90b341695e89621

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

require 'hashie/mash'

module Hashie
  class Mash
    class Rash < Mash

      protected

      def convert_key(key) #:nodoc:
        underscore_string(key.to_s)
      end

      # Unlike its parent Mash, a Rash will convert other Hashie::Hash values to a Rash when assigning
      # instead of respecting the existing subclass
      def convert_value(val, duping=false) #:nodoc:
        case val
        when self.class
          val.dup
        when ::Hash
          val = val.dup if duping
          self.class.new(val)
        when Array
          val.collect{ |e| convert_value(e) }
        else
          val
        end
      end

      # converts a camel_cased string to a underscore string
      # subs spaces with underscores, strips whitespace
      # Same way ActiveSupport does string.underscore
      def underscore_string(str)
        str.to_s.strip.
          gsub(' ', '_').
          gsub(/::/, '/').
          gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
          gsub(/([a-z\d])([A-Z])/,'\1_\2').
          tr("-", "_").
          squeeze("_").
          downcase
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rash_alt-0.4.5 lib/hashie/mash/rash.rb
rash_alt-0.4.4 lib/hashie/mash/rash.rb
rash_alt-0.4.3 lib/hashie/mash/rash.rb
rash_alt-0.4.2 lib/hashie/mash/rash.rb
rash_alt-0.4.1 lib/hashie/mash/rash.rb
rash_alt-0.4.0 lib/hashie/mash/rash.rb