Class: Addressing::DefaultFormatter
- Inherits:
-
Object
- Object
- Addressing::DefaultFormatter
- Defined in:
- lib/addressing/default_formatter.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_OPTIONS =
{ locale: "en", 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.
12 13 14 15 16 |
# File 'lib/addressing/default_formatter.rb', line 12 def initialize( = {}) () @default_options = self.class::DEFAULT_OPTIONS.merge() end |
Instance Method Details
#format(address, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/addressing/default_formatter.rb', line 18 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(/%[a-z1-9_]+/) { |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 |