Skip to content

Commit b616243

Browse files
authored
Merge pull request #11 from dwei-stripe/dwei-support-raise-on-missing-params-during-interpretation
Add `raise_on_missing_params` option to interpreter
2 parents 0bfe53d + 224792a commit b616243

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/message_format.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
module MessageFormat
77
class MessageFormat
88

9-
def initialize ( pattern, locale=nil )
9+
def initialize ( pattern, locale=nil, raise_on_missing_params: false )
1010
@locale = (locale || TwitterCldr.locale).to_sym
1111
@format = Interpreter.interpret(
1212
Parser.parse(pattern),
13-
{ :locale => @locale }
13+
{
14+
:locale => @locale,
15+
:raise_on_missing_params => raise_on_missing_params,
16+
},
1417
)
1518
end
1619

@@ -22,15 +25,18 @@ def format ( args=nil )
2225

2326
class << self
2427

25-
def new ( pattern, locale=nil )
26-
MessageFormat.new(pattern, locale)
28+
def new ( pattern, locale=nil, raise_on_missing_params: false )
29+
MessageFormat.new(pattern, locale, raise_on_missing_params)
2730
end
2831

29-
def format_message ( pattern, args=nil, locale=nil )
32+
def format_message ( pattern, args=nil, locale=nil, raise_on_missing_params: false )
3033
locale ||= TwitterCldr.locale
3134
Interpreter.interpret(
3235
Parser.parse(pattern),
33-
{ :locale => locale.to_sym }
36+
{
37+
:locale => locale.to_sym,
38+
:raise_on_missing_params => raise_on_missing_params,
39+
}
3440
).call(args)
3541
end
3642

lib/message_format/interpreter.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,35 @@ def initialize ( options=nil )
2323
else
2424
@locale = TwitterCldr.locale
2525
end
26+
@raise_on_missing_params = options[:raise_on_missing_params]
27+
end
28+
29+
#
30+
# MissingParametersError
31+
# Holds information about parameters that were accessed during interpretation but were not
32+
# provided. Only raised if the `raise_on_missing_params` option is set to `true`.
33+
#
34+
# Example:
35+
# message = MessageFormat.new('Hello { place } and { player }!', 'en-US', raise_on_missing_params: true)
36+
# formatted = message.format({ :place => 'World' }) # raises with "player" identified as a missing parameter
37+
#
38+
# Note that only parameters that were actually accessed during interpretation will be reported.
39+
#
40+
class MissingParametersError < StandardError
41+
attr_reader :missing_params
42+
43+
def initialize ( message, missing_params )
44+
super(message)
45+
@missing_params = missing_params
46+
end
2647
end
2748

2849
def interpret ( elements )
50+
@missing_ids = []
2951
interpret_subs(elements)
52+
if @raise_on_missing_params && !@missing_ids.empty?
53+
raise MissingParametersError.new('Missing parameters detected during interpretation', @missing_ids.compact)
54+
end
3055
end
3156

3257
def interpret_subs ( elements, parent=nil )
@@ -82,6 +107,7 @@ def interpret_element ( element, parent=nil )
82107
def interpret_number ( id, offset, style )
83108
locale = @locale
84109
lambda do |args|
110+
@missing_ids.push(id) unless args.key?(id)
85111
number = TwitterCldr::Localized::LocalizedNumber.new(args[id] - offset, locale)
86112
if style == 'integer'
87113
number.to_decimal.to_s(:precision => 0)
@@ -102,6 +128,7 @@ def interpret_number ( id, offset, style )
102128
def interpret_date_time ( id, type, style='medium' )
103129
locale = @locale
104130
lambda do |args|
131+
@missing_ids.push(id) unless args.key?(id)
105132
datetime = TwitterCldr::Localized::LocalizedDateTime.new(args[id], locale)
106133
datetime = type == 'date' ? datetime.to_date : datetime.to_time
107134
if style == 'medium'
@@ -128,6 +155,7 @@ def interpret_plural ( id, type, offset, children )
128155
locale = @locale
129156
plural_type = type == 'selectordinal' ? :ordinal : :cardinal
130157
lambda do |args|
158+
@missing_ids.push(id) unless args.key?(id)
131159
arg = args[id]
132160
exactSelector = ('=' + arg.to_s).to_sym
133161
keywordSelector = TwitterCldr::Formatters::Plurals::Rules.rule_for(arg - offset, locale, plural_type)
@@ -145,6 +173,7 @@ def interpret_select ( id, children )
145173
options[key.to_sym] = interpret_subs(value, nil)
146174
end
147175
lambda do |args|
176+
@missing_ids.push(id) unless args.key?(id)
148177
selector = args[id].to_sym
149178
func =
150179
options[selector] ||
@@ -154,7 +183,10 @@ def interpret_select ( id, children )
154183
end
155184

156185
def interpret_simple ( id )
157-
lambda { |args| args[id].to_s }
186+
lambda do |args|
187+
@missing_ids.push(id) unless args.key?(id)
188+
args[id].to_s
189+
end
158190
end
159191

160192
def self.interpret ( elements, options=nil )

0 commit comments

Comments
 (0)