Class: Biggs::Formatter
- Inherits:
-
Object
- Object
- Biggs::Formatter
- Defined in:
- lib/biggs/formatter.rb
Constant Summary collapse
- FIELDS_WO_COUNTRY =
[:recipient, :street, :city, :state, :zip].freeze
- FIELDS =
(FIELDS_WO_COUNTRY + [:country]).freeze
Instance Attribute Summary collapse
-
#blank_country_on ⇒ Object
Returns the value of attribute blank_country_on.
-
#default_country_with_state ⇒ Object
Returns the value of attribute default_country_with_state.
-
#default_country_without_state ⇒ Object
Returns the value of attribute default_country_without_state.
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.
7 8 9 10 |
# File 'lib/biggs/formatter.rb', line 7 def initialize(={}) @blank_country_on = [[:blank_country_on]].compact.flatten.map{|s| s.to_s.downcase} @remove_empty_lines = !![:remove_empty_lines] end |
Instance Attribute Details
#blank_country_on ⇒ Object
Returns the value of attribute blank_country_on.
27 28 29 |
# File 'lib/biggs/formatter.rb', line 27 def blank_country_on @blank_country_on end |
#default_country_with_state ⇒ Object
Returns the value of attribute default_country_with_state.
27 28 29 |
# File 'lib/biggs/formatter.rb', line 27 def default_country_with_state @default_country_with_state end |
#default_country_without_state ⇒ Object
Returns the value of attribute default_country_without_state.
27 28 29 |
# File 'lib/biggs/formatter.rb', line 27 def default_country_without_state @default_country_without_state end |
Instance Method Details
#format(iso_code, values = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/biggs/formatter.rb', line 12 def format(iso_code, values={}) values.symbolize_keys! if values.respond_to?(:symbolize_keys!) format = Biggs::Format.find(iso_code) format_string = (format.format_string || default_format_string(values[:state])).dup.to_s country_name = blank_country_on.include?(format.iso_code) ? "" : format.country_name || format.iso_code FIELDS_WO_COUNTRY.each do |key| format_string.gsub!(/\{\{#{key}\}\}/, (values[key] || "").to_s) end format_string.gsub!(/\{\{country\}\}/, country_name) format_string.gsub!(/\n(\s+)?\n/, "\n") if @remove_empty_lines format_string.strip end |