Class: HashIdentable::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_identable/identity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, id) ⇒ Identity

Returns a new instance of Identity.



9
10
11
12
# File 'lib/hash_identable/identity.rb', line 9

def initialize klass, id
  @klass = klass.to_s
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/hash_identable/identity.rb', line 7

def id
  @id
end

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/hash_identable/identity.rb', line 7

def klass
  @klass
end

Class Method Details

.find(encoded_id) ⇒ Object



43
44
45
46
47
# File 'lib/hash_identable/identity.rb', line 43

def self.find(encoded_id)
  decoded = HshFunction.decode(encoded_id)
  klass = HashIdentable.lookup_table.fetch(decoded[0]){ raise NoObjectRegistered, "No object registered" }
  new klass, decoded[1]
end

Instance Method Details

#encoded_idObject



27
28
29
# File 'lib/hash_identable/identity.rb', line 27

def encoded_id
  id_wrapper.encode(hash_key)
end

#hash_keyObject



22
23
24
25
# File 'lib/hash_identable/identity.rb', line 22

def hash_key
  raise "An ID is required to build a #hash_key" if id.nil? or klass.nil?
  return [hash_table_id, id]
end

#hash_table_idObject



18
19
20
# File 'lib/hash_identable/identity.rb', line 18

def hash_table_id
  @hash_table_id ||= HashIdentable.lookup_table.fetch_id(klass){ raise NoObjectRegistered, "No object registered" }
end

#klass_constantObject



39
40
41
# File 'lib/hash_identable/identity.rb', line 39

def klass_constant
  klass.constantize
end

#to_hObject



31
32
33
34
35
36
37
# File 'lib/hash_identable/identity.rb', line 31

def to_h
  {
    klass: klass,
    id: id,
    encoded_id: encoded_id
  }
end

#to_sObject



14
15
16
# File 'lib/hash_identable/identity.rb', line 14

def to_s
  encoded_id.to_s
end