Unit conversion handles trailing whitespace

dev
Andy Meneely 2014-12-31 00:57:19 -05:00
parent c8847bcfaf
commit 574f99af12
2 changed files with 16 additions and 9 deletions

View File

@ -200,11 +200,11 @@ module Squib
Squib::UNIT_CONVERSION_PARAMS.each_pair do |param_name, api_param| Squib::UNIT_CONVERSION_PARAMS.each_pair do |param_name, api_param|
if needed_params.include? param_name if needed_params.include? param_name
opts[api_param].each_with_index do |arg, i| opts[api_param].each_with_index do |arg, i|
case arg.to_s case arg.to_s.rstrip
when /in$/ #ends with "in" when /in$/ #ends with "in"
opts[api_param][i] = arg[0..-2].to_f * @dpi opts[api_param][i] = arg.rstrip[0..-2].to_f * @dpi
when /cm$/ #ends with "cm" when /cm$/ #ends with "cm"
opts[api_param][i] = arg[0..-2].to_f * @dpi * @@INCHES_IN_CM opts[api_param][i] = arg.rstrip[0..-2].to_f * @dpi * @@INCHES_IN_CM
end end
end end
end end

View File

@ -167,6 +167,13 @@ describe Squib::InputHelpers do
expect(opts).to eq({:x => [156.0, 600.0]}) #assume 300dpi default expect(opts).to eq({:x => [156.0, 600.0]}) #assume 300dpi default
end end
it 'handles whitespace' do
args = {x: ['1in ']}
needed_params = [:x]
opts = @deck.send(:convert_units, args, needed_params)
expect(opts).to eq({:x => [300.0] }) #assume 300dpi default
end
it 'converts centimeters' do it 'converts centimeters' do
args = {x: ['2cm']} args = {x: ['2cm']}
needed_params = [:x] needed_params = [:x]
@ -177,12 +184,12 @@ describe Squib::InputHelpers do
end end
context '#rowify' do context '#rowify' do
# it 'does nothing on an integer' do it 'does nothing on an integer' do
# opts = @deck.send(:rowify, {columns: 2, rows: 2}) opts = @deck.send(:rowify, {columns: 2, rows: 2})
# expect(opts).to eq({ columns: 2, expect(opts).to eq({ columns: 2,
# rows: 2 rows: 2
# }) })
# end end
it 'computes properly on non-integer' do it 'computes properly on non-integer' do
opts = @deck.send(:rowify, {columns: 1, rows: :infinite}) opts = @deck.send(:rowify, {columns: 1, rows: :infinite})