Class: MOCO::BaseEntity
- Inherits:
-
Object
show all
- Defined in:
- lib/moco/entities.rb
Overview
Base entity class others inherit from, providing comparison, to_h, to_json
Instance Method Summary
collapse
Instance Method Details
#==(other) ⇒ Object
16
17
18
|
# File 'lib/moco/entities.rb', line 16
def ==(other)
id == other.id
end
|
#eql?(other) ⇒ Boolean
6
7
8
9
10
|
# File 'lib/moco/entities.rb', line 6
def eql?(other)
return false unless other.is_a? self.class
id == other.id
end
|
#hash ⇒ Object
12
13
14
|
# File 'lib/moco/entities.rb', line 12
def hash
id.hash
end
|
#to_h ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/moco/entities.rb', line 20
def to_h
hash = {}
instance_variables.each do |var|
key = var.to_s.delete_prefix("@")
hash[key.to_sym] = instance_variable_get(var)
end
hash
end
|
#to_json(*arg) ⇒ Object
rubocop:disable Metrics/MethodLength
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/moco/entities.rb', line 30
def to_json(*arg)
to_h do |k, v|
if v.is_a? Hash
if v.key?(:id) && !v[:id].nil?
["#{k}_id", v[:id]]
else
[k, v.except(:id)]
end
else
[k, v]
end
end.to_h.to_json(arg)
end
|