Sha256: 4b5105bc21f63a840ca3d993bc017473f5e3335e976e7bf3c05364ac38b64051

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

module Biggs
  class Formatter
    
    FIELDS = [:recipient, :street, :city, :state, :zip, :country]
    
    def initialize(options={})
      @blank_country_on = [options[:blank_country_on]].compact.flatten.map{|s| s.to_s.downcase}
    end
  
    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
  
    attr_accessor :blank_country_on, :default_country_without_state, :default_country_with_state
  
    private
    
    def default_format_string(state)
      state && state != "" ?
        Biggs.formats[default_country_with_state || "us"] :
        Biggs.formats[default_country_without_state || "fr"]
    end
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
biggs-0.4.0 lib/biggs/formatter.rb
biggs-0.3.3 lib/biggs/formatter.rb
biggs-0.3.2 lib/biggs/formatter.rb
biggs-0.3.1 lib/biggs/formatter.rb
biggs-0.3.0 lib/biggs/formatter.rb