Refactoring out the unit conversion to its own place
parent
4ecbaa4e31
commit
1896689614
|
|
@ -0,0 +1,21 @@
|
||||||
|
require 'squib/constants'
|
||||||
|
|
||||||
|
module Squib
|
||||||
|
module Args
|
||||||
|
module UnitConversion
|
||||||
|
|
||||||
|
module_function
|
||||||
|
def parse(arg, dpi=300)
|
||||||
|
case arg.to_s.rstrip
|
||||||
|
when /in$/ #ends with "in"
|
||||||
|
arg.rstrip[0..-2].to_f * dpi
|
||||||
|
when /cm$/ #ends with "cm"
|
||||||
|
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM
|
||||||
|
else
|
||||||
|
arg
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
require 'squib/constants'
|
require 'squib/constants'
|
||||||
|
require 'squib/args/unit_conversion'
|
||||||
|
|
||||||
module Squib
|
module Squib
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
|
@ -200,12 +201,7 @@ 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.rstrip
|
opts[api_param][i] = Args::UnitConversion.parse(arg, @dpi)
|
||||||
when /in$/ #ends with "in"
|
|
||||||
opts[api_param][i] = arg.rstrip[0..-2].to_f * @dpi
|
|
||||||
when /cm$/ #ends with "cm"
|
|
||||||
opts[api_param][i] = arg.rstrip[0..-2].to_f * @dpi * Squib::INCHES_IN_CM
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require 'squib/args/unit_conversion'
|
||||||
|
|
||||||
|
describe Squib::Args::UnitConversion do
|
||||||
|
|
||||||
|
it 'does nothing on just numbers' do
|
||||||
|
expect(subject.parse(20)).to eq(20)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'strips trailing whitespace' do
|
||||||
|
expect(subject.parse('1in ')).to eq(300)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is ok w/internal whitesapce' do
|
||||||
|
expect(subject.parse('1 in')).to eq(300)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does cm' do
|
||||||
|
expect(subject.parse('1cm')).to eq(118.1102361)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue