crude rework of the hashing to work in opal
This commit is contained in:
parent
2adec3226c
commit
3b888b203d
@ -40,20 +40,24 @@ module Parfait
|
|||||||
|
|
||||||
def self.for_hash( object_class , hash)
|
def self.for_hash( object_class , hash)
|
||||||
new_type = Type.new( object_class , hash)
|
new_type = Type.new( object_class , hash)
|
||||||
code = new_type.hash
|
code = hash_code_for_hash( hash )
|
||||||
Space.object_space.types[code] = new_type
|
Space.object_space.types[code] = new_type
|
||||||
new_type
|
new_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.hash_code_for( dict )
|
def self.hash_for_type(type)
|
||||||
|
self.hash_code_for_hash( type.to_hash )
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.hash_code_for_hash( dict )
|
||||||
index = 1
|
index = 1
|
||||||
hash = 0
|
hash_code = ""
|
||||||
dict.each do |name , type|
|
dict.each do |name , type|
|
||||||
item_hash = name.hash + type.hash
|
item_hash = name.to_s + type.to_s
|
||||||
hash += item_hash + (item_hash / 256 ) * index
|
hash_code += item_hash #+ (item_hash / 256 ) * index
|
||||||
index += 1
|
index += 1
|
||||||
end
|
end
|
||||||
hash
|
hash_code.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize( object_class , hash = nil)
|
def initialize( object_class , hash = nil)
|
||||||
@ -83,8 +87,9 @@ module Parfait
|
|||||||
def create_instance_method( method_name , arguments )
|
def create_instance_method( method_name , arguments )
|
||||||
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
||||||
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
||||||
arguments = Parfait::Type.for_hash( self.object_class , arguments) if arguments.is_a?(Hash)
|
type = arguments
|
||||||
add_instance_method TypedMethod.new( self , method_name , arguments )
|
type = Parfait::Type.for_hash( self.object_class , arguments) if arguments.is_a?(Hash)
|
||||||
|
add_instance_method TypedMethod.new( self , method_name , type )
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_instance_method( method )
|
def add_instance_method( method )
|
||||||
@ -133,7 +138,7 @@ module Parfait
|
|||||||
raise "No nil type" unless type
|
raise "No nil type" unless type
|
||||||
hash = to_hash
|
hash = to_hash
|
||||||
hash[name] = type
|
hash[name] = type
|
||||||
code = Type.hash_code_for( hash )
|
code = Type.hash_code_for_hash( hash )
|
||||||
existing = Space.object_space.types[code]
|
existing = Space.object_space.types[code]
|
||||||
if existing
|
if existing
|
||||||
return existing
|
return existing
|
||||||
@ -191,7 +196,7 @@ module Parfait
|
|||||||
end
|
end
|
||||||
|
|
||||||
def hash
|
def hash
|
||||||
Type.hash_code_for( to_hash )
|
Type.hash_code_for_hash( to_hash )
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -31,13 +31,13 @@ class TypeHash < MiniTest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_hashcode_with_hash
|
def test_hashcode_with_hash
|
||||||
assert_equal @first.hash , Parfait::Type.hash_code_for( @first.to_hash)
|
assert_equal @first.hash , Parfait::Type.hash_code_for_hash( @first.to_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_second_hash_different
|
def test_second_hash_different
|
||||||
hash2 = @first.to_hash
|
hash2 = @first.to_hash
|
||||||
hash2[:random] = :Type
|
hash2[:random] = :Type
|
||||||
assert @first.hash != Parfait::Type.hash_code_for( hash2 )
|
assert @first.hash != Parfait::Type.hash_code_for_hash( hash2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_add_is_different
|
def test_add_is_different
|
||||||
|
Loading…
Reference in New Issue
Block a user