Class: Biggs::Formatter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
# File 'lib/biggs/formatter.rb', line 7

def initialize(options={})
  @blank_country_on = [options[:blank_country_on]].compact.flatten.map{|s| s.to_s.downcase}
  @remove_empty_lines = !!options[:remove_empty_lines]
end

Instance Attribute Details

#blank_country_onObject

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_stateObject

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_stateObject

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