3 changed files with 45 additions and 6 deletions
@ -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 |
||||
@ -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