Class: MOCO::BaseEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/moco/entities.rb

Overview

Base entity class others inherit from, providing comparison, to_h, to_json

Direct Known Subclasses

Activity, Customer, Project, Task, User

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



18
19
20
# File 'lib/moco/entities.rb', line 18

def ==(other)
  id == other.id
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/moco/entities.rb', line 8

def eql?(other)
  return false unless other.is_a? self.class

  id == other.id
end

#hashObject



14
15
16
# File 'lib/moco/entities.rb', line 14

def hash
  id.hash
end

#to_hObject



22
23
24
25
26
27
28
29
# File 'lib/moco/entities.rb', line 22

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/moco/entities.rb', line 32

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