Module: ActionView::Helpers::NumberHelper
Relationships & Source Files | |
Namespace Children | |
Exceptions:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | actionview/lib/action_view/helpers/number_helper.rb |
Overview
Action View Number Helpers
Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, file size, and pretty printing.
Most methods expect a number
argument, and will return it unchanged if can’t be converted into a valid number.
Instance Method Summary
-
#number_to_currency(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_currency.
-
#number_to_human(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human.
-
#number_to_human_size(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human_size.
-
#number_to_percentage(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_percentage.
-
#number_to_phone(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_phone.
-
#number_with_delimiter(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_delimited.
-
#number_with_precision(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_rounded.
- #delegate_number_helper_method(method, number, options) private
- #escape_units(units) private
- #escape_unsafe_options(options) private
- #parse_float(number, raise_error) private
- #valid_float?(number) ⇒ Boolean private
- #wrap_with_output_safety_handling(number, raise_on_invalid, &block) private
Instance Method Details
#delegate_number_helper_method(method, number, options) (private)
[ GitHub ]# File 'actionview/lib/action_view/helpers/number_helper.rb', line 116
def delegate_number_helper_method(method, number, ) return unless number = ( .symbolize_keys) wrap_with_output_safety_handling(number, .delete(:raise)) { ActiveSupport::NumberHelper.public_send(method, number, ) } end
#escape_units(units) (private)
[ GitHub ]# File 'actionview/lib/action_view/helpers/number_helper.rb', line 135
def escape_units(units) units.transform_values do |v| ERB::Util.html_escape(v) end end
#escape_unsafe_options(options) (private)
[ GitHub ]# File 'actionview/lib/action_view/helpers/number_helper.rb', line 125
def ( ) [:format] = ERB::Util.html_escape( [:format]) if [:format] [:negative_format] = ERB::Util.html_escape( [:negative_format]) if [:negative_format] [:separator] = ERB::Util.html_escape( [:separator]) if [:separator] [:delimiter] = ERB::Util.html_escape( [:delimiter]) if [:delimiter] [:unit] = ERB::Util.html_escape( [:unit]) if [:unit] && ! [:unit].html_safe? [:units] = escape_units( [:units]) if [:units] && Hash === [:units] end
#number_to_currency(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_currency.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_currency("12x34") # => "$12x34"
number_to_currency("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 51
def number_to_currency(number, = {}) delegate_number_helper_method(:number_to_currency, number, ) end
#number_to_human(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_human("12x34") # => "12x34"
number_to_human("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 111
def number_to_human(number, = {}) delegate_number_helper_method(:number_to_human, number, ) end
#number_to_human_size(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_human_size.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_human_size("12x34") # => "12x34"
number_to_human_size("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 99
def number_to_human_size(number, = {}) delegate_number_helper_method(:number_to_human_size, number, ) end
#number_to_percentage(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_percentage.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_percentage("99x") # => "99x%"
number_to_percentage("99x", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 63
def number_to_percentage(number, = {}) delegate_number_helper_method(:number_to_percentage, number, ) end
#number_to_phone(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_phone.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_to_phone("12x34") # => "12x34"
number_to_phone("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 35
def number_to_phone(number, = {}) return unless number = .symbolize_keys parse_float(number, true) if .delete(:raise) ERB::Util.html_escape(ActiveSupport::NumberHelper.number_to_phone(number, )) end
#number_with_delimiter(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_delimited.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_with_delimiter("12x34") # => "12x34"
number_with_delimiter("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 75
def number_with_delimiter(number, = {}) delegate_number_helper_method(:number_to_delimited, number, ) end
#number_with_precision(number, options = {})
Delegates to ActiveSupport::NumberHelper#number_to_rounded.
Additionally, supports a :raise
option that will cause NumberHelper::InvalidNumberError
to be raised if number
is not a valid number:
number_with_precision("12x34") # => "12x34"
number_with_precision("12x34", raise: true) # => InvalidNumberError
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 87
def number_with_precision(number, = {}) delegate_number_helper_method(:number_to_rounded, number, ) end
#parse_float(number, raise_error) (private)
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 158
def parse_float(number, raise_error) result = Float(number, exception: false) raise InvalidNumberError, number if result.nil? && raise_error result end
#valid_float?(number) ⇒ Boolean
(private)
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 154
def valid_float?(number) !parse_float(number, false).nil? end
#wrap_with_output_safety_handling(number, raise_on_invalid, &block) (private)
# File 'actionview/lib/action_view/helpers/number_helper.rb', line 141
def wrap_with_output_safety_handling(number, raise_on_invalid, &block) valid_float = valid_float?(number) raise InvalidNumberError, number if raise_on_invalid && !valid_float formatted_number = yield if valid_float || number.html_safe? formatted_number.html_safe else formatted_number end end