Class: Brigitte::Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/brigitte/card.rb

Overview

A Card has a value and sign defined separately.

Note: Compare cards’ value with their weight. Cards are only equal if their id’s are the same.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, sign, id = nil) ⇒ Card

Returns a new instance of Card.



16
17
18
19
20
# File 'lib/brigitte/card.rb', line 16

def initialize(value, sign, id = nil)
  @id = id || SecureRandom.uuid
  @value = value
  @sign = sign
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/brigitte/card.rb', line 14

def id
  @id
end

#signObject (readonly)

Returns the value of attribute sign.



14
15
16
# File 'lib/brigitte/card.rb', line 14

def sign
  @sign
end

#valueObject (readonly)

Returns the value of attribute value.



14
15
16
# File 'lib/brigitte/card.rb', line 14

def value
  @value
end

Class Method Details

.from_h(card_hash) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/brigitte/card.rb', line 53

def self.from_h(card_hash)
  return if card_hash.empty?

  new(
    card_hash[:value],
    card_hash[:sign],
    card_hash[:id]
  )
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/brigitte/card.rb', line 22

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

#order_levelObject



39
40
41
42
43
# File 'lib/brigitte/card.rb', line 39

def order_level
  return 15 if @value == '2'

  weight
end

#to_hObject



45
46
47
48
49
50
51
# File 'lib/brigitte/card.rb', line 45

def to_h
  {
    id: id,
    value: value,
    sign: sign
  }
end

#to_sObject



26
27
28
# File 'lib/brigitte/card.rb', line 26

def to_s
  value + sign
end

#weightObject



30
31
32
33
34
35
36
37
# File 'lib/brigitte/card.rb', line 30

def weight
  return 11 if @value == 'J'
  return 12 if @value == 'Q'
  return 13 if @value == 'K'
  return 14 if @value == 'A'

  value.to_i
end