-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rb
123 lines (96 loc) · 2.39 KB
/
solution.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Please copy/paste all three classes into this file to submit your solution!
# Article class here
class Article
@@all = []
attr_accessor :title, :magazine
def initialize(title, magazine, author)
@title = title
@magazine = magazine
@author = author
@@all << self
end
def self.all
@@all
end
def author
@author
end
def magazine
@magazine
end
def title
@title
end
def author_name
self.author.name
end
def magazine_name
self.magazine.name
end
end
# Author class here
class Author
attr_reader :name
def initialize(name)
@name = name
end
def articles
Article.all.select { |article| article.author == self }
end
def magazines
articles.collect { |article| article.magazine }.uniq
end
def add_article(magazine, title)
Article.new(title, magazine, self)
end
def topic_areas
magazines.collect { |magazine| magazine.category }.uniq
end
end
# Magazine class here
class Magazine
attr_accessor :name, :category
@@all = []
def initialize(name, category)
@name = name
@category = category
@@all << self
end
def self.all
@@all
end
def articles
Article.all.select { |article| article.magazine == self }
end
def authors
articles.collect { |article| article.author }.uniq
end
def article_titles
articles.collect { |article| article.title }
end
def author_names
authors.collect { |author| author.name }
end
def contributor_names
Article.all.filter { |article| article.magazine == self }.map { |article| article.author.name }.uniq
end
def self.find_by_name(name)
self.all.find { |magazine| magazine.name == name }
end
def self.find_by_category(category)
self.all.select { |magazine| magazine.category == category }
end
def self.most_popular
self.all.max_by { |magazine| magazine.contributor_names.count }
end
def contributor_count
contributor_names.count
end
def contributing_authors
popular_authors = Article.all.filter { |article| article.magazine.name == @name }.map {
|article|
article.author.name
}.tally.each { |key, value| value > 2 }
popular_authors
end
end