Class: Basketball::Entity

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/basketball/entity.rb

Overview

Base class for uniquely identifiable classes. Subclasses are simply based on a string-based ID and comparison/sorting/equality will be done in a case-insensitive manner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Entity

Returns a new instance of Entity.



11
12
13
# File 'lib/basketball/entity.rb', line 11

def initialize(id = nil)
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/basketball/entity.rb', line 9

def id
  @id
end

Instance Method Details

#<=>(other) ⇒ Object



15
16
17
# File 'lib/basketball/entity.rb', line 15

def <=>(other)
  comparable_id <=> other.comparable_id
end

#==(other) ⇒ Object Also known as: eql?



19
20
21
# File 'lib/basketball/entity.rb', line 19

def ==(other)
  comparable_id == other.comparable_id
end

#comparable_idObject



32
33
34
# File 'lib/basketball/entity.rb', line 32

def comparable_id
  id.to_s.upcase
end

#hashObject



24
25
26
# File 'lib/basketball/entity.rb', line 24

def hash
  comparable_id.hash
end

#to_sObject



28
29
30
# File 'lib/basketball/entity.rb', line 28

def to_s
  id.to_s
end