Sha256: d21e501c677d433c4091a0b7816a9c8ef25ec1601e5e1b92456303349c7c5d6b

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

module Hashable
  # Return recursively hash representation of the object
  #
  # @return hash
  def to_deep_hash
    hash = {}
    instance_variables.each do |key|
      hash[key[1..-1].to_sym] = deeply_to_hash(instance_variable_get(key))
    end
    hash
  end
  alias_method :to_dh, :to_deep_hash

  protected
    # nodoc
    def convert_hash_values(hash)
      hash.each do |key, val|
        hash[key] = deeply_to_hash(val)
      end
      hash
    end

    # nodoc
    def convert_array_values(array)
      array.each_index do |index|
        array[index] = deeply_to_hash(array[index])
      end
      array
    end

    # nodoc
    def deeply_to_hash(val)
      if val.is_a?(Hash)
        convert_hash_values(val)
      elsif val.is_a?(Array)
        convert_array_values(val)
      elsif val.respond_to?(:to_dh)
        val.to_dh
      else
        val
      end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashable-0.1.0 lib/hashable/hashable.rb