padrino-helpers 0.16.0.pre4 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +29 -29
- data/Rakefile +1 -1
- data/lib/padrino/rendering/erb_template.rb +7 -7
- data/lib/padrino/rendering/erubi_template.rb +4 -4
- data/lib/padrino/rendering/erubis_template.rb +4 -4
- data/lib/padrino/rendering/haml_template.rb +1 -1
- data/lib/padrino/rendering/hamlit_template.rb +4 -4
- data/lib/padrino/rendering/slim_template.rb +5 -5
- data/lib/padrino/rendering.rb +54 -45
- data/lib/padrino/safe_buffer.rb +31 -36
- data/lib/padrino-helpers/asset_tag_helpers.rb +48 -48
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +74 -70
- data/lib/padrino-helpers/form_builder/standard_form_builder.rb +20 -20
- data/lib/padrino-helpers/form_helpers/errors.rb +14 -14
- data/lib/padrino-helpers/form_helpers/options.rb +25 -24
- data/lib/padrino-helpers/form_helpers/security.rb +5 -5
- data/lib/padrino-helpers/form_helpers.rb +133 -133
- data/lib/padrino-helpers/format_helpers.rb +61 -63
- data/lib/padrino-helpers/number_helpers.rb +82 -78
- data/lib/padrino-helpers/output_helpers/abstract_handler.rb +5 -3
- data/lib/padrino-helpers/output_helpers/erb_handler.rb +2 -2
- data/lib/padrino-helpers/output_helpers/haml_handler.rb +1 -1
- data/lib/padrino-helpers/output_helpers.rb +16 -17
- data/lib/padrino-helpers/render_helpers.rb +28 -27
- data/lib/padrino-helpers/tag_helpers.rb +62 -58
- data/lib/padrino-helpers/translation_helpers.rb +2 -2
- data/lib/padrino-helpers.rb +1 -2
- data/padrino-helpers.gemspec +17 -17
- data/test/fixtures/apps/render.rb +2 -2
- data/test/fixtures/markup_app/app.rb +9 -8
- data/test/fixtures/markup_app/views/button_to.erb +3 -3
- data/test/fixtures/markup_app/views/button_to.haml +2 -2
- data/test/fixtures/markup_app/views/button_to.slim +3 -3
- data/test/fixtures/markup_app/views/content_for.erb +4 -4
- data/test/fixtures/markup_app/views/content_for.haml +4 -4
- data/test/fixtures/markup_app/views/content_for.slim +2 -2
- data/test/fixtures/markup_app/views/content_tag.erb +3 -3
- data/test/fixtures/markup_app/views/content_tag.haml +3 -3
- data/test/fixtures/markup_app/views/content_tag.slim +3 -3
- data/test/fixtures/markup_app/views/fields_for.erb +2 -2
- data/test/fixtures/markup_app/views/fields_for.haml +2 -2
- data/test/fixtures/markup_app/views/fields_for.slim +2 -2
- data/test/fixtures/markup_app/views/form_for.erb +41 -41
- data/test/fixtures/markup_app/views/form_for.haml +41 -41
- data/test/fixtures/markup_app/views/form_for.slim +41 -42
- data/test/fixtures/markup_app/views/form_tag.erb +47 -47
- data/test/fixtures/markup_app/views/form_tag.haml +48 -48
- data/test/fixtures/markup_app/views/form_tag.slim +49 -49
- data/test/fixtures/markup_app/views/link_to.erb +2 -2
- data/test/fixtures/markup_app/views/link_to.haml +2 -2
- data/test/fixtures/markup_app/views/link_to.slim +2 -2
- data/test/fixtures/markup_app/views/meta_tag.erb +2 -2
- data/test/fixtures/markup_app/views/meta_tag.haml +2 -2
- data/test/fixtures/markup_app/views/meta_tag.slim +2 -2
- data/test/fixtures/markup_app/views/simple_partial.erb +1 -1
- data/test/fixtures/markup_app/views/simple_partial.haml +1 -1
- data/test/fixtures/markup_app/views/simple_partial.slim +1 -1
- data/test/fixtures/render_app/app.rb +13 -12
- data/test/fixtures/render_app/views/_unsafe.html.builder +1 -1
- data/test/fixtures/render_app/views/_unsafe_object.html.builder +1 -1
- data/test/fixtures/render_app/views/explicit_engine.haml +3 -3
- data/test/helper.rb +19 -19
- data/test/test_asset_tag_helpers.rb +133 -133
- data/test/test_form_builder.rb +629 -628
- data/test/test_form_helpers.rb +610 -610
- data/test/test_format_helpers.rb +66 -63
- data/test/test_helpers.rb +2 -2
- data/test/test_locale.rb +13 -11
- data/test/test_number_helpers.rb +65 -65
- data/test/test_output_helpers.rb +76 -76
- data/test/test_render_helpers.rb +101 -101
- data/test/test_rendering.rb +329 -326
- data/test/test_rendering_extensions.rb +4 -4
- data/test/test_tag_helpers.rb +55 -55
- metadata +6 -6
|
@@ -53,7 +53,7 @@ module Padrino
|
|
|
53
53
|
# strip_tags("<b>Hey</b>") => "Hey"
|
|
54
54
|
#
|
|
55
55
|
def strip_tags(html)
|
|
56
|
-
html
|
|
56
|
+
html&.gsub(%r{</?[^>]*>}, '')
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
##
|
|
@@ -72,9 +72,9 @@ module Padrino
|
|
|
72
72
|
#
|
|
73
73
|
# @example
|
|
74
74
|
# simple_format("hello\nworld") # => "<p>hello<br/>world</p>"
|
|
75
|
-
# simple_format("hello\nworld", :
|
|
75
|
+
# simple_format("hello\nworld", tag: :div, class: :foo) # => "<div class="foo">hello<br/>world</div>"
|
|
76
76
|
#
|
|
77
|
-
def simple_format(text, options={})
|
|
77
|
+
def simple_format(text, options = {})
|
|
78
78
|
t = options.delete(:tag) || :p
|
|
79
79
|
start_tag = tag(t, options, true)
|
|
80
80
|
text = escape_html(text.to_s.dup) unless text.html_safe?
|
|
@@ -103,7 +103,7 @@ module Padrino
|
|
|
103
103
|
# pluralize(2, 'person') => '2 people'
|
|
104
104
|
#
|
|
105
105
|
def pluralize(count, singular, plural = nil)
|
|
106
|
-
"#{count || 0} " + (
|
|
106
|
+
"#{count || 0} " + ([1, '1'].include?(count) ? singular : (plural || singular.pluralize))
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
##
|
|
@@ -122,15 +122,15 @@ module Padrino
|
|
|
122
122
|
# @return [String] The text truncated after the given number of characters.
|
|
123
123
|
#
|
|
124
124
|
# @example
|
|
125
|
-
# truncate(
|
|
126
|
-
#
|
|
127
|
-
def truncate(text, options={})
|
|
128
|
-
options = { :
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
125
|
+
# truncate('Once upon a time in a world far far away', length: 8) => "Once upon..."
|
|
126
|
+
#
|
|
127
|
+
def truncate(text, options = {})
|
|
128
|
+
options = { length: 30, omission: '...' }.update(options)
|
|
129
|
+
return unless text
|
|
130
|
+
|
|
131
|
+
len = options[:length] - options[:omission].length
|
|
132
|
+
chars = text
|
|
133
|
+
(chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s
|
|
134
134
|
end
|
|
135
135
|
|
|
136
136
|
##
|
|
@@ -149,14 +149,14 @@ module Padrino
|
|
|
149
149
|
# @return [String] The text truncated after the given number of words.
|
|
150
150
|
#
|
|
151
151
|
# @example
|
|
152
|
-
# truncate_words(
|
|
152
|
+
# truncate_words('Once upon a time in a world far far away', length: 8) => "Once upon a time in a world far..."
|
|
153
153
|
#
|
|
154
|
-
def truncate_words(text, options={})
|
|
155
|
-
options = { :
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
154
|
+
def truncate_words(text, options = {})
|
|
155
|
+
options = { length: 30, omission: '...' }.update(options)
|
|
156
|
+
return unless text
|
|
157
|
+
|
|
158
|
+
words = text.split
|
|
159
|
+
words[0..(options[:length] - 1)].join(' ') + (words.length > options[:length] ? options[:omission] : '')
|
|
160
160
|
end
|
|
161
161
|
|
|
162
162
|
##
|
|
@@ -174,14 +174,12 @@ module Padrino
|
|
|
174
174
|
# @return [String] The text with line wraps for lines longer then +line_width+.
|
|
175
175
|
#
|
|
176
176
|
# @example
|
|
177
|
-
# word_wrap('Once upon a time', :
|
|
177
|
+
# word_wrap('Once upon a time', line_width: 8) => "Once upon\na time"
|
|
178
178
|
#
|
|
179
179
|
def word_wrap(text, *args)
|
|
180
180
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
181
|
-
unless args.empty?
|
|
182
|
-
|
|
183
|
-
end
|
|
184
|
-
options = { :line_width => 80 }.update(options)
|
|
181
|
+
options[:line_width] = args[0] || 80 unless args.empty?
|
|
182
|
+
options = { line_width: 80 }.update(options)
|
|
185
183
|
|
|
186
184
|
text.split("\n").map do |line|
|
|
187
185
|
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
|
|
@@ -210,11 +208,11 @@ module Padrino
|
|
|
210
208
|
# highlight('Lorem ipsum dolor sit amet', 'dolor')
|
|
211
209
|
# # => Lorem ipsum <strong class="highlight">dolor</strong> sit amet
|
|
212
210
|
#
|
|
213
|
-
# highlight('Lorem ipsum dolor sit amet', 'dolor', :
|
|
211
|
+
# highlight('Lorem ipsum dolor sit amet', 'dolor', highlighter: '<span class="custom">\1</span>')
|
|
214
212
|
# # => Lorem ipsum <strong class="custom">dolor</strong> sit amet
|
|
215
213
|
#
|
|
216
214
|
def highlight(text, words, *args)
|
|
217
|
-
options = { :
|
|
215
|
+
options = { highlighter: '<strong class="highlight">\1</strong>' }.update(args.last.is_a?(Hash) ? args.pop : {})
|
|
218
216
|
|
|
219
217
|
if text.empty? || words.empty?
|
|
220
218
|
text
|
|
@@ -286,47 +284,47 @@ module Padrino
|
|
|
286
284
|
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
|
|
287
285
|
from_time = from_time.to_time if from_time.respond_to?(:to_time)
|
|
288
286
|
to_time = to_time.to_time if to_time.respond_to?(:to_time)
|
|
289
|
-
distance_in_minutes = ((
|
|
290
|
-
distance_in_seconds = (
|
|
287
|
+
distance_in_minutes = ((to_time.to_i - from_time.to_i).abs / 60).round
|
|
288
|
+
distance_in_seconds = (to_time.to_i - from_time.to_i).abs.round
|
|
291
289
|
|
|
292
290
|
phrase, locals =
|
|
293
291
|
case distance_in_minutes
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
end
|
|
304
|
-
else
|
|
305
|
-
distance_in_minutes == 0 ?
|
|
306
|
-
[:less_than_x_minutes, :count => 1] :
|
|
307
|
-
[:x_minutes, :count => distance_in_minutes]
|
|
292
|
+
when 0..1
|
|
293
|
+
if include_seconds
|
|
294
|
+
case distance_in_seconds
|
|
295
|
+
when 0..4 then [:less_than_x_seconds, { count: 5 }]
|
|
296
|
+
when 5..9 then [:less_than_x_seconds, { count: 10 }]
|
|
297
|
+
when 10..19 then [:less_than_x_seconds, { count: 20 }]
|
|
298
|
+
when 20..39 then [:half_a_minute]
|
|
299
|
+
when 40..59 then [:less_than_x_minutes, { count: 1 }]
|
|
300
|
+
else [:x_minutes, { count: 1 }]
|
|
308
301
|
end
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
when 90..1439 then [:about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round ]
|
|
312
|
-
when 1440..2529 then [:x_days, :count => 1 ]
|
|
313
|
-
when 2530..43199 then [:x_days, :count => (distance_in_minutes.to_f / 1440.0).round ]
|
|
314
|
-
when 43200..86399 then [:about_x_months, :count => 1 ]
|
|
315
|
-
when 86400..525599 then [:x_months, :count => (distance_in_minutes.to_f / 43200.0).round]
|
|
302
|
+
elsif distance_in_minutes.zero?
|
|
303
|
+
[:less_than_x_minutes, { count: 1 }]
|
|
316
304
|
else
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
305
|
+
[:x_minutes, { count: distance_in_minutes }]
|
|
306
|
+
end
|
|
307
|
+
when 2..44 then [:x_minutes, { count: distance_in_minutes }]
|
|
308
|
+
when 45..89 then [:about_x_hours, { count: 1 }]
|
|
309
|
+
when 90..1439 then [:about_x_hours, { count: (distance_in_minutes.to_f / 60.0).round }]
|
|
310
|
+
when 1440..2529 then [:x_days, { count: 1 }]
|
|
311
|
+
when 2530..43_199 then [:x_days, { count: (distance_in_minutes.to_f / 1440.0).round }]
|
|
312
|
+
when 43_200..86_399 then [:about_x_months, { count: 1 }]
|
|
313
|
+
when 86_400..525_599 then [:x_months, { count: (distance_in_minutes.to_f / 43_200.0).round }]
|
|
314
|
+
else
|
|
315
|
+
distance_in_years = distance_in_minutes / 525_600
|
|
316
|
+
minute_offset_for_leap_year = (distance_in_years / 4) * 1440
|
|
317
|
+
remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525_600)
|
|
318
|
+
if remainder < 131_400
|
|
319
|
+
[:about_x_years, { count: distance_in_years }]
|
|
320
|
+
elsif remainder < 394_200
|
|
321
|
+
[:over_x_years, { count: distance_in_years }]
|
|
322
|
+
else
|
|
323
|
+
[:almost_x_years, { count: distance_in_years + 1 }]
|
|
324
|
+
end
|
|
327
325
|
end
|
|
328
326
|
|
|
329
|
-
I18n.translate phrase, :
|
|
327
|
+
I18n.translate phrase, count: locals[:count], locale: options[:locale], scope: :'datetime.distance_in_words'
|
|
330
328
|
end
|
|
331
329
|
|
|
332
330
|
##
|
|
@@ -362,11 +360,11 @@ module Padrino
|
|
|
362
360
|
def js_escape_html(html_content)
|
|
363
361
|
return '' unless html_content
|
|
364
362
|
javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
|
|
365
|
-
escaped_content = html_content.gsub(
|
|
363
|
+
escaped_content = html_content.gsub(%r{(\\|</|\r\n|[\n\r"'])}) { |m| javascript_mapping[m] }
|
|
366
364
|
escaped_content = escaped_content.html_safe if html_content.html_safe?
|
|
367
365
|
escaped_content
|
|
368
366
|
end
|
|
369
|
-
alias
|
|
367
|
+
alias escape_javascript js_escape_html
|
|
370
368
|
end
|
|
371
369
|
end
|
|
372
370
|
end
|
|
@@ -32,19 +32,19 @@ module Padrino
|
|
|
32
32
|
# @return [String] The formatted representation of the currency.
|
|
33
33
|
#
|
|
34
34
|
# @example
|
|
35
|
-
# number_to_currency(1234567890.50)
|
|
36
|
-
# number_to_currency(1234567890.506)
|
|
37
|
-
# number_to_currency(1234567890.506, :
|
|
38
|
-
# number_to_currency(1234567890.50, :
|
|
35
|
+
# number_to_currency(1234567890.50) # => $1,234,567,890.50
|
|
36
|
+
# number_to_currency(1234567890.506) # => $1,234,567,890.51
|
|
37
|
+
# number_to_currency(1234567890.506, precision: 3) # => $1,234,567,890.506
|
|
38
|
+
# number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '')
|
|
39
39
|
# # => £1234567890,50
|
|
40
|
-
# number_to_currency(1234567890.50, :
|
|
40
|
+
# number_to_currency(1234567890.50, unit: '£', separator: ',', delimiter: '', format: '%n %u')
|
|
41
41
|
# # => 1234567890,50 £
|
|
42
42
|
#
|
|
43
43
|
def number_to_currency(number, options = {})
|
|
44
44
|
options = Utils.symbolize_keys(options)
|
|
45
45
|
|
|
46
|
-
defaults = I18n.translate(:'number.format', :
|
|
47
|
-
currency = I18n.translate(:'number.currency.format', :
|
|
46
|
+
defaults = I18n.translate(:'number.format', locale: options[:locale], raise: true) rescue {}
|
|
47
|
+
currency = I18n.translate(:'number.currency.format', locale: options[:locale], raise: true) rescue {}
|
|
48
48
|
defaults = defaults.merge(currency)
|
|
49
49
|
|
|
50
50
|
precision = options[:precision] || defaults[:precision]
|
|
@@ -52,15 +52,12 @@ module Padrino
|
|
|
52
52
|
separator = options[:separator] || defaults[:separator]
|
|
53
53
|
delimiter = options[:delimiter] || defaults[:delimiter]
|
|
54
54
|
format = options[:format] || defaults[:format]
|
|
55
|
-
separator = '' if precision
|
|
55
|
+
separator = '' if precision.zero?
|
|
56
56
|
|
|
57
57
|
begin
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
:separator => separator)
|
|
62
|
-
).gsub(/%u/, unit)
|
|
63
|
-
rescue
|
|
58
|
+
value = number_with_precision(number, precision: precision, delimiter: delimiter, separator: separator)
|
|
59
|
+
format.gsub(/%n/, value).gsub(/%u/, unit)
|
|
60
|
+
rescue StandardError
|
|
64
61
|
number
|
|
65
62
|
end
|
|
66
63
|
end
|
|
@@ -83,16 +80,16 @@ module Padrino
|
|
|
83
80
|
# @return [String] The formatted representation of the percentage
|
|
84
81
|
#
|
|
85
82
|
# @example
|
|
86
|
-
# number_to_percentage(100)
|
|
87
|
-
# number_to_percentage(100, :
|
|
88
|
-
# number_to_percentage(1000, :
|
|
89
|
-
# number_to_percentage(302.24398923423, :
|
|
83
|
+
# number_to_percentage(100) # => 100.000%
|
|
84
|
+
# number_to_percentage(100, precision: 0) # => 100%
|
|
85
|
+
# number_to_percentage(1000, delimiter: '.', separator: ',') # => 1.000,000%
|
|
86
|
+
# number_to_percentage(302.24398923423, precision: 5) # => 302.24399%
|
|
90
87
|
#
|
|
91
88
|
def number_to_percentage(number, options = {})
|
|
92
89
|
options = Utils.symbolize_keys(options)
|
|
93
90
|
|
|
94
|
-
defaults = I18n.translate(:'number.format', :
|
|
95
|
-
percentage = I18n.translate(:'number.percentage.format', :
|
|
91
|
+
defaults = I18n.translate(:'number.format', locale: options[:locale], raise: true) rescue {}
|
|
92
|
+
percentage = I18n.translate(:'number.percentage.format', locale: options[:locale], raise: true) rescue {}
|
|
96
93
|
defaults = defaults.merge(percentage)
|
|
97
94
|
|
|
98
95
|
precision = options[:precision] || defaults[:precision]
|
|
@@ -100,11 +97,13 @@ module Padrino
|
|
|
100
97
|
delimiter = options[:delimiter] || defaults[:delimiter]
|
|
101
98
|
|
|
102
99
|
begin
|
|
103
|
-
number_with_precision(
|
|
104
|
-
|
|
105
|
-
:
|
|
106
|
-
:
|
|
107
|
-
|
|
100
|
+
number_with_precision(
|
|
101
|
+
number,
|
|
102
|
+
precision: precision,
|
|
103
|
+
separator: separator,
|
|
104
|
+
delimiter: delimiter
|
|
105
|
+
) + '%'
|
|
106
|
+
rescue StandardError
|
|
108
107
|
number
|
|
109
108
|
end
|
|
110
109
|
end
|
|
@@ -113,7 +112,7 @@ module Padrino
|
|
|
113
112
|
# Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
|
|
114
113
|
# customize the format in the +options+ hash.
|
|
115
114
|
#
|
|
116
|
-
# @overload number_with_delimiter(number, options={})
|
|
115
|
+
# @overload number_with_delimiter(number, options = {})
|
|
117
116
|
# @param [Integer, Float] number
|
|
118
117
|
# Number value to format.
|
|
119
118
|
# @param [Hash] options
|
|
@@ -126,26 +125,26 @@ module Padrino
|
|
|
126
125
|
# @return [String] The formatted representation of the number.
|
|
127
126
|
#
|
|
128
127
|
# @example
|
|
129
|
-
# number_with_delimiter(12345678)
|
|
130
|
-
# number_with_delimiter(12345678.05)
|
|
131
|
-
# number_with_delimiter(12345678, :
|
|
132
|
-
# number_with_delimiter(12345678, :
|
|
133
|
-
# number_with_delimiter(98765432.98, :
|
|
128
|
+
# number_with_delimiter(12345678) # => 12,345,678
|
|
129
|
+
# number_with_delimiter(12345678.05) # => 12,345,678.05
|
|
130
|
+
# number_with_delimiter(12345678, delimiter: '.') # => 12.345.678
|
|
131
|
+
# number_with_delimiter(12345678, separator: ',') # => 12,345,678
|
|
132
|
+
# number_with_delimiter(98765432.98, delimiter: ' ', separator: ',')
|
|
134
133
|
# # => 98 765 432,98
|
|
135
134
|
#
|
|
136
135
|
def number_with_delimiter(number, *args)
|
|
137
136
|
options = args.last.is_a?(Hash) ? Utils.symbolize_keys(args.pop) : {}
|
|
138
137
|
|
|
139
|
-
defaults = I18n.translate(:'number.format', :
|
|
138
|
+
defaults = I18n.translate(:'number.format', locale: options[:locale], raise: true) rescue {}
|
|
140
139
|
|
|
141
|
-
delimiter ||=
|
|
142
|
-
separator ||=
|
|
140
|
+
delimiter ||= options[:delimiter] || defaults[:delimiter]
|
|
141
|
+
separator ||= options[:separator] || defaults[:separator]
|
|
143
142
|
|
|
144
143
|
begin
|
|
145
144
|
parts = number.to_s.split('.')
|
|
146
145
|
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
|
|
147
146
|
parts.join(separator)
|
|
148
|
-
rescue
|
|
147
|
+
rescue StandardError
|
|
149
148
|
number
|
|
150
149
|
end
|
|
151
150
|
end
|
|
@@ -154,7 +153,7 @@ module Padrino
|
|
|
154
153
|
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
|
155
154
|
# You can customize the format in the +options+ hash.
|
|
156
155
|
#
|
|
157
|
-
# @overload number_with_precision(number, options={})
|
|
156
|
+
# @overload number_with_precision(number, options = {})
|
|
158
157
|
# @param [Integer, Float] number
|
|
159
158
|
# Number value to format.
|
|
160
159
|
# @param [Hash] options
|
|
@@ -169,37 +168,39 @@ module Padrino
|
|
|
169
168
|
# @return [String] The formatted representation of the number.
|
|
170
169
|
#
|
|
171
170
|
# @example
|
|
172
|
-
# number_with_precision(111.2345)
|
|
173
|
-
# number_with_precision(111.2345, :
|
|
174
|
-
# number_with_precision(13, :
|
|
175
|
-
# number_with_precision(389.32314, :
|
|
176
|
-
# number_with_precision(1111.2345, :
|
|
171
|
+
# number_with_precision(111.2345) # => 111.235
|
|
172
|
+
# number_with_precision(111.2345, precision: 2) # => 111.23
|
|
173
|
+
# number_with_precision(13, precision: 5) # => 13.00000
|
|
174
|
+
# number_with_precision(389.32314, precision: 0) # => 389
|
|
175
|
+
# number_with_precision(1111.2345, precision: 2, separator: ',', delimiter: '.')
|
|
177
176
|
# # => 1.111,23
|
|
178
177
|
#
|
|
179
178
|
def number_with_precision(number, *args)
|
|
180
179
|
options = args.last.is_a?(Hash) ? Utils.symbolize_keys(args.pop) : {}
|
|
181
180
|
|
|
182
|
-
defaults = I18n.translate(:'number.format', :
|
|
183
|
-
precision_defaults = I18n.translate(:'number.precision.format', :
|
|
184
|
-
:
|
|
181
|
+
defaults = I18n.translate(:'number.format', locale: options[:locale], raise: true) rescue {}
|
|
182
|
+
precision_defaults = I18n.translate(:'number.precision.format', locale: options[:locale],
|
|
183
|
+
raise: true) rescue {}
|
|
185
184
|
defaults = defaults.merge(precision_defaults)
|
|
186
185
|
|
|
187
|
-
precision ||=
|
|
188
|
-
separator ||=
|
|
189
|
-
delimiter ||=
|
|
186
|
+
precision ||= options[:precision] || defaults[:precision]
|
|
187
|
+
separator ||= options[:separator] || defaults[:separator]
|
|
188
|
+
delimiter ||= options[:delimiter] || defaults[:delimiter]
|
|
190
189
|
|
|
191
190
|
begin
|
|
192
|
-
rounded_number = (Float(number) * (10
|
|
193
|
-
number_with_delimiter(
|
|
194
|
-
|
|
195
|
-
:
|
|
196
|
-
|
|
191
|
+
rounded_number = (Float(number) * (10**precision)).round.to_f / 10**precision
|
|
192
|
+
number_with_delimiter(
|
|
193
|
+
format("%01.#{precision}f", rounded_number),
|
|
194
|
+
separator: separator,
|
|
195
|
+
delimiter: delimiter
|
|
196
|
+
)
|
|
197
|
+
rescue StandardError
|
|
197
198
|
number
|
|
198
199
|
end
|
|
199
200
|
end
|
|
200
201
|
|
|
201
202
|
# The units available for storage formatting.
|
|
202
|
-
STORAGE_UNITS = [
|
|
203
|
+
STORAGE_UNITS = %i[byte kb mb gb tb].freeze
|
|
203
204
|
|
|
204
205
|
##
|
|
205
206
|
# Formats the bytes in +size+ into a more understandable representation
|
|
@@ -209,7 +210,7 @@ module Padrino
|
|
|
209
210
|
# format in the +options+ hash.
|
|
210
211
|
#
|
|
211
212
|
#
|
|
212
|
-
# @overload number_to_human_size(number, options={})
|
|
213
|
+
# @overload number_to_human_size(number, options = {})
|
|
213
214
|
# @param [Integer] number
|
|
214
215
|
# Number value to format.
|
|
215
216
|
# @param [Hash] options
|
|
@@ -224,53 +225,56 @@ module Padrino
|
|
|
224
225
|
# @return [String] The formatted representation of bytes
|
|
225
226
|
#
|
|
226
227
|
# @example
|
|
227
|
-
# number_to_human_size(123)
|
|
228
|
-
# number_to_human_size(1234)
|
|
229
|
-
# number_to_human_size(12345)
|
|
230
|
-
# number_to_human_size(1234567)
|
|
231
|
-
# number_to_human_size(1234567890)
|
|
232
|
-
# number_to_human_size(1234567890123)
|
|
233
|
-
# number_to_human_size(1234567, :
|
|
234
|
-
# number_to_human_size(483989, :
|
|
235
|
-
# number_to_human_size(1234567, :
|
|
228
|
+
# number_to_human_size(123) # => 123 Bytes
|
|
229
|
+
# number_to_human_size(1234) # => 1.2 KB
|
|
230
|
+
# number_to_human_size(12345) # => 12.1 KB
|
|
231
|
+
# number_to_human_size(1234567) # => 1.2 MB
|
|
232
|
+
# number_to_human_size(1234567890) # => 1.1 GB
|
|
233
|
+
# number_to_human_size(1234567890123) # => 1.1 TB
|
|
234
|
+
# number_to_human_size(1234567, precision: 2) # => 1.18 MB
|
|
235
|
+
# number_to_human_size(483989, precision: 0) # => 473 KB
|
|
236
|
+
# number_to_human_size(1234567, precision: 2, separator: ',') # => 1,18 MB
|
|
236
237
|
#
|
|
237
238
|
def number_to_human_size(number, *args)
|
|
238
239
|
return nil if number.nil?
|
|
239
240
|
|
|
240
241
|
options = args.last.is_a?(Hash) ? Utils.symbolize_keys(args.pop) : {}
|
|
241
242
|
|
|
242
|
-
defaults = I18n.translate(:'number.format', :
|
|
243
|
-
human = I18n.translate(:'number.human.format', :
|
|
243
|
+
defaults = I18n.translate(:'number.format', locale: options[:locale], raise: true) rescue {}
|
|
244
|
+
human = I18n.translate(:'number.human.format', locale: options[:locale], raise: true) rescue {}
|
|
244
245
|
defaults = defaults.merge(human)
|
|
245
246
|
|
|
246
|
-
precision ||=
|
|
247
|
-
separator ||=
|
|
248
|
-
delimiter ||=
|
|
247
|
+
precision ||= options[:precision] || defaults[:precision]
|
|
248
|
+
separator ||= options[:separator] || defaults[:separator]
|
|
249
|
+
delimiter ||= options[:delimiter] || defaults[:delimiter]
|
|
249
250
|
|
|
250
|
-
storage_units_format = I18n.translate(:'number.human.storage_units.format', :
|
|
251
|
+
storage_units_format = I18n.translate(:'number.human.storage_units.format', locale: options[:locale], raise: true)
|
|
251
252
|
|
|
252
253
|
if number.to_i < 1024
|
|
253
|
-
unit = I18n.translate(:'number.human.storage_units.units.byte', :
|
|
254
|
+
unit = I18n.translate(:'number.human.storage_units.units.byte', locale: options[:locale], count: number.to_i, raise: true)
|
|
254
255
|
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit)
|
|
255
256
|
else
|
|
256
257
|
max_exp = STORAGE_UNITS.size - 1
|
|
257
258
|
number = Float(number)
|
|
258
259
|
exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
|
|
259
260
|
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
|
260
|
-
number /= 1024
|
|
261
|
+
number /= 1024**exponent
|
|
261
262
|
|
|
262
263
|
unit_key = STORAGE_UNITS[exponent]
|
|
263
|
-
unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :
|
|
264
|
+
unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", locale: options[:locale], count: number, raise: true)
|
|
264
265
|
|
|
265
266
|
begin
|
|
266
267
|
escaped_separator = Regexp.escape(separator)
|
|
267
|
-
formatted_number =
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
268
|
+
formatted_number =
|
|
269
|
+
number_with_precision(
|
|
270
|
+
number,
|
|
271
|
+
precision: precision,
|
|
272
|
+
separator: separator,
|
|
273
|
+
delimiter: delimiter
|
|
274
|
+
).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
|
275
|
+
|
|
272
276
|
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
|
|
273
|
-
rescue
|
|
277
|
+
rescue StandardError
|
|
274
278
|
number
|
|
275
279
|
end
|
|
276
280
|
end
|
|
@@ -16,6 +16,7 @@ module Padrino
|
|
|
16
16
|
# @handler.engine_matches?(block) => true
|
|
17
17
|
#
|
|
18
18
|
def engine_matches?(block)
|
|
19
|
+
false
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
##
|
|
@@ -29,11 +30,12 @@ module Padrino
|
|
|
29
30
|
# @handler.capture_from_template(&block) => "...html..."
|
|
30
31
|
#
|
|
31
32
|
def capture_from_template(*args, &block)
|
|
32
|
-
self.output_buffer,
|
|
33
|
+
self.output_buffer, buf_was = SafeBuffer.new, self.output_buffer
|
|
33
34
|
raw = yield(*args)
|
|
34
35
|
captured = template.instance_variable_get(:@_out_buf)
|
|
35
|
-
self.output_buffer = _buf_was
|
|
36
36
|
engine_matches?(block) && !captured.empty? ? captured : raw.to_s
|
|
37
|
+
ensure
|
|
38
|
+
self.output_buffer = buf_was
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
##
|
|
@@ -46,7 +48,7 @@ module Padrino
|
|
|
46
48
|
# @example
|
|
47
49
|
# @handler.concat_to_template("This will be output to the template buffer")
|
|
48
50
|
#
|
|
49
|
-
def concat_to_template(text=
|
|
51
|
+
def concat_to_template(text = '', context = nil)
|
|
50
52
|
text
|
|
51
53
|
end
|
|
52
54
|
|
|
@@ -8,8 +8,8 @@ module Padrino
|
|
|
8
8
|
##
|
|
9
9
|
# Outputs the given text to the templates buffer directly.
|
|
10
10
|
#
|
|
11
|
-
def concat_to_template(text=
|
|
12
|
-
return text if context
|
|
11
|
+
def concat_to_template(text = '', context = nil)
|
|
12
|
+
return text if context&.eval('@__in_ruby_literal')
|
|
13
13
|
output_buffer << text
|
|
14
14
|
nil
|
|
15
15
|
end
|
|
@@ -2,7 +2,7 @@ module Padrino
|
|
|
2
2
|
module Helpers
|
|
3
3
|
module OutputHelpers
|
|
4
4
|
# Haml and Hamlit require different detection code
|
|
5
|
-
if defined?(Haml) && Tilt.template_for('.haml').to_s ==
|
|
5
|
+
if defined?(Haml) && Tilt.template_for('.haml').to_s == 'Padrino::Rendering::HamlTemplate'
|
|
6
6
|
##
|
|
7
7
|
# Handler for Haml templates.
|
|
8
8
|
#
|