-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.rb
53 lines (42 loc) · 1.09 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
53
require 'date'
# Class representing an Item
class Item
attr_accessor :id, :genre, :author, :label, :publish_date
def initialize(params = {})
@id = params[:id] || Random.rand(1..10_000)
@genre = params[:genre]
@author = params[:author]
@label = params[:label]
@publish_date = parse_date(params[:publish_date])
@archived = false
end
def move_to_archive
@archived = true if can_be_archived?
end
def add_genre(genre)
@genre = genre
genre.items.push(self) unless genre.items.include?(self)
end
def add_author(author)
@author = author
author.items.push(self) unless author.items.include?(self)
end
def add_label(label)
@label = label
label.items.push(self) unless label.items.include?(self)
end
private
attr_reader :archived
def parse_date(date_string)
return nil unless date_string
begin
Date.strptime(date_string, '%d-%m-%Y')
rescue Date::Error
# Silenciosamente establece la fecha de publicación a nil
nil
end
end
def can_be_archived?
(Time.now.year - @publish_date.year > 10)
end
end