Class: Biggs::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/biggs/formatter.rb

Constant Summary collapse

FIELDS =
[:recipient, :street, :city, :state, :zip, :country]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



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

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

Instance Attribute Details

#blank_country_onObject

Returns the value of attribute blank_country_on.



24
25
26
# File 'lib/biggs/formatter.rb', line 24

def blank_country_on
  @blank_country_on
end

#default_country_with_stateObject

Returns the value of attribute default_country_with_state.



24
25
26
# File 'lib/biggs/formatter.rb', line 24

def default_country_with_state
  @default_country_with_state
end

#default_country_without_stateObject

Returns the value of attribute default_country_without_state.



24
25
26
# File 'lib/biggs/formatter.rb', line 24

def default_country_without_state
  @default_country_without_state
end

Instance Method Details

#format(iso_code, values = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/biggs/formatter.rb', line 10

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 - [:country]).each do |key|
    format_string.gsub!(/\{\{#{key}\}\}/, (values[key] || "").to_s)
  end
  format_string.gsub!(/\{\{country\}\}/, country_name)
  format_string.gsub(/\n$/, "")
end