lib/universa/contract.rb in universa-0.3.1 vs lib/universa/contract.rb in universa-0.3.2

- old
+ new

@@ -51,11 +51,11 @@ # Construct from string representation of the ID, not to confuse with binary one. This method takes both # regular base64 representation and RFC3548 url-safe modification, as from {#to_url_safe_string}. # # @param [String] string_id id string representation, like from +hash_id_instance.to_s+. See {#to_s}. def self.from_string(string_id) - string_id.force_encoding('utf-8').gsub('-','+').gsub('_','/') + string_id.force_encoding('utf-8').gsub('-', '+').gsub('_', '/') invoke_static 'with_digest', string_id end # Get binary representation. It is shorter than string representation but contain non-printable characters and # can cause problems if treated like a string. Use {#to_s} to get string representation instead. @@ -78,11 +78,11 @@ # the 62:nd + character with the minus - # # Could be decoded safely back with {HashId.from_string} but not (most likely) with JAVA API itself # @return [String] RFC3548 modified base64 def to_url_safe_string - Base64.encode64(get_digest).gsub(/\s/, '').gsub('/','_').gsub('+', '-') + Base64.encode64(get_digest).gsub(/\s/, '').gsub('/', '_').gsub('+', '-') end # To use it as a hash key_address. # @return hash calculated over the digest bytes def hash @@ -92,10 +92,25 @@ # To use it as a hash key_address. Same as this == other. def eql? other self == other end + # Compare hashid with string representation, binary representation or another HashId instance + # automatically. + def == other + return false if other == nil + if other.is_a?(Universa::HashId) + super + else + if other.size == 96 + bytes == other + else + to_s == other + end + end + end + end # Universa contract adapter. class Contract < RemoteAdapter remote_class "com.icodici.universa.contract.Contract" @@ -275,9 +290,16 @@ def create_revocation(*keys) revoke = Service.umi.invoke_static 'ContractsService', 'createRevocation', *keys revoke.seal revoke end + + # Ruby-style contracts equality. + def == other + return false if !other || !other.is_a?(Contract) + hash_id == other.hash_id + end + end end \ No newline at end of file