padrino-helpers 0.10.2 → 0.10.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +3 -3
- data/.yardopts +1 -0
- data/{LICENSE → LICENSE.txt} +0 -0
- data/README.rdoc +3 -3
- data/lib/padrino-helpers.rb +21 -14
- data/lib/padrino-helpers/asset_tag_helpers.rb +163 -34
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +1 -1
- data/lib/padrino-helpers/form_helpers.rb +287 -134
- data/lib/padrino-helpers/format_helpers.rb +135 -17
- data/lib/padrino-helpers/locale/lv.yml +103 -0
- data/lib/padrino-helpers/locale/zh_cn.yml +1 -1
- data/lib/padrino-helpers/number_helpers.rb +73 -59
- data/lib/padrino-helpers/output_helpers.rb +59 -15
- data/lib/padrino-helpers/output_helpers/abstract_handler.rb +15 -20
- data/lib/padrino-helpers/output_helpers/erb_handler.rb +12 -15
- data/lib/padrino-helpers/output_helpers/haml_handler.rb +10 -14
- data/lib/padrino-helpers/output_helpers/slim_handler.rb +10 -14
- data/lib/padrino-helpers/render_helpers.rb +23 -7
- data/lib/padrino-helpers/tag_helpers.rb +41 -16
- data/lib/padrino-helpers/translation_helpers.rb +14 -0
- data/test/fixtures/markup_app/views/content_for.erb +3 -0
- data/test/fixtures/markup_app/views/content_for.haml +3 -0
- data/test/fixtures/markup_app/views/content_for.slim +3 -0
- data/test/helper.rb +3 -15
- data/test/test_asset_tag_helpers.rb +1 -1
- data/test/test_form_builder.rb +3 -5
- data/test/test_form_helpers.rb +1 -1
- data/test/test_format_helpers.rb +1 -1
- data/test/test_locale.rb +1 -1
- data/test/test_number_helpers.rb +1 -1
- data/test/test_output_helpers.rb +22 -2
- data/test/test_render_helpers.rb +1 -1
- data/test/test_tag_helpers.rb +1 -1
- metadata +10 -8
@@ -4,6 +4,16 @@ module Padrino
|
|
4
4
|
##
|
5
5
|
# Returns escaped text to protect against malicious content
|
6
6
|
#
|
7
|
+
# @param [String] text
|
8
|
+
# Unsanitized HTML string that needs to be escaped.
|
9
|
+
#
|
10
|
+
# @return [String] HTML with escaped characters.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# escape_html("<b>Hey<b>") => "<b>Hey<b;gt;"
|
14
|
+
# h("Me & Bob") => "Me & Bob"
|
15
|
+
#
|
16
|
+
# @api public
|
7
17
|
def escape_html(text)
|
8
18
|
Rack::Utils.escape_html(text)
|
9
19
|
end
|
@@ -14,6 +24,18 @@ module Padrino
|
|
14
24
|
# Returns escaped text to protect against malicious content
|
15
25
|
# Returns blank if the text is empty
|
16
26
|
#
|
27
|
+
# @param [String] text
|
28
|
+
# Unsanitized HTML string that needs to be escaped.
|
29
|
+
# @param [String] blank_text
|
30
|
+
# Text to return if escaped text is blank.
|
31
|
+
#
|
32
|
+
# @return [String] HTML with escaped characters or the value specified if blank.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# h!("Me & Bob") => "Me & Bob"
|
36
|
+
# h!("", "Whoops") => "Whoops"
|
37
|
+
#
|
38
|
+
# @api public
|
17
39
|
def h!(text, blank_text = ' ')
|
18
40
|
return blank_text if text.nil? || text.empty?
|
19
41
|
h text
|
@@ -22,6 +44,15 @@ module Padrino
|
|
22
44
|
##
|
23
45
|
# Strips all HTML tags from the html
|
24
46
|
#
|
47
|
+
# @param [String] html
|
48
|
+
# The HTML for which to strip tags.
|
49
|
+
#
|
50
|
+
# @return [String] HTML with tags stripped.
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
# strip_tags("<b>Hey</b>") => "Hey"
|
54
|
+
#
|
55
|
+
# @api public
|
25
56
|
def strip_tags(html)
|
26
57
|
html.gsub(/<\/?[^>]*>/, "") if html
|
27
58
|
end
|
@@ -31,11 +62,20 @@ module Padrino
|
|
31
62
|
# as a paragraph and wrapped in <p> or your own tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended.
|
32
63
|
# This method does not remove the newlines from the text.
|
33
64
|
#
|
34
|
-
#
|
65
|
+
# @param [String] text
|
66
|
+
# The simple text to be formatted.
|
67
|
+
# @param [Hash] options
|
68
|
+
# Formatting options for the text. Can accept html options for the wrapper tag.
|
69
|
+
# @option options [Symbol] :tag (p)
|
70
|
+
# The html tag to use for formatting newlines.
|
71
|
+
#
|
72
|
+
# @return [String] The text formatted as simple HTML.
|
35
73
|
#
|
74
|
+
# @example
|
36
75
|
# simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
37
76
|
# simple_format("hello\nworld", :tag => :div, :class => :foo) # => "<div class="foo">hello<br/>world</div>"
|
38
77
|
#
|
78
|
+
# @api public
|
39
79
|
def simple_format(text, options={})
|
40
80
|
t = options.delete(:tag) || :p
|
41
81
|
start_tag = tag(t, options.merge(:open => true))
|
@@ -51,10 +91,19 @@ module Padrino
|
|
51
91
|
# Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1,
|
52
92
|
# otherwise it will use the Inflector to determine the plural form
|
53
93
|
#
|
54
|
-
#
|
94
|
+
# @param [Fixnum] count
|
95
|
+
# The count which determines pluralization.
|
96
|
+
# @param [String] singular
|
97
|
+
# The word to be pluralized if appropriate based on +count+.
|
98
|
+
# @param [String] plural
|
99
|
+
# Explicit pluralized word to be used; if not specified uses inflector.
|
55
100
|
#
|
101
|
+
# @return [String] The properly pluralized word.
|
102
|
+
#
|
103
|
+
# @example
|
56
104
|
# pluralize(2, 'person') => '2 people'
|
57
105
|
#
|
106
|
+
# @api public
|
58
107
|
def pluralize(count, singular, plural = nil)
|
59
108
|
"#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
|
60
109
|
end
|
@@ -63,10 +112,21 @@ module Padrino
|
|
63
112
|
# Truncates a given text after a given :length if text is longer than :length (defaults to 30).
|
64
113
|
# The last characters will be replaced with the :omission (defaults to "…") for a total length not exceeding :length.
|
65
114
|
#
|
66
|
-
#
|
115
|
+
# @param [String] text
|
116
|
+
# The text to be truncated.
|
117
|
+
# @param [Hash] options
|
118
|
+
# Formatting options for the truncation.
|
119
|
+
# @option options [Fixnum] :length (30)
|
120
|
+
# The number of characters before truncation occurs.
|
121
|
+
# @option options [String] :omission ("...")
|
122
|
+
# The characters that are placed after the truncated text.
|
123
|
+
#
|
124
|
+
# @return [String] The text truncated after the given number of characters.
|
67
125
|
#
|
126
|
+
# @example
|
68
127
|
# truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
|
69
128
|
#
|
129
|
+
# @api public
|
70
130
|
def truncate(text, options={})
|
71
131
|
options.reverse_merge!(:length => 30, :omission => "...")
|
72
132
|
if text
|
@@ -75,15 +135,26 @@ module Padrino
|
|
75
135
|
(chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s
|
76
136
|
end
|
77
137
|
end
|
78
|
-
|
138
|
+
|
79
139
|
##
|
80
140
|
# Truncates words of a given text after a given :length if number of words in text is more than :length (defaults to 30).
|
81
141
|
# The last words will be replaced with the :omission (defaults to "…") for a total number of words not exceeding :length.
|
82
142
|
#
|
83
|
-
#
|
143
|
+
# @param [String] text
|
144
|
+
# The text to be truncated.
|
145
|
+
# @param [Hash] options
|
146
|
+
# Formatting options for the truncation.
|
147
|
+
# @option options [Fixnum] :length (30)
|
148
|
+
# The number of words before truncation occurs.
|
149
|
+
# @option options [String] :omission ("...")
|
150
|
+
# The characters that are placed after the truncated text.
|
84
151
|
#
|
152
|
+
# @return [String] The text truncated after the given number of words.
|
153
|
+
#
|
154
|
+
# @example
|
85
155
|
# truncate_words("Once upon a time in a world far far away", :length => 8) => "Once upon a time in a world far..."
|
86
156
|
#
|
157
|
+
# @api public
|
87
158
|
def truncate_words(text, options={})
|
88
159
|
options.reverse_merge!(:length => 30, :omission => "...")
|
89
160
|
if text
|
@@ -96,10 +167,20 @@ module Padrino
|
|
96
167
|
# Wraps the text into lines no longer than line_width width.
|
97
168
|
# This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
|
98
169
|
#
|
99
|
-
#
|
170
|
+
# @overload word_wrap(text, options={})
|
171
|
+
# @param [String] text
|
172
|
+
# The text to be wrapped.
|
173
|
+
# @param [Hash] options
|
174
|
+
# Formatting options for the wrapping.
|
175
|
+
# @option options [Fixnum] :line_width (80)
|
176
|
+
# The line width before a wrap should occur.
|
177
|
+
#
|
178
|
+
# @return [String] The text with line wraps for lines longer then +line_width+
|
100
179
|
#
|
180
|
+
# @example
|
101
181
|
# word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
|
102
182
|
#
|
183
|
+
# @api public
|
103
184
|
def word_wrap(text, *args)
|
104
185
|
options = args.extract_options!
|
105
186
|
unless args.blank?
|
@@ -116,16 +197,28 @@ module Padrino
|
|
116
197
|
# Highlights one or more words everywhere in text by inserting it into a :highlighter string.
|
117
198
|
#
|
118
199
|
# The highlighter can be customized by passing :+highlighter+ as a single-quoted string
|
119
|
-
# with \1 where the phrase is to be inserted
|
200
|
+
# with \1 where the phrase is to be inserted.
|
120
201
|
#
|
121
|
-
#
|
202
|
+
# @overload highlight(text, words, options={})
|
203
|
+
# @param [String] text
|
204
|
+
# The text that will be searched.
|
205
|
+
# @param [String] words
|
206
|
+
# The words to be highlighted in the +text+.
|
207
|
+
# @param [Hash] options
|
208
|
+
# Formatting options for the highlight.
|
209
|
+
# @option options [String] :highlighter (’<strong class="highlight">\1</strong>’)
|
210
|
+
# The html pattern for wrapping the highlighted words.
|
122
211
|
#
|
123
|
-
#
|
212
|
+
# @return [String] The text with the words specified wrapped with highlighted spans.
|
213
|
+
#
|
214
|
+
# @example
|
124
215
|
# highlight('Lorem ipsum dolor sit amet', 'dolor')
|
216
|
+
# # => Lorem ipsum <strong class="highlight">dolor</strong> sit amet
|
125
217
|
#
|
126
|
-
# # => Lorem ipsum <span class="custom">dolor</span> sit amet
|
127
218
|
# highlight('Lorem ipsum dolor sit amet', 'dolor', :highlighter => '<span class="custom">\1</span>')
|
219
|
+
# # => Lorem ipsum <strong class="custom">dolor</strong> sit amet
|
128
220
|
#
|
221
|
+
# @api public
|
129
222
|
def highlight(text, words, *args)
|
130
223
|
options = args.extract_options!
|
131
224
|
options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
|
@@ -140,7 +233,7 @@ module Padrino
|
|
140
233
|
|
141
234
|
##
|
142
235
|
# Reports the approximate distance in time between two Time or Date objects or integers as seconds.
|
143
|
-
# Set
|
236
|
+
# Set +include_seconds+ to true if you want more detailed approximations when distance < 1 min, 29 secs
|
144
237
|
# Distances are reported based on the following table:
|
145
238
|
#
|
146
239
|
# 0 <-> 29 secs # => less than a minute
|
@@ -157,7 +250,7 @@ module Padrino
|
|
157
250
|
# 1 yr, 9 months <-> 2 yr minus 1 sec # => almost 2 years
|
158
251
|
# 2 yrs <-> max time or date # => (same rules as 1 yr)
|
159
252
|
#
|
160
|
-
# With
|
253
|
+
# With +include_seconds+ = true and the difference < 1 minute 29 seconds:
|
161
254
|
# 0-4 secs # => less than 5 seconds
|
162
255
|
# 5-9 secs # => less than 10 seconds
|
163
256
|
# 10-19 secs # => less than 20 seconds
|
@@ -165,8 +258,20 @@ module Padrino
|
|
165
258
|
# 40-59 secs # => less than a minute
|
166
259
|
# 60-89 secs # => 1 minute
|
167
260
|
#
|
168
|
-
#
|
261
|
+
# @param [Time] from_time
|
262
|
+
# The time to be compared against +to_time+ in order to approximate the distance.
|
263
|
+
# @param [Time] to_time
|
264
|
+
# The time to be compared against +from_time+ in order to approximate the distance.
|
265
|
+
# @param [Boolean] include_seconds
|
266
|
+
# Set true for more detailed approximations.
|
267
|
+
# @param [Hash] options
|
268
|
+
# Flags for the approximation.
|
269
|
+
# @option options [String] :locale
|
270
|
+
# The translation locale to be used for approximating the time.
|
271
|
+
#
|
272
|
+
# @return [String] The time formatted as a relative string.
|
169
273
|
#
|
274
|
+
# @example
|
170
275
|
# from_time = Time.now
|
171
276
|
# distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
|
172
277
|
# distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
|
@@ -180,12 +285,12 @@ module Padrino
|
|
180
285
|
# distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
|
181
286
|
# distance_of_time_in_words(from_time, from_time + 3.years + 6.months) # => over 3 years
|
182
287
|
# distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => about 4 years
|
183
|
-
#
|
184
288
|
# to_time = Time.now + 6.years + 19.days
|
185
289
|
# distance_of_time_in_words(from_time, to_time, true) # => about 6 years
|
186
290
|
# distance_of_time_in_words(to_time, from_time, true) # => about 6 years
|
187
291
|
# distance_of_time_in_words(Time.now, Time.now) # => less than a minute
|
188
292
|
#
|
293
|
+
# @api public
|
189
294
|
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
|
190
295
|
from_time = from_time.to_time if from_time.respond_to?(:to_time)
|
191
296
|
to_time = to_time.to_time if to_time.respond_to?(:to_time)
|
@@ -233,14 +338,19 @@ module Padrino
|
|
233
338
|
##
|
234
339
|
# Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
|
235
340
|
#
|
236
|
-
#
|
341
|
+
# @param [Time] from_time
|
342
|
+
# The time to be compared against now in order to approximate the distance.
|
343
|
+
# @param [Boolean] include_seconds
|
344
|
+
# Set true for more detailed approximations.
|
345
|
+
#
|
346
|
+
# @return [String] The time formatted as a relative string.
|
237
347
|
#
|
348
|
+
# @example
|
238
349
|
# time_ago_in_words(3.minutes.from_now) # => 3 minutes
|
239
350
|
# time_ago_in_words(Time.now - 15.hours) # => 15 hours
|
240
351
|
# time_ago_in_words(Time.now) # => less than a minute
|
241
352
|
#
|
242
|
-
#
|
243
|
-
#
|
353
|
+
# @api public
|
244
354
|
def time_ago_in_words(from_time, include_seconds = false)
|
245
355
|
distance_of_time_in_words(from_time, Time.now, include_seconds)
|
246
356
|
end
|
@@ -248,13 +358,21 @@ module Padrino
|
|
248
358
|
##
|
249
359
|
# Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino
|
250
360
|
#
|
361
|
+
# @param [String] html
|
362
|
+
# The html content to be escaped into javascript compatible format.
|
363
|
+
#
|
364
|
+
# @return [String] The html escaped for javascript passing.
|
365
|
+
#
|
366
|
+
# @example
|
251
367
|
# js_escape_html("<h1>Hey</h1>")
|
252
368
|
#
|
369
|
+
# @api public
|
253
370
|
def js_escape_html(html_content)
|
254
371
|
return '' unless html_content
|
255
372
|
javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
|
256
373
|
html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
|
257
374
|
end
|
375
|
+
|
258
376
|
end # FormatHelpers
|
259
377
|
end # Helpers
|
260
378
|
end # Padrino
|
@@ -0,0 +1,103 @@
|
|
1
|
+
lv:
|
2
|
+
number:
|
3
|
+
# Used in number_with_delimiter()
|
4
|
+
# These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
|
5
|
+
format:
|
6
|
+
# Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
|
7
|
+
separator: ","
|
8
|
+
# Delimets thousands (e.g. 1,000,000 is a million) (always in groups of three)
|
9
|
+
delimiter: "."
|
10
|
+
# Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
|
11
|
+
precision: 2
|
12
|
+
|
13
|
+
# Used in number_to_currency()
|
14
|
+
currency:
|
15
|
+
format:
|
16
|
+
# Where is the currency sign? %u is the currency unit, %n the number (default: $5.00)
|
17
|
+
format: "%u %n"
|
18
|
+
unit: "LVL"
|
19
|
+
# These three are to override number.format and are optional
|
20
|
+
separator: ","
|
21
|
+
delimiter: "."
|
22
|
+
precision: 2
|
23
|
+
|
24
|
+
# Used in number_to_percentage()
|
25
|
+
percentage:
|
26
|
+
format:
|
27
|
+
# These three are to override number.format and are optional
|
28
|
+
# separator:
|
29
|
+
delimiter: ""
|
30
|
+
# precision:
|
31
|
+
|
32
|
+
# Used in number_to_precision()
|
33
|
+
precision:
|
34
|
+
format:
|
35
|
+
# These three are to override number.format and are optional
|
36
|
+
# separator:
|
37
|
+
delimiter: ""
|
38
|
+
# precision:
|
39
|
+
|
40
|
+
# Used in number_to_human_size()
|
41
|
+
human:
|
42
|
+
format:
|
43
|
+
# These three are to override number.format and are optional
|
44
|
+
# separator:
|
45
|
+
delimiter: ""
|
46
|
+
precision: 1
|
47
|
+
storage_units:
|
48
|
+
# Storage units output formatting.
|
49
|
+
# %u is the storage unit, %n is the number (default: 2 MB)
|
50
|
+
format: "%n %u"
|
51
|
+
units:
|
52
|
+
byte:
|
53
|
+
one: "Baits"
|
54
|
+
other: "Baiti"
|
55
|
+
kb: "KB"
|
56
|
+
mb: "MB"
|
57
|
+
gb: "GB"
|
58
|
+
tb: "TB"
|
59
|
+
|
60
|
+
# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
|
61
|
+
datetime:
|
62
|
+
distance_in_words:
|
63
|
+
half_a_minute: "pusminūte"
|
64
|
+
less_than_x_seconds:
|
65
|
+
one: "mazāk par vienu sekundi"
|
66
|
+
other: "mazāk par %{count} sekundēm"
|
67
|
+
x_seconds:
|
68
|
+
one: "1 sekunde"
|
69
|
+
other: "%{count} sekundes"
|
70
|
+
less_than_x_minutes:
|
71
|
+
one: "mazāk par vienu minūti"
|
72
|
+
other: "mazāk par %{count} minūtēm"
|
73
|
+
x_minutes:
|
74
|
+
one: "1 minūte"
|
75
|
+
other: "%{count} minūtes"
|
76
|
+
about_x_hours:
|
77
|
+
one: "apmēram 1 stunda"
|
78
|
+
other: "apmēram %{count} stundas"
|
79
|
+
x_days:
|
80
|
+
one: "1 diena"
|
81
|
+
other: "%{count} dienas"
|
82
|
+
about_x_months:
|
83
|
+
one: "apmēram 1 mēnesis"
|
84
|
+
other: "apmēram %{count} mēneši"
|
85
|
+
x_months:
|
86
|
+
one: "1 mēnesis"
|
87
|
+
other: "%{count} mēneši"
|
88
|
+
about_x_years:
|
89
|
+
one: "apmēram 1 gads"
|
90
|
+
other: "apmēram %{count} gadi"
|
91
|
+
over_x_years:
|
92
|
+
one: "vairāk kā gads"
|
93
|
+
other: "vairāk kā %{count} gadi"
|
94
|
+
almost_x_years:
|
95
|
+
one: "gandrīz 1 gads"
|
96
|
+
other: "gandrīz %{count} gadi"
|
97
|
+
models:
|
98
|
+
errors:
|
99
|
+
template:
|
100
|
+
header:
|
101
|
+
one: "Dēļ 1 kļūdas šis %{model} netika saglabāts"
|
102
|
+
other: "Dēļ %{count} kļūdām šis %{model} netika saglabāts"
|
103
|
+
body: "Problēmas ir šajos ievades laukos:"
|
@@ -57,7 +57,7 @@ zh_cn:
|
|
57
57
|
gb: "GB"
|
58
58
|
tb: "TB"
|
59
59
|
|
60
|
-
# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
|
60
|
+
# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
|
61
61
|
datetime:
|
62
62
|
distance_in_words:
|
63
63
|
half_a_minute: "半分钟"
|
@@ -5,35 +5,42 @@ module Padrino
|
|
5
5
|
# Methods are provided for phone numbers, currency, percentage,
|
6
6
|
# precision, positional notation, and file size.
|
7
7
|
#
|
8
|
-
#
|
8
|
+
# Adapted from Rails Number Helpers.
|
9
9
|
#
|
10
10
|
module NumberHelpers
|
11
11
|
##
|
12
12
|
# Formats a +number+ into a currency string (e.g., $13.65). You can customize the format
|
13
13
|
# in the +options+ hash.
|
14
14
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
15
|
+
# @param [Float] number
|
16
|
+
# Currency value to format.
|
17
|
+
# @param [Hash] options
|
18
|
+
# Options for currency conversion.
|
19
|
+
# @option options [Fixnum] :precision (2)
|
20
|
+
# Sets the level of precision.
|
21
|
+
# @option options [String] :unit ("$")
|
22
|
+
# Sets the denomination of the currency.
|
23
|
+
# @option options [String] :separator (".")
|
24
|
+
# Sets the separator between the units.
|
25
|
+
# @option options [String] :delimiter (",")
|
26
|
+
# Sets the thousands delimiter.
|
27
|
+
# @option options [String] :format ("%u%n")
|
28
|
+
# Sets the format of the output string. The field types are:
|
23
29
|
# %u The currency unit
|
24
30
|
# %n The number
|
25
31
|
#
|
26
|
-
#
|
32
|
+
# @return [String] The formatted representation of the currency
|
27
33
|
#
|
34
|
+
# @example
|
28
35
|
# number_to_currency(1234567890.50) # => $1,234,567,890.50
|
29
36
|
# number_to_currency(1234567890.506) # => $1,234,567,890.51
|
30
37
|
# number_to_currency(1234567890.506, :precision => 3) # => $1,234,567,890.506
|
31
|
-
#
|
32
38
|
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "")
|
33
39
|
# # => £1234567890,50
|
34
40
|
# number_to_currency(1234567890.50, :unit => "£", :separator => ",", :delimiter => "", :format => "%n %u")
|
35
41
|
# # => 1234567890,50 £
|
36
42
|
#
|
43
|
+
# @api public
|
37
44
|
def number_to_currency(number, options = {})
|
38
45
|
options.symbolize_keys!
|
39
46
|
|
@@ -63,19 +70,26 @@ module Padrino
|
|
63
70
|
# Formats a +number+ as a percentage string (e.g., 65%). You can customize the
|
64
71
|
# format in the +options+ hash.
|
65
72
|
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
73
|
+
# @param [Fixnum, Float] number
|
74
|
+
# Percentage value to format.
|
75
|
+
# @param [Hash] options
|
76
|
+
# Options for percentage conversion.
|
77
|
+
# @option options [Fixnum] :precision (3)
|
78
|
+
# Sets the level of precision.
|
79
|
+
# @option options [String] :separator (".")
|
80
|
+
# Sets the separator between the units.
|
81
|
+
# @option options [String] :delimiter ("")
|
82
|
+
# Sets the thousands delimiter
|
71
83
|
#
|
72
|
-
#
|
84
|
+
# @return [String] The formatted representation of the percentage
|
73
85
|
#
|
86
|
+
# @example
|
74
87
|
# number_to_percentage(100) # => 100.000%
|
75
88
|
# number_to_percentage(100, :precision => 0) # => 100%
|
76
89
|
# number_to_percentage(1000, :delimiter => '.', :separator => ',') # => 1.000,000%
|
77
90
|
# number_to_percentage(302.24398923423, :precision => 5) # => 302.24399%
|
78
91
|
#
|
92
|
+
# @api public
|
79
93
|
def number_to_percentage(number, options = {})
|
80
94
|
options.symbolize_keys!
|
81
95
|
|
@@ -101,13 +115,19 @@ module Padrino
|
|
101
115
|
# Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
|
102
116
|
# customize the format in the +options+ hash.
|
103
117
|
#
|
104
|
-
#
|
118
|
+
# @overload number_with_delimiter(number, options={})
|
119
|
+
# @param [Fixnum, Float] number
|
120
|
+
# Number value to format.
|
121
|
+
# @param [Hash] options
|
122
|
+
# Options for formatter.
|
123
|
+
# @option options [String] :delimiter (", ")
|
124
|
+
# Sets the thousands delimiter
|
125
|
+
# @option options [String] :separator (".")
|
126
|
+
# Sets the separator between the units.
|
105
127
|
#
|
106
|
-
#
|
107
|
-
# :separator:: Sets the separator between the units (defaults to ".").
|
108
|
-
#
|
109
|
-
# ==== Examples
|
128
|
+
# @return [String] The formatted representation of the number
|
110
129
|
#
|
130
|
+
# @example
|
111
131
|
# number_with_delimiter(12345678) # => 12,345,678
|
112
132
|
# number_with_delimiter(12345678.05) # => 12,345,678.05
|
113
133
|
# number_with_delimiter(12345678, :delimiter => ".") # => 12.345.678
|
@@ -115,13 +135,7 @@ module Padrino
|
|
115
135
|
# number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
116
136
|
# # => 98 765 432,98
|
117
137
|
#
|
118
|
-
#
|
119
|
-
# +delimiter+ as its optional second and the +separator+ as its
|
120
|
-
# optional third parameter:
|
121
|
-
#
|
122
|
-
# number_with_delimiter(12345678, " ") # => 12 345.678
|
123
|
-
# number_with_delimiter(12345678.05, ".", ",") # => 12.345.678,05
|
124
|
-
#
|
138
|
+
# @api public
|
125
139
|
def number_with_delimiter(number, *args)
|
126
140
|
options = args.extract_options!
|
127
141
|
options.symbolize_keys!
|
@@ -144,14 +158,21 @@ module Padrino
|
|
144
158
|
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
145
159
|
# You can customize the format in the +options+ hash.
|
146
160
|
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
161
|
+
# @overload number_with_precision(number, options={})
|
162
|
+
# @param [Fixnum, Float] number
|
163
|
+
# Number value to format.
|
164
|
+
# @param [Hash] options
|
165
|
+
# Options for formatter.
|
166
|
+
# @option options [Fixnum] :precision (3)
|
167
|
+
# Sets the level of precision.
|
168
|
+
# @option options [String] :separator (".")
|
169
|
+
# Sets the separator between the units.
|
170
|
+
# @option options [String] :delimiter ("")
|
171
|
+
# Sets the thousands delimiter
|
172
|
+
#
|
173
|
+
# @return [String] The formatted representation of the number
|
174
|
+
#
|
175
|
+
# @example
|
155
176
|
# number_with_precision(111.2345) # => 111.235
|
156
177
|
# number_with_precision(111.2345, :precision => 2) # => 111.23
|
157
178
|
# number_with_precision(13, :precision => 5) # => 13.00000
|
@@ -159,11 +180,7 @@ module Padrino
|
|
159
180
|
# number_with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
160
181
|
# # => 1.111,23
|
161
182
|
#
|
162
|
-
#
|
163
|
-
# +precision+ as its optional second parameter:
|
164
|
-
#
|
165
|
-
# number_with_precision(number_with_precision(111.2345, 2) # => 111.23
|
166
|
-
#
|
183
|
+
# @api public
|
167
184
|
def number_with_precision(number, *args)
|
168
185
|
options = args.extract_options!
|
169
186
|
options.symbolize_keys!
|
@@ -196,14 +213,22 @@ module Padrino
|
|
196
213
|
# +size+ cannot be converted into a number. You can customize the
|
197
214
|
# format in the +options+ hash.
|
198
215
|
#
|
199
|
-
# ==== Options
|
200
216
|
#
|
201
|
-
#
|
202
|
-
#
|
203
|
-
#
|
217
|
+
# @overload number_to_human_size(number, options={})
|
218
|
+
# @param [Fixnum] number
|
219
|
+
# Number value to format.
|
220
|
+
# @param [Hash] options
|
221
|
+
# Options for formatter.
|
222
|
+
# @option options [Fixnum] :precision (1)
|
223
|
+
# Sets the level of precision.
|
224
|
+
# @option options [String] :separator (".")
|
225
|
+
# Sets the separator between the units.
|
226
|
+
# @option options [String] :delimiter ("")
|
227
|
+
# Sets the thousands delimiter
|
204
228
|
#
|
205
|
-
#
|
229
|
+
# @return [String] The formatted representation of bytes
|
206
230
|
#
|
231
|
+
# @example
|
207
232
|
# number_to_human_size(123) # => 123 Bytes
|
208
233
|
# number_to_human_size(1234) # => 1.2 KB
|
209
234
|
# number_to_human_size(12345) # => 12.1 KB
|
@@ -214,18 +239,7 @@ module Padrino
|
|
214
239
|
# number_to_human_size(483989, :precision => 0) # => 473 KB
|
215
240
|
# number_to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
|
216
241
|
#
|
217
|
-
#
|
218
|
-
# specified precision:
|
219
|
-
#
|
220
|
-
# helper.number_to_human_size(1234567890123, :precision => 5) # => "1.12283 TB"
|
221
|
-
# helper.number_to_human_size(524288000, :precision=>5) # => "500 MB"
|
222
|
-
#
|
223
|
-
# You can still use <tt>number_to_human_size</tt> with the old API that accepts the
|
224
|
-
# +precision+ as its optional second parameter:
|
225
|
-
#
|
226
|
-
# number_to_human_size(1234567, 2) # => 1.18 MB
|
227
|
-
# number_to_human_size(483989, 0) # => 473 KB
|
228
|
-
#
|
242
|
+
# @api public
|
229
243
|
def number_to_human_size(number, *args)
|
230
244
|
return nil if number.nil?
|
231
245
|
|