-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarise_articles_by_author.rb
112 lines (89 loc) · 2.95 KB
/
summarise_articles_by_author.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
# frozen_string_literal: true
require 'forwardable'
module Prolog
module UseCases
# Just what it says on the tin: like SummariseContent for a single Author.
class SummariseArticlesByAuthor
# Result notification encapsulation for query.
class Result
attr_reader :articles, :errors, :keywords
def initialize(errors:, articles:, keywords: [])
@errors = errors
@articles = articles
@keywords = keywords
self
end
def success?
errors.empty?
end
end # class Prolog::UseCases::SummariseArticlesByAuthor::Result
# Build list of articles by author visible to current user.
class ListArticles
def initialize(repository:, author_name:, authoriser:)
@author_name = author_name
@current_user = authoriser.current_user
@repository = repository
self
end
def call
articles = filter(articles_by_author)
Result.new articles: articles, errors: []
end
private
attr_reader :author_name, :current_user, :repository
def articles_by_author
repository.find(author_name: author_name)
end
def author?(article)
current_user == article.author_name
end
def filter(articles)
articles.select do |article|
article.published? || author?(article)
end
end
end # class Prolog::UseCases::SummariseContent::ListArticles
def initialize(authoriser:, repository:)
@authoriser = authoriser
@repository = repository
self
end
def call(author_name:)
@list = articles_by(author_name)
result
end
private
# NOTE: We know this doesn't really make any Forwardable methods private.
# It does, however, show that our delegations are to be treated as though
# they are to be used as though they were private methods.
extend Forwardable
attr_reader :authoriser, :repository
def_delegators :@list, :articles, :errors
def articles_by(author_name)
params = { author_name: author_name, authoriser: authoriser,
repository: repository }
ListArticles.new(params).call
end
def result
Result.new result_params
end
def result_params
{ articles: articles, keywords: Internals.keywords_from(articles),
errors: errors }
end
# Methods that neither affect nor depend on instance state.
module Internals
def self.keywords_from(articles)
_keyword_set_from(_all_keywords_in(articles))
end
def self._all_keywords_in(articles)
articles.map(&:keywords).flatten
end
def self._keyword_set_from(keywords)
Set.new keywords.sort
end
end
private_constant :Internals
end # class Prolog::UseCases::SummariseArticlesByAuthor
end
end