Class: HashIdentable::Identity
- Inherits:
-
Object
- Object
- HashIdentable::Identity
- Defined in:
- lib/hash_identable/identity.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
Instance Method Summary collapse
- #encoded_id ⇒ Object
- #hash_key ⇒ Object
- #hash_table_id ⇒ Object
-
#initialize(klass, id) ⇒ Identity
constructor
A new instance of Identity.
- #klass_constant ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/hash_identable/identity.rb', line 7 def id @id end |
#klass ⇒ Object (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_id ⇒ Object
27 28 29 |
# File 'lib/hash_identable/identity.rb', line 27 def encoded_id id_wrapper.encode(hash_key) end |
#hash_key ⇒ Object
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_id ⇒ Object
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_constant ⇒ Object
39 40 41 |
# File 'lib/hash_identable/identity.rb', line 39 def klass_constant klass.constantize end |
#to_h ⇒ Object
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_s ⇒ Object
14 15 16 |
# File 'lib/hash_identable/identity.rb', line 14 def to_s encoded_id.to_s end |