generated from ger619/ruby-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitem.rb
43 lines (36 loc) · 812 Bytes
/
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
require 'date'
class Item
attr_reader :id, :genre, :author, :label, :publish_date, :archived
def initialize(id = Random.rand(1..1000))
@id = id
@genre = nil
@author = nil
@label = nil
@publish_date = nil
@archived = false
end
def add_label(label)
label.is_a?(Label) && @label.nil? && (
@label = label
label.add_item(self)
)
end
def add_genre(genre)
genre.is_a?(Genre) && @genre.nil? && (
@genre = genre
genre.add_item(self)
)
end
def add_author(author)
author.is_a?(Author) && @author.nil? && (
@author = author
author.add_item(self)
)
end
def move_to_archive
@archived = true if can_be_archived?
end
def can_be_archived?
(Date.today - Date.parse(@publish_date)).to_i > 365 * 10
end
end