-
Notifications
You must be signed in to change notification settings - Fork 1
/
address.rb
91 lines (81 loc) · 2.34 KB
/
address.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Value object for addresses.
module Acts::Addressed
class Address
# List of readable attributes.
FIELDS = [:street, :street2, :city, :state, :zip, :coords]
FIELDS.each do |f|
attr_reader f
end
# Initializes a new #Address object from a #Hash of fields or separate arguments in the order given in FIELDS.
def initialize(*args)
case args.length
when 0
# do nothing
when 1
if args[0].kind_of?(Hash)
set_from_hash args[0]
else
raise ArgumentError, "expected Hash, got #{args[0].class.name}"
end
when FIELDS.length
set_from_array args
else
raise ArgumentError, "expected 0, 1, or #{FIELDS.length} arguments, got #{args.length}"
end
if !@state.blank? and !@state.kind_of?(State)
@state = State.find(@state)
end
end
# Returns the #Country that the address belongs to.
def country
state.country
end
# Returns the #State that the address belongs to, or a #QuietNil if there is no state.
def state
@state || QuietNil.instance
end
# Converts #Address to a #String.
#
# Valid values of +format+:
# <tt>:geo</tt>:: Address in comma-separated format for feeding to geocoder.
def to_s(format = :geo)
case format
when :geo
"#{street}, #{city}, #{state.code}, #{zip}, #{country.code}"
else
raise ArgumentError, "unknown format parameter: #{format}"
end
end
# Compares two Addresses by value.
def ==(other)
if other.kind_of?(Address)
FIELDS.each do |f|
if self.send(f) != other.send(f)
return false
end
end
return true
else
return false
end
end
protected
# TODO: set_from_* methods should perhaps become public.
# Sets instance variables from the supplied #Hash.
def set_from_hash(hash)
FIELDS.each do |f|
if hash.has_key?(f)
eval "@#{f.to_s} = hash[:#{f.to_s}]"
end
end
end
# Sets instance variables from the supplied #Array, in the same order as #FIELDS.
def set_from_array(array)
hash = {}
FIELDS.each_index do |i|
hash[FIELDS[i]] = array[i]
end
set_from_hash hash
end
end
end