Module: Somewhere::ClassMethods

Defined in:
lib/somewhere.rb

Instance Method Summary collapse

Instance Method Details

#address(method = nil, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/somewhere.rb', line 9

def address method=nil, opts = {}
	options = { :include_prefix => true, :label => false, :country => true }
	options.merge! opts
	
	if method.blank?
		options[:include_prefix] = false
		method = :address
	end

mapping = [:label, :street_1, :street_2, :city, :state, :postal_code, :country]

mapping.collect! { |f|
	options[f] ||= options[:include_prefix] ? method.to_s+'_#{f}' : f.to_s unless options[f] == false
	[options[f], f.to_s] unless options[f] == false
}.compact!

composed_of method.to_sym,
	:class_name => "Address",
	:allow_nil => true,
	:mapping => mapping,
	:converter => Proc.new { |value| value.respond_to?(:to_address) ? (value.to_address unless value.blank?) : raise(ArgumentError, "Can't convert #{value.class} to Address") }
end