Sha256: 808c080895fb83114edb6b2c7490f69775b0a1256ad717b1fe05555656b4de89

Contents?: true

Size: 763 Bytes

Versions: 19

Compression:

Stored size: 763 Bytes

Contents

module Pupa
  module Concerns
    # Adds private methods for changing hash keys to strings or symbols.
    module IndifferentAccess
      extend ActiveSupport::Concern

      private

      def transform_keys(object, meth)
        case object
        when Hash
          {}.tap do |hash|
            object.each do |key,value|
              hash[key.send(meth)] = transform_keys(value, meth)
            end
          end
        when Array
          object.map do |value|
            transform_keys(value, meth)
          end
        else
          object
        end
      end

      def symbolize_keys(object)
        transform_keys(object, :to_sym)
      end

      def stringify_keys(object)
        transform_keys(object, :to_s)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pupa-0.2.4 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.2.3 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.2.2 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.2.1 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.2.0 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.11 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.10 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.9 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.8 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.7 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.6 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.5 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.4 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.3 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.2 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.1 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.1.0 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.0.13 lib/pupa/models/concerns/indifferent_access.rb
pupa-0.0.12 lib/pupa/models/concerns/indifferent_access.rb