-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitem.rb
52 lines (42 loc) · 1.03 KB
/
item.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
50
51
52
require 'date'
class Item
attr_accessor :id
attr_reader :archived, :genre, :label, :author
def initialize(publish_date)
@id = Random.rand(0..10_000)
@publish_date = publish_date
@archived = false
end
def genre=(genre)
@genre = genre
genre.items.push(self) unless genre.items.include?(self)
end
def author=(author)
@author = author
author.items.push(self) unless author.items.include?(self)
end
def label=(label)
@label = label
label.items.push(self) unless label.items.include?(self)
end
def move_to_archive
@archived = can_be_archived?
end
def to_s
"Publish date: #{@publish_date}, Archived: #{@archived}\n#{@genre}\n#{@author}\n#{@label}"
end
def to_json(_args)
{
'id' => @id,
'publish_date' => @publish_date,
'archived' => @archived,
'genre_id' => @genre.id,
'author_id' => @author.id,
'label_id' => @label.id
}
end
private
def can_be_archived?
Date.today.year - Date.parse(@publish_date).year > 10
end
end