aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/number_helper/number_to_phone.rb
diff options
context:
space:
mode:
authorMatt Bridges <mbridges.91@gmail.com>2013-06-12 11:53:29 -0500
committerMatt Bridges <mbridges.91@gmail.com>2013-07-01 12:31:36 -0500
commit2da9d67c278b0f37d2dea89ff557dc07cbb3a7b0 (patch)
tree8e657d583379a482f25719fed62a0960deb3e8a8 /activesupport/lib/active_support/number_helper/number_to_phone.rb
parente47b6dee858e62dceba867dd160b968d679c82e8 (diff)
downloadrails-2da9d67c278b0f37d2dea89ff557dc07cbb3a7b0.tar.gz
rails-2da9d67c278b0f37d2dea89ff557dc07cbb3a7b0.tar.bz2
rails-2da9d67c278b0f37d2dea89ff557dc07cbb3a7b0.zip
Extract ActiveSupport::NumberHelper methods to classes
Due to the overall complexity of each method individually as well as the global shared private module methods, this pulls each helper into it's own converter class inheriting from a generic `NumberBuilder` class. * The `NumberBuilder` class contains the private methods needed for each helper method an eliminates the need for special definition of specialized private module methods. * The `ActiveSupport::NumberHelper::DEFAULTS` constant has been moved into the `NumberBuilder` class because the `NumberBuilder` is the only class which needs access to it. * For each of the builders, the `#convert` method is broken down to smaller parts and extracted into private methods for clarity of purpose. * Most of the mutation that once was necessary has now been eliminated. * Several of the mathematical operations for percentage, delimited, and rounded have been moved into private methods to ease readability and clarity. * Internationalization is still a bit crufty, and definitely could be improved, but it is functional and a bit easier to follow. The following helpers were extracted into their respective classes. * `#number_to_percentage` -> `NumberToPercentageConverter` * `#number_to_delimited` -> `NumberToDelimitedConverter` * `#number_to_phone` -> `NumberToPhoneConverter` * `#number_to_currency` -> `NumberToCurrencyConverter` * `#number_to_rounded` -> `NumberToRoundedConverter` * `#number_to_human_size` -> `NumberToHumanSizeConverter` * `#number_to_human` -> `NumberToHumanConverter`
Diffstat (limited to 'activesupport/lib/active_support/number_helper/number_to_phone.rb')
-rw-r--r--activesupport/lib/active_support/number_helper/number_to_phone.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/number_helper/number_to_phone.rb b/activesupport/lib/active_support/number_helper/number_to_phone.rb
new file mode 100644
index 0000000000..171c2dff18
--- /dev/null
+++ b/activesupport/lib/active_support/number_helper/number_to_phone.rb
@@ -0,0 +1,54 @@
+require 'active_support/number_helper/number_converter'
+require 'active_support/number_helper/number_to_rounded'
+
+module ActiveSupport
+ module NumberHelper
+ class NumberToPhoneConverter < NumberConverter
+ def convert
+ str = ''
+ str << country_code(opts[:country_code])
+ str << convert_to_phone_number(@number.to_s.strip)
+ str << phone_ext(opts[:extension])
+ end
+
+ private
+
+ def convert_to_phone_number(number)
+ if opts[:area_code]
+ convert_with_area_code(number)
+ else
+ convert_without_area_code(number)
+ end
+ end
+
+ def convert_with_area_code(number)
+ number.gsub(/(\d{1,3})(\d{3})(\d{4}$)/,"(\\1) \\2#{delimiter}\\3")
+ end
+
+ def convert_without_area_code(number)
+ number.tap { |n|
+ n.gsub!(/(\d{0,3})(\d{3})(\d{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
+ n.slice!(0, 1) if begins_with_delimiter?(n)
+ }
+ end
+
+ def begins_with_delimiter?(number)
+ number.start_with?(delimiter) && !delimiter.blank?
+ end
+
+ def delimiter
+ opts[:delimiter] || "-"
+ end
+
+ def country_code(code)
+ code.blank? ? "" : "+#{code}#{delimiter}"
+ end
+
+ def phone_ext(ext)
+ ext.blank? ? "" : " x #{ext}"
+ end
+
+ end
+ end
+end
+