Class: Addressing::DefaultFormatter
- Inherits:
-
Object
- Object
- Addressing::DefaultFormatter
- Defined in:
- lib/addressing/default_formatter.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_LOCALE =
"en"- FORMAT_PLACEHOLDER_PATTERN =
/%[a-z1-9_]+/- LEADING_TRAILING_PUNCTUATION_PATTERN =
/\A[ \-,]+|[ \-,]+\z/- MULTIPLE_SPACES_PATTERN =
/\s\s+/- DEFAULT_OPTIONS =
{ locale: DEFAULT_LOCALE, html: true, html_tag: "p", html_attributes: {translate: "no"} }
Instance Method Summary collapse
- #format(address, options = {}) ⇒ Object
-
#initialize(default_options = {}) ⇒ DefaultFormatter
constructor
A new instance of DefaultFormatter.
Constructor Details
#initialize(default_options = {}) ⇒ DefaultFormatter
Returns a new instance of DefaultFormatter.
17 18 19 20 21 22 |
# File 'lib/addressing/default_formatter.rb', line 17 def initialize( = {}) () @default_options = self.class::DEFAULT_OPTIONS.merge() @country_list_cache = {} end |
Instance Method Details
#format(address, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/addressing/default_formatter.rb', line 24 def format(address, = {}) () = @default_options.merge() address_format = AddressFormat.get(address.country_code) # Add the country to the bottom or the top of the format string, # depending on whether the format is minor-to-major or major-to-minor. format_string = if Locale.match_candidates(address_format.locale, address.locale) "%country\n" + address_format.local_format else address_format.format + "\n%country" end view = build_view(address, address_format, ) view = render_view(view) replacements = view.map { |key, element| ["%#{key}", element] }.to_h output = format_string.gsub(FORMAT_PLACEHOLDER_PATTERN) { |m| replacements[m] } output = clean_output(output) if [:html] output = output.gsub("\n", "<br>\n") # Add the HTML wrapper element. output = render_html_element(value: "\n#{output}\n", html_tag: [:html_tag], html_attributes: [:html_attributes]) end output end |