Class: Address

Inherits:
Object
  • Object
show all
Defined in:
lib/address.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/address.rb', line 5

def city
  @city
end

#countryObject

Returns the value of attribute country.



8
9
10
# File 'lib/address.rb', line 8

def country
  @country
end

#labelObject

Returns the value of attribute label.



2
3
4
# File 'lib/address.rb', line 2

def label
  @label
end

#postal_codeObject

Returns the value of attribute postal_code.



7
8
9
# File 'lib/address.rb', line 7

def postal_code
  @postal_code
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/address.rb', line 6

def state
  @state
end

#street_1Object

Returns the value of attribute street_1.



3
4
5
# File 'lib/address.rb', line 3

def street_1
  @street_1
end

#street_2Object

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]
	options = { :exclude => [] } if opts.blank?
	
	if options[:include].present?
		fields = options[:include]
	elsif options[:exclude].present?
		fields -= options[: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)
	options = {:label => true, :country => true, :delimiter => ", "}
	
	case format
		when Hash
			options.merge! format
		when Symbol
			case format
				when :one_line; options[:delimiter] = ", "
				when :multi_line; options[:delimiter] = "\n"
			end
	end
	
	fields = []
	fields << :label if options[:label]
	fields += [:street_1, :street_2, :city_state_post]
	fields << :country if options[: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 options[:delimiter]
end