Class: Basketball::ValueObject

Inherits:
Object
  • Object
show all
Extended by:
ValueObjectDSL
Includes:
Comparable
Defined in:
lib/basketball/value_object.rb

Overview

A Value Object is something that has no specific identity, instead its identity is the sum of the attribute values. Changing one will change the entire object’s identity. Comes with a very simple DSL provided by ValueObjectDSL for specifying properties along with base equality and sorting methods.

Direct Known Subclasses

Common::Position, Season::Detail, Season::Game

Instance Method Summary collapse

Methods included from ValueObjectDSL

all_sorted_value_keys, all_value_keys, value_keys, value_reader

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
# File 'lib/basketball/value_object.rb', line 26

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

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



30
31
32
# File 'lib/basketball/value_object.rb', line 30

def ==(other)
  to_h == other.to_h
end

#[](key) ⇒ Object



22
23
24
# File 'lib/basketball/value_object.rb', line 22

def [](key)
  to_h[key]
end

#all_sorted_valuesObject



39
40
41
# File 'lib/basketball/value_object.rb', line 39

def all_sorted_values
  self.class.all_sorted_value_keys.map { |k| self[k] }
end

#hashObject



35
36
37
# File 'lib/basketball/value_object.rb', line 35

def hash
  all_sorted_values.hash
end

#to_hObject



18
19
20
# File 'lib/basketball/value_object.rb', line 18

def to_h
  self.class.all_sorted_value_keys.to_h { |k| [k, send(k)] }
end

#to_sObject



14
15
16
# File 'lib/basketball/value_object.rb', line 14

def to_s
  to_h.map { |k, v| "#{k}: #{v}" }.join(', ')
end