Sha256: 20bf4a049d228042b8e7c9f31bb339606c3883c3706f1e6e2f9c8195fc7b0568

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

module RubyLess
  class SignatureHash < Hash
    alias get []

    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

7 entries across 7 versions & 1 rubygems

Version Path
rubyless-0.8.6 lib/ruby_less/signature_hash.rb
rubyless-0.8.5 lib/ruby_less/signature_hash.rb
rubyless-0.8.4 lib/ruby_less/signature_hash.rb
rubyless-0.8.3 lib/ruby_less/signature_hash.rb
rubyless-0.8.2 lib/ruby_less/signature_hash.rb
rubyless-0.8.1 lib/ruby_less/signature_hash.rb
rubyless-0.8.0 lib/ruby_less/signature_hash.rb