-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbase.rb
More file actions
80 lines (69 loc) · 2.23 KB
/
base.rb
File metadata and controls
80 lines (69 loc) · 2.23 KB
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
# frozen_string_literal: true
module Ruby
module Enum
module Errors
class Base < StandardError
# Problem occurred.
attr_reader :problem
# Summary of the problem.
attr_reader :summary
# Suggested problem resolution.
attr_reader :resolution
# Compose the message.
# === Parameters
# [key] Lookup key in the translation table.
# [attributes] The objects to pass to create the message.
def compose_message(key, attributes = {})
@problem = create_problem(key, attributes)
@summary = create_summary(key, attributes)
@resolution = create_resolution(key, attributes)
"\nProblem:\n #{@problem}" \
"\nSummary:\n #{@summary}" + "\nResolution:\n #{@resolution}"
end
BASE_KEY = 'ruby.enum.errors.messages' # :nodoc:
private
# Given the key of the specific error and the options hash, translate the
# message.
#
# === Parameters
# [key] The key of the error in the locales.
# [options] The objects to pass to create the message.
#
# Returns a localized error message string.
def translate(key, options)
Ruby::Enum.i18n.translate("#{BASE_KEY}.#{key}", locale: :en, **options).strip
end
# Create the problem.
#
# === Parameters
# [key] The error key.
# [attributes] The attributes to interpolate.
#
# Returns the problem.
def create_problem(key, attributes)
translate("#{key}.message", attributes)
end
# Create the summary.
#
# === Parameters
# [key] The error key.
# [attributes] The attributes to interpolate.
#
# Returns the summary.
def create_summary(key, attributes)
translate("#{key}.summary", attributes)
end
# Create the resolution.
#
# === Parameters
# [key] The error key.
# [attributes] The attributes to interpolate.
#
# Returns the resolution.
def create_resolution(key, attributes)
translate("#{key}.resolution", attributes)
end
end
end
end
end