Class: Address
- Inherits:
-
Object
- Object
- Address
- Defined in:
- lib/address.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
Returns the value of attribute city.
-
#country ⇒ Object
Returns the value of attribute country.
-
#label ⇒ Object
Returns the value of attribute label.
-
#postal_code ⇒ Object
Returns the value of attribute postal_code.
-
#state ⇒ Object
Returns the value of attribute state.
-
#street_1 ⇒ Object
Returns the value of attribute street_1.
-
#street_2 ⇒ Object
Returns the value of attribute street_2.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Address
constructor
A new instance of Address.
- #to_hash(opts = {}) ⇒ Object
- #to_s(format = nil) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Address
Returns a new instance of Address.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/address.rb', line 12 def initialize(*args) case args.first when Hash fields = args.first self.label = fields[:label] self.street_1 = fields[:street_1] self.street_2 = fields[:street_2] self.city = fields[:city] self.state = fields[:state] self.postal_code = fields[:postal_code] || fields[:zip] self.country = fields[:country] else # Constructor for Rails mapper self.label = args[0] self.street_1 = args[1] self.street_2 = args[2] self.city = args[3] self.state = args[4] self.postal_code = args[5] self.country = args[6] end end |
Instance Attribute Details
#city ⇒ Object
Returns the value of attribute city.
5 6 7 |
# File 'lib/address.rb', line 5 def city @city end |
#country ⇒ Object
Returns the value of attribute country.
8 9 10 |
# File 'lib/address.rb', line 8 def country @country end |
#label ⇒ Object
Returns the value of attribute label.
2 3 4 |
# File 'lib/address.rb', line 2 def label @label end |
#postal_code ⇒ Object
Returns the value of attribute postal_code.
7 8 9 |
# File 'lib/address.rb', line 7 def postal_code @postal_code end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/address.rb', line 6 def state @state end |
#street_1 ⇒ Object
Returns the value of attribute street_1.
3 4 5 |
# File 'lib/address.rb', line 3 def street_1 @street_1 end |
#street_2 ⇒ Object
Returns the value of attribute street_2.
4 5 6 |
# File 'lib/address.rb', line 4 def street_2 @street_2 end |
Instance Method Details
#to_hash(opts = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/address.rb', line 61 def to_hash(opts={}) fields = [:label, :street_1, :street_2, :city, :state, :postal_code, :country] = { :exclude => [] } if opts.blank? if [:include].present? fields = [:include] elsif [:exclude].present? fields -= [:exclude] end hash = {} for f in fields do hash[f] = send f end hash end |
#to_s(format = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/address.rb', line 35 def to_s(format=nil) = {:label => true, :country => true, :delimiter => ", "} case format when Hash .merge! format when Symbol case format when :one_line; [:delimiter] = ", " when :multi_line; [:delimiter] = "\n" end end fields = [] fields << :label if [:label] fields += [:street_1, :street_2, :city_state_post] fields << :country if [:country] city_state_post = nil unless city.blank? and state.blank? and postal_code.blank? city_state_post = "#{city}, #{state} #{postal_code}" end fields.map {|a| eval a.to_s }.reject(&:blank?).join [:delimiter] end |