Module: KeyDial::Coercion::Coercer

Included in:
Arrays, Hashes, Structs
Defined in:
lib/key_dial/coercion.rb

Instance Method Summary collapse

Instance Method Details

#to(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/key_dial/coercion.rb', line 6

def to(type)
	if type.is_a?(Class)
		if type == self.class || self.class < type
			return self
		elsif (type == Hash || type < Hash) && type.respond_to?(:from)
			return type.from(self)
		elsif (type == Array || type < Array) && type.respond_to?(:from)
			return type.from(self)
		elsif (type == Struct || type < Struct) && type.respond_to?(:from)
			return type.from(self)
		else
			raise ArgumentError, "Cannot coerce to " + type.to_s
		end
	else
		raise ArgumentError, "Must specify a class to coerce to."
	end
end