Class: RubyLess::SignatureHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/ruby_less/signature_hash.rb

Instance Method Summary collapse

Instance Method Details

#[](signature) ⇒ Object

This method is used *A LOT*, make sure it stays as fast as possible.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_less/signature_hash.rb', line 7

def [](signature)
  if type = get(signature)
    # fastest: all keys are equal
    return type
  elsif signature.kind_of?(Array)
    size = signature.size
    static_types = true
    ancestors = signature.map do |k|
      if k.kind_of?(Symbol)
        [k]
      elsif k.kind_of?(Class) && k.name != ''
        k.ancestors
      else
        static_types = false
        k.respond_to?(:ancestors) ? k.ancestors : [k]
      end
    end
    each do |key, type|
      next unless key.size == size
      ok = true
      key.each_with_index do |k, i|
        if !ancestors[i].include?(k)
          ok = false
          break
        end
      end
      if ok
        # insert in cache if the signature does not contain dynamic types
        self[signature] = type if static_types
        return type
      end
    end
  end
  nil
end

#getObject



4
# File 'lib/ruby_less/signature_hash.rb', line 4

alias get []