units: support mm

Closes #161
dev
Andy Meneely 2016-06-27 11:10:39 -04:00
parent acd04b34a3
commit 4ea30c63e2
7 changed files with 77 additions and 2 deletions

View File

@ -1,6 +1,11 @@
# Squib CHANGELOG # Squib CHANGELOG
Squib follows [semantic versioning](http://semver.org). Squib follows [semantic versioning](http://semver.org).
## v0.11.0 / 2016-06-27
Features:
* Unit conversion supports mm (#161)
## v0.10.0 / 2016-05-06 ## v0.10.0 / 2016-05-06
Features: Features:

19
docs/dsl/mm.rst Normal file
View File

@ -0,0 +1,19 @@
mm
------
Given millimeters, returns the number of pixels according to the deck's DPI.
Parameters
^^^^^^^^^^
n
the number of mm
Examples
^^^^^^^^
.. code-block:: ruby
mm(1) # 11.811px (for default Deck::dpi of 300)
mm(2) + mm(1) # 35.433ox (for default Deck::dpi of 300)

View File

@ -1,6 +1,6 @@
Unit Conversion Unit Conversion
=============== ===============
By default, Squib thinks in pixels. This decision was made so that we can have pixel-perfect layouts without automatically scaling everything, even though working in units is sometimes easier. We provide some conversion methods, including looking for strings that end in "in" and "cm" and computing based on the current DPI. The dpi is set on `Squib::Deck.new` (not `config.yml`). By default, Squib thinks in pixels. This decision was made so that we can have pixel-perfect layouts without automatically scaling everything, even though working in units is sometimes easier. We provide some conversion methods, including looking for strings that end in "in", "cm", or "mm" and computing based on the current DPI. The dpi is set on `Squib::Deck.new` (not `config.yml`).
Example is in `samples/units.rb` found [here](https://github.com/andymeneely/squib/tree/master/samples/units.rb) Example is in `samples/units.rb` found [here](https://github.com/andymeneely/squib/tree/master/samples/units.rb)

View File

@ -13,5 +13,10 @@ module Squib
@dpi * Squib::INCHES_IN_CM * n.to_f @dpi * Squib::INCHES_IN_CM * n.to_f
end end
# DSL method. See http://squib.readthedocs.io
def mm(n)
@dpi * Squib::INCHES_IN_CM * n.to_f / 10.0
end
end end
end end

View File

@ -15,6 +15,8 @@ module Squib
arg.rstrip[0..-2].to_f * dpi arg.rstrip[0..-2].to_f * dpi
when /cm$/ # ends with "cm" when /cm$/ # ends with "cm"
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM
when /mm$/ # ends with "mm"
arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM / 10.0
else else
arg arg
end end

View File

@ -0,0 +1,37 @@
require 'spec_helper'
describe Squib::Deck do
let(:deck) { Squib::Deck.new }
context '#in' do
it 'converts inches properly' do
expect(deck.inches(1)).to eq 300
end
it 'handles strings too' do
expect(deck.inches('1')).to eq 300
end
end
context '#cm' do
it 'converts inches properly' do
expect(deck.cm(1)).to eq 118.1102361
end
it 'handles strings too' do
expect(deck.cm('1')).to eq 118.1102361
end
end
context '#mm' do
it 'converts inches properly' do
expect(deck.mm(1)).to eq 11.81102361
end
it 'handles strings too' do
expect(deck.mm('1')).to eq 11.81102361
end
end
end

View File

@ -15,8 +15,15 @@ describe Squib::Args::UnitConversion do
expect(subject.parse('1 in')).to eq(300) expect(subject.parse('1 in')).to eq(300)
end end
it 'does cm' do it 'does cm' do
expect(subject.parse('1cm')).to eq(118.1102361) expect(subject.parse('1cm')).to eq(118.1102361)
expect(subject.parse('1cm ')).to eq(118.1102361)
end end
it 'does mm' do
expect(subject.parse('1mm')).to eq(11.81102361)
expect(subject.parse('1mm ')).to eq(11.81102361)
end
end end