Last active
August 29, 2015 14:11
-
-
Save thiagoa/ec192babb1d2afc34d70 to your computer and use it in GitHub Desktop.
Address decorator extracted from a project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'delegate' | |
module ConcatHelper | |
def concat(*elements, separator) | |
elements.reject(&:blank?).join(separator) | |
end | |
def format(format_string, string) | |
format_string % string unless string.blank? | |
end | |
end | |
class AddressTemplate < SimpleDelegator | |
include ConcatHelper | |
def to_s | |
display_address | |
end | |
private | |
alias address __getobj__ | |
def display_address | |
return '' unless address | |
number_complement = concat(number, complement, ' ') | |
city_state = concat(city, state, ' / ') | |
district_city_state = concat(district, city_state, ' - ') | |
concat(street, number_complement, district_city_state, ', ') | |
end | |
def number | |
format 'Nº %s', address.number | |
end | |
def complement | |
format '(%s)', address.complement | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment