-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.rb
49 lines (38 loc) · 873 Bytes
/
list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require_relative "card"
class List
attr_reader :id, :name, :cards
@id_sequence = 0
def initialize(name:, cards: [], id: nil)
@id = id || self.class.next_id
@name = name
@cards = cards.empty? ? [] : cards.map { |card| Card.new card }
keep_biggest_id
end
def create_card(data)
new_card = Card.new(data)
@cards << new_card
end
def delete_card(id)
@cards.each_with_index do |card, index|
@cards.delete_at(index) if card.id == id
end
end
def update_list_name(name:)
@name = name
end
def to_json(*_args)
{ id: @id, name: @name, cards: @cards }.to_json
end
def self.next_id
@id_sequence += 1
end
def self.sequence_val
@id_sequence
end
def keep_biggest_id
self.class.sequence = @id if @id > self.class.sequence_val
end
def self.sequence=(id)
@id_sequence = id
end
end