Class: Quby::Compiler::Services::QubyProxy::ShortenKeysUniq

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/compiler/services/quby_proxy.rb

Instance Method Summary collapse

Constructor Details

#initializeShortenKeysUniq

Returns a new instance of ShortenKeysUniq.



336
337
338
# File 'lib/quby/compiler/services/quby_proxy.rb', line 336

def initialize
  @seen_results = []
end

Instance Method Details

#shorten_one(key) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/quby/compiler/services/quby_proxy.rb', line 340

def shorten_one(key)
  key = key.to_s
  limit = 2
  shortened_key = nil
  loop do
    shortened_key = key[0..limit]
    if key[limit] == "_"
      limit += 1
      next
    end
    break unless @seen_results.include?(shortened_key)
    raise "duplicate key, #{key}" if shortened_key.length == key.length
    limit += 1
  end

  @seen_results << shortened_key
  shortened_key
end

#shorten_two(first_key, second_key) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/quby/compiler/services/quby_proxy.rb', line 359

def shorten_two(first_key, second_key)
  first_key = first_key.to_s
  second_key = second_key.to_s
  first_limit = [2, first_key.length - 1].min
  second_limit = 0
  shortened_key = nil
  loop do
    shortened_key = "#{first_key[0..first_limit]}_#{second_key[0..second_limit]}"
    if first_key[first_limit] == "_"
      first_limit += 1
      next
    end
    if second_key[second_limit] == "_"
      second_limit += 1
      next
    end
    break unless @seen_results.include?(shortened_key)
    raise "duplicate key, #{first_key}_#{second_key}" if first_limit == (first_key.length - 1) &&
                                                         second_limit == (second_key.length - 1)

    if second_limit == (second_key.length - 1)
      first_limit += 1
      second_limit = 0
    else
      second_limit += 1
    end
  end

  @seen_results << shortened_key
  shortened_key
end