Sha256: c1d30919b1b4d94a168be18ca32880f152c6f8d4d74d9f18504d223c19226b52

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

class Hash
  # TODO: Refactoring
  def optimize
    routes = []
    singleton_hash = {}
    first_keys = self.keys
    second_keys = []
    third_keys = []

    first_keys.each do |key|
      unless self[key].is_a?(Hash)
        routes << [key]
        next
      end
      second_keys << [key, self[key].keys]
    end

    second_keys.each do |second_key|
      second_key.last.each do |key|
        unless self[second_key.first][key].is_a?(Hash)
          routes << [second_key.first, key]
          next
        end
        third_keys << [second_key.first, key, self[second_key.first][key].keys]
      end
    end

    third_keys.each do |third_key|
      third_key.last.each do |key|
        unless self[third_key.first][third_key[1]][key].is_a?(Hash)
          routes << [third_key.first, third_key[1], key]
          next
        end
        force_keys << [third_key.first, third_key[1], key, self[third_key.first][third_key[1]][key].keys]
      end
    end

    routes.each do |route|
      val = self.dup
      route.each do |key|
        val = val[key]
      end
      base = route.shift.to_s
      key_name = route.inject(base) {|key, map| key.to_s + "[" + map.to_s + "]" }
      singleton_hash.update({key_name=>val.to_s})
    end

    singleton_hash
  end
end


Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
youroom_api-0.1.4 lib/object_extention.rb
youroom_api-0.1.3 lib/object_extention.rb
youroom_api-0.1.2 lib/object_extention.rb
youroom_api-0.1.1 lib/object_extention.rb
youroom_api-0.1.0 lib/object_extention.rb
youroom_api-0.0.13 lib/object_extention.rb
youroom_api-0.0.12 lib/object_extention.rb
youroom_api-0.0.11 lib/object_extention.rb
youroom_api-0.0.10 lib/object_extention.rb