Skip to content

Instantly share code, notes, and snippets.

@therealjasonkenney
Created June 28, 2014 23:58
Show Gist options
  • Save therealjasonkenney/8c37799b40be7a680383 to your computer and use it in GitHub Desktop.
Save therealjasonkenney/8c37799b40be7a680383 to your computer and use it in GitHub Desktop.
Curious...
#!env ruby
class Address
attr_accessor :street1, :street2, :city, :state, :zip
def initialize(attributes = {})
attributes.each do |k,v|
self.send(:"#{k}=", v)
end
end
end
module AddressDecorator
def address
"#{street1}\n" << if street2
"#{street2}\n"
else
''
end << "#{city},#{state}\n#{zip}"
end
def street1
titleize(super)
end
def street2
titleize(super) if super
end
def state
case super
when 'MA'; 'Assachusetts'
when 'NY'; 'Moo York'
when 'RI'; raise 'Invalid loser'
else; 'I don\'t know'
end
end
private
def titleize(s)
s.split(/(\W)/).map(&:capitalize).join
end
end
a = Address.new(:street1 => '123 Baker St.', :city => 'Omaha', :state => 'MA', :zip => '02561')
b = Address.new(:street1 => '123 Baker St.', :city => 'Omaha', :state => 'MA ', :zip => '02561')
b.extend AddressDecorator
puts "#{b.address}"
puts "#{a.address}" if a.respond_to?(:address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment