Class: Biggs::Formatter
- Inherits:
-
Object
- Object
- Biggs::Formatter
- Defined in:
- lib/biggs/formatter.rb
Constant Summary collapse
- FIELDS =
[:recipient, :street, :city, :region, :region_short, :postalcode, :country]
Instance Attribute Summary collapse
-
#blank_country_on ⇒ Object
Returns the value of attribute blank_country_on.
-
#default_country_with_region ⇒ Object
Returns the value of attribute default_country_with_region.
-
#default_country_without_region ⇒ Object
Returns the value of attribute default_country_without_region.
Instance Method Summary collapse
- #format(iso_code, values = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(options = {}) ⇒ Formatter
Returns a new instance of Formatter.
5 6 7 |
# File 'lib/biggs/formatter.rb', line 5 def initialize( = {}) @blank_country_on = [[:blank_country_on]].compact.flatten.map { |s| s.to_s.downcase } end |
Instance Attribute Details
#blank_country_on ⇒ Object
Returns the value of attribute blank_country_on.
47 48 49 |
# File 'lib/biggs/formatter.rb', line 47 def blank_country_on @blank_country_on end |
#default_country_with_region ⇒ Object
Returns the value of attribute default_country_with_region.
47 48 49 |
# File 'lib/biggs/formatter.rb', line 47 def default_country_with_region @default_country_with_region end |
#default_country_without_region ⇒ Object
Returns the value of attribute default_country_without_region.
47 48 49 |
# File 'lib/biggs/formatter.rb', line 47 def default_country_without_region @default_country_without_region end |
Instance Method Details
#format(iso_code, values = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/biggs/formatter.rb', line 9 def format(iso_code, values = {}) # HACK: most of this method, honestly. I needed to get {{region_short}} working and # bring this into compliance with countries v0.11.x. values.symbolize_keys! if values.respond_to?(:symbolize_keys!) format = Biggs::Format.find(iso_code) raise "Could not find country with code '#{iso_code}'." unless format && format.country format_string = (format.format_string || default_format_string(values[:region])).dup.to_s if values[:region_short] && !values[:region] && format_string.include?("{{region}}") region_code = values[:region_short] region = format.country.subdivisions[:region_short] raise "Could not find region/subdivision with code '#{region_code}'. " + "To override, specify both :region and :region_short." unless region values[:region] = region[:name] elsif values[:region] && !values[:region_short] && format_string.include?("{{region_short}}") # FIXME: this is an iterative search over all names in the countries list. Not great, Bob. subdivision = format.country.subdivisions.find do |sub_kvp| sub_kvp[1]["name"] == values[:region] || sub_kvp[1]["names"].include?(values[:region]) end raise "Could not find short_code" unless subdivision values[:region_short] = subdivision[0] end format_string = (format.format_string || default_format_string(values[:region])).dup.to_s country_name = blank_country_on.include?(format.iso_code) ? '' : format.country_name || format.iso_code (FIELDS - [:country]).each do |key| format_string.gsub!(/\{\{#{key}\}\}/, (values[key] || '').to_s) end format_string.gsub!(/\{\{country\}\}/, country_name) format_string.gsub(/\n$/, '') end |