Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active August 29, 2015 14:11
Show Gist options
  • Save thiagoa/ec192babb1d2afc34d70 to your computer and use it in GitHub Desktop.
Save thiagoa/ec192babb1d2afc34d70 to your computer and use it in GitHub Desktop.
Address decorator extracted from a project
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