Skip to content

Commit 1ecb656

Browse files
committed
added format_message, removed lots of explicit returns
1 parent 0b08ae5 commit 1ecb656

File tree

7 files changed

+122
-73
lines changed

7 files changed

+122
-73
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
## 0.0.2
4+
5+
* **New Feature**
6+
* added `MessageFormat.format_message` class method
7+
* **Polish**
8+
* better follow community style conventions
9+
10+
## 0.0.1
11+
12+
* Initial release

CODE_OF_CONDUCT.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Contributor Code of Conduct
2+
3+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4+
5+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6+
7+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8+
9+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10+
11+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12+
13+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)

lib/message_format.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ def initialize ( pattern, locale=nil )
1515
end
1616

1717
def format ( args=nil )
18-
return @format.call(args)
18+
@format.call(args)
1919
end
2020

2121
end
2222

2323
class << self
2424

2525
def new ( pattern, locale=nil )
26-
return MessageFormat.new(pattern, locale)
26+
MessageFormat.new(pattern, locale)
27+
end
28+
29+
def format_message ( pattern, args=nil, locale=nil )
30+
locale ||= TwitterCldr.locale
31+
Interpreter.interpret(
32+
Parser.parse(pattern),
33+
{ :locale => locale.to_sym }
34+
).call(args)
2735
end
2836

2937
end

lib/message_format/interpreter.rb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize ( options=nil )
2626
end
2727

2828
def interpret ( elements )
29-
return interpret_subs(elements)
29+
interpret_subs(elements)
3030
end
3131

3232
def interpret_subs ( elements, parent=nil )
@@ -39,14 +39,14 @@ def interpret_subs ( elements, parent=nil )
3939
return elements[0]
4040
end
4141

42-
return lambda do |args|
42+
lambda do |args|
4343
elements.map { |element| element.call(args) }.join ''
4444
end
4545
end
4646

4747
def interpret_element ( element, parent=nil )
4848
if element.is_a?(String)
49-
return lambda { |args=nil| element }
49+
return lambda { |_=nil| element }
5050
end
5151

5252
id, type, style = element
@@ -63,57 +63,57 @@ def interpret_element ( element, parent=nil )
6363

6464
case type
6565
when 'number'
66-
return interpret_number(id, offset, style)
66+
interpret_number(id, offset, style)
6767
when 'date', 'time'
68-
return interpret_date_time(id, type, style)
68+
interpret_date_time(id, type, style)
6969
when 'plural', 'selectordinal'
7070
offset = element[2]
7171
options = element[3]
72-
return interpret_plural(id, type, offset, options)
72+
interpret_plural(id, type, offset, options)
7373
when 'select'
74-
return interpret_select(id, style)
74+
interpret_select(id, style)
7575
when 'spellout', 'ordinal', 'duration'
76-
return interpret_number(id, offset, type)
76+
interpret_number(id, offset, type)
7777
else
78-
return interpret_simple(id)
78+
interpret_simple(id)
7979
end
8080
end
8181

8282
def interpret_number ( id, offset, style )
8383
locale = @locale
84-
return lambda do |args|
84+
lambda do |args|
8585
number = TwitterCldr::Localized::LocalizedNumber.new(args[id] - offset, locale)
8686
if style == 'integer'
87-
return number.to_decimal(:precision => 0).to_s
87+
number.to_decimal(:precision => 0).to_s
8888
elsif style == 'percent'
89-
return number.to_percent.to_s
89+
number.to_percent.to_s
9090
elsif style == 'currency'
91-
return number.to_currency.to_s
91+
number.to_currency.to_s
9292
elsif style == 'spellout'
93-
return number.spellout
93+
number.spellout
9494
elsif style == 'ordinal'
95-
return number.to_rbnf_s('OrdinalRules', 'digits-ordinal')
95+
number.to_rbnf_s('OrdinalRules', 'digits-ordinal')
9696
else
97-
return number.to_s
97+
number.to_s
9898
end
9999
end
100100
end
101101

102102
def interpret_date_time ( id, type, style='medium' )
103103
locale = @locale
104-
return lambda do |args|
104+
lambda do |args|
105105
datetime = TwitterCldr::Localized::LocalizedDateTime.new(args[id], locale)
106106
datetime = type == 'date' ? datetime.to_date : datetime.to_time
107107
if style == 'medium'
108-
return datetime.to_medium_s
108+
datetime.to_medium_s
109109
elsif style == 'long'
110-
return datetime.to_long_s
110+
datetime.to_long_s
111111
elsif style == 'short'
112-
return datetime.to_short_s
112+
datetime.to_short_s
113113
elsif style == 'full'
114-
return datetime.to_full_s
114+
datetime.to_full_s
115115
else
116-
return datetime.to_additional_s(style)
116+
datetime.to_additional_s(style)
117117
end
118118
end
119119
end
@@ -127,15 +127,15 @@ def interpret_plural ( id, type, offset, children )
127127

128128
locale = @locale
129129
plural_type = type == 'selectordinal' ? :ordinal : :cardinal
130-
return lambda do |args|
130+
lambda do |args|
131131
arg = args[id]
132132
exactSelector = ('=' + arg.to_s).to_sym
133133
keywordSelector = TwitterCldr::Formatters::Plurals::Rules.rule_for(arg - offset, locale, plural_type)
134134
func =
135135
options[exactSelector] ||
136136
options[keywordSelector] ||
137137
options[:other]
138-
return func.call(args)
138+
func.call(args)
139139
end
140140
end
141141

@@ -144,23 +144,21 @@ def interpret_select ( id, children )
144144
children.each do |key, value|
145145
options[key.to_sym] = interpret_subs(value, nil)
146146
end
147-
return lambda do |args|
147+
lambda do |args|
148148
selector = args[id].to_sym
149149
func =
150150
options[selector] ||
151151
options[:other]
152-
return func.call(args)
152+
func.call(args)
153153
end
154154
end
155155

156156
def interpret_simple ( id )
157-
return lambda do |args|
158-
return args[id].to_s
159-
end
157+
lambda { |args| args[id].to_s }
160158
end
161159

162160
def self.interpret ( elements, options=nil )
163-
return Interpreter.new(options).interpret(elements)
161+
Interpreter.new(options).interpret(elements)
164162
end
165163

166164
end

lib/message_format/parser.rb

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,32 @@ def parse ( pattern )
3232
@pattern = pattern
3333
@length = pattern.length
3434
@index = 0
35-
return parse_message("message")
35+
parse_message("message")
3636
end
3737

3838
def is_digit ( char )
39-
return (
40-
char == '0' or
41-
char == '1' or
42-
char == '2' or
43-
char == '3' or
44-
char == '4' or
45-
char == '5' or
46-
char == '6' or
47-
char == '7' or
48-
char == '8' or
49-
char == '9'
50-
)
39+
char == '0' or
40+
char == '1' or
41+
char == '2' or
42+
char == '3' or
43+
char == '4' or
44+
char == '5' or
45+
char == '6' or
46+
char == '7' or
47+
char == '8' or
48+
char == '9'
5149
end
5250

5351
def is_whitespace ( char )
54-
return (
55-
char == "\s" or
56-
char == "\t" or
57-
char == "\n" or
58-
char == "\r" or
59-
char == "\f" or
60-
char == "\v" or
61-
char == "\u00A0" or
62-
char == "\u2028" or
63-
char == "\u2029"
64-
)
52+
char == "\s" or
53+
char == "\t" or
54+
char == "\n" or
55+
char == "\r" or
56+
char == "\f" or
57+
char == "\v" or
58+
char == "\u00A0" or
59+
char == "\u2028" or
60+
char == "\u2029"
6561
end
6662

6763
def skip_whitespace ()
@@ -120,7 +116,7 @@ def parse_text ( parent_type )
120116
end
121117
end
122118

123-
return text
119+
text
124120
end
125121

126122
def parse_argument ()
@@ -175,7 +171,7 @@ def parse_argument ()
175171
end
176172
@index += 1 # move passed
177173

178-
return (type == 'plural' or type == 'selectordinal') ?
174+
(type == 'plural' or type == 'selectordinal') ?
179175
[ id, type, offset, format ] :
180176
[ id, type, format ]
181177
end
@@ -198,7 +194,7 @@ def parse_arg_id ()
198194
raise_expected('argument id')
199195
end
200196
skip_whitespace()
201-
return id
197+
id
202198
end
203199

204200
def parse_arg_type ()
@@ -218,7 +214,7 @@ def parse_arg_type ()
218214
raise_expected(types.join(', '))
219215
end
220216
skip_whitespace()
221-
return arg_type
217+
arg_type
222218
end
223219

224220
def parse_simple_format ()
@@ -228,7 +224,7 @@ def parse_simple_format ()
228224
raise_expected('argument style name')
229225
end
230226
skip_whitespace()
231-
return style
227+
style
232228
end
233229

234230
def parse_plural_offset ()
@@ -250,7 +246,7 @@ def parse_plural_offset ()
250246
offset = @pattern[start..@index].to_i
251247
skip_whitespace()
252248
end
253-
return offset
249+
offset
254250
end
255251

256252
def parse_sub_messages ( parent_type )
@@ -273,7 +269,7 @@ def parse_sub_messages ( parent_type )
273269
if !options.has_key?('other') # does not have an other selector
274270
raise_expected(nil, nil, '"other" option must be specified in ' + parent_type)
275271
end
276-
return options
272+
options
277273
end
278274

279275
def parse_selector ()
@@ -293,7 +289,7 @@ def parse_selector ()
293289
raise_expected('selector')
294290
end
295291
skip_whitespace()
296-
return selector
292+
selector
297293
end
298294

299295
def parse_sub_message ( parent_type )
@@ -308,7 +304,7 @@ def parse_sub_message ( parent_type )
308304
raise_expected('}')
309305
end
310306
@index += 1 # move passed }
311-
return message
307+
message
312308
end
313309

314310
def parse_message ( parent_type )
@@ -330,7 +326,7 @@ def parse_message ( parent_type )
330326
elements.push(text)
331327
end
332328
end
333-
return elements
329+
elements
334330
end
335331

336332
def raise_expected ( expected=nil, found=nil, message=nil )
@@ -349,14 +345,13 @@ def raise_expected ( expected=nil, found=nil, message=nil )
349345
end
350346

351347
def error_message ( expected=nil, found )
352-
if !expected
353-
return "Unexpected \"#{ found }\" found"
354-
end
355-
return "Expected \"#{ expected }\" but found \"#{ found }\""
348+
expected ?
349+
"Expected \"#{ expected }\" but found \"#{ found }\"" :
350+
"Unexpected \"#{ found }\" found"
356351
end
357352

358353
def self.parse ( pattern )
359-
return Parser.new().parse(pattern)
354+
Parser.new().parse(pattern)
360355
end
361356

362357
#

lib/message_format/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MessageFormat
2-
VERSION = "0.0.1"
2+
VERSION = "0.0.2"
33
end

0 commit comments

Comments
 (0)