Browse Source

units: support mm

Closes #161
dev
Andy Meneely 10 years ago
parent
commit
4ea30c63e2
  1. 5
      CHANGELOG.md
  2. 19
      docs/dsl/mm.rst
  3. 2
      docs/units.rst
  4. 5
      lib/squib/api/units.rb
  5. 2
      lib/squib/args/unit_conversion.rb
  6. 37
      spec/api/api_units_spec.rb
  7. 9
      spec/args/unit_conversion_spec.rb

5
CHANGELOG.md

@ -1,6 +1,11 @@
# Squib CHANGELOG
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
Features:

19
docs/dsl/mm.rst

@ -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)

2
docs/units.rst

@ -1,6 +1,6 @@
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)

5
lib/squib/api/units.rb

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

2
lib/squib/args/unit_conversion.rb

@ -15,6 +15,8 @@ module Squib
arg.rstrip[0..-2].to_f * dpi
when /cm$/ # ends with "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
arg
end

37
spec/api/api_units_spec.rb

@ -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

9
spec/args/unit_conversion_spec.rb

@ -15,8 +15,15 @@ describe Squib::Args::UnitConversion do
expect(subject.parse('1 in')).to eq(300)
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)
end
it 'does mm' do
expect(subject.parse('1mm')).to eq(11.81102361)
expect(subject.parse('1mm ')).to eq(11.81102361)
end
end

Loading…
Cancel
Save