Sha256: 5fc4ab4c7ab868cff622bfa521982c2ea780e274567bd3a567bd8977b6913553

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module RubyLess
  class SignatureHash < Hash
    alias get []
    
    # This method is used *A LOT*, make sure it stays as fast as possible.
    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubyless-0.8.11 lib/ruby_less/signature_hash.rb
rubyless-0.8.10 lib/ruby_less/signature_hash.rb
rubyless-0.8.9 lib/ruby_less/signature_hash.rb
rubyless-0.8.8 lib/ruby_less/signature_hash.rb
rubyless-0.8.7 lib/ruby_less/signature_hash.rb