Skip to content

Commit

Permalink
Extract the value casting to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jan 31, 2013
1 parent 45883a3 commit 2b60a6b
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,38 @@ def connection_url_to_hash(url) # :nodoc:
:port => config.port,
:database => config.path.sub(%r{^/},""),
:host => config.host }

spec.reject!{ |_,value| value.blank? }

uri_parser = URI::Parser.new

spec.map { |key,value| spec[key] = uri_parser.unescape(value) if value.is_a?(String) }

if config.query
options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
# If anything looks numeric, make it numeric (e.g. pool count, timeout values, etc.)
options.map do |key,value|
options[key] = case value
when SIMPLE_INT
value.to_i
when SIMPLE_FLOAT
value.to_f
when 'true'
true
when 'false'
false
else
value
end
end

options.each { |key, value| options[key] = type_cast_value(value) }

spec.merge!(options)
end

spec
end

def type_cast_value(value)
case value
when SIMPLE_INT
value.to_i
when SIMPLE_FLOAT
value.to_f
when 'true'
true
when 'false'
false
else
value
end
end
end
end
end
Expand Down

0 comments on commit 2b60a6b

Please sign in to comment.