diff --git a/CHANGELOG.md b/CHANGELOG.md index 423d780..6862495 100644 --- a/CHANGELOG.md +++ b/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: diff --git a/docs/dsl/mm.rst b/docs/dsl/mm.rst new file mode 100644 index 0000000..cf381cc --- /dev/null +++ b/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) diff --git a/docs/units.rst b/docs/units.rst index e79f6e0..f0a78dc 100644 --- a/docs/units.rst +++ b/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) diff --git a/lib/squib/api/units.rb b/lib/squib/api/units.rb index e90b36b..9ab24a6 100644 --- a/lib/squib/api/units.rb +++ b/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 diff --git a/lib/squib/args/unit_conversion.rb b/lib/squib/args/unit_conversion.rb index 42a77cd..88d82fd 100644 --- a/lib/squib/args/unit_conversion.rb +++ b/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 diff --git a/spec/api/api_units_spec.rb b/spec/api/api_units_spec.rb new file mode 100644 index 0000000..bf62c26 --- /dev/null +++ b/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 diff --git a/spec/args/unit_conversion_spec.rb b/spec/args/unit_conversion_spec.rb index 82bc248..bdbf9e7 100644 --- a/spec/args/unit_conversion_spec.rb +++ b/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