diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb index cd93636..0d5fa2c 100644 --- a/lib/squib/api/save.rb +++ b/lib/squib/api/save.rb @@ -6,6 +6,7 @@ module Squib # Saves the given range of cards to either PNG or PDF # + # # @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges} # @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist. # @option opts [Symbol] format (:png) the format that this will be rendered too. Options `:pdf, :png`. Array of both is allowed: `[:pdf, :png]` @@ -24,6 +25,8 @@ module Squib # @example # save range: 1..8, dir: '_pnp', prefix: 'bw_' # + # Options support Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion} + # # @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges} # @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist. # @option opts [String] prefix (card_) the prefix of the file name to be printed. @@ -34,10 +37,9 @@ module Squib def save_png(opts = {}) range = Args::CardRange.new(opts[:range], deck_size: size) batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) - opts = needs(opts,[:range, :creatable_dir, :prefix, :count_format, :rotate]) - @progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar| + @progress_bar.start("Saving PNGs to #{batch.dir}/#{batch.count_format}*", size) do |bar| range.each do |i| - @cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:count_format], opts[:rotate], opts[:angle]) + @cards[i].save_png(batch[i]) bar.increment end end diff --git a/lib/squib/args/save_batch.rb b/lib/squib/args/save_batch.rb index dce29ce..768ae3f 100644 --- a/lib/squib/args/save_batch.rb +++ b/lib/squib/args/save_batch.rb @@ -6,7 +6,7 @@ module Squib class SaveBatch include ArgLoader - def initialize #TODO DSL method default for prefix + def initialize end def self.parameters @@ -14,18 +14,19 @@ module Squib prefix: 'card_', count_format: '%02d', rotate: false, + angle: 0, } end def self.expanding_parameters - [] # none of them + self.parameters.keys # all of them end def self.params_with_units [] # none of them end - def validate_dir(arg) + def validate_dir(arg, _i) unless Dir.exists?(arg) Squib.logger.warn("Dir '#{arg}' does not exist, creating it.") Dir.mkdir arg @@ -33,6 +34,21 @@ module Squib return arg end + def validate_rotate(arg, i) + case arg + when true, :clockwise + angle[i] = 0.5 * Math::PI + return true + when :counterclockwise + angle[i] = 1.5 * Math::PI + return true + when false + false + else + raise 'invalid option to rotate: only [true, false, :clockwise, :counterclockwise]' + end + end + end end end diff --git a/lib/squib/graphics/save_images.rb b/lib/squib/graphics/save_images.rb index b915045..15f672d 100644 --- a/lib/squib/graphics/save_images.rb +++ b/lib/squib/graphics/save_images.rb @@ -3,13 +3,13 @@ module Squib # :nodoc: # @api private - def save_png(i, dir, prefix, count_format, do_rotate, angle) - if [true, :clockwise, :counterclockwise].include?(do_rotate) - surface = rotated_image(angle) - else - surface = @cairo_surface - end - write_png(surface, i, dir, prefix, count_format) + def save_png(batch) + surface = if batch.rotate + rotated_image(batch.angle) + else + surface = @cairo_surface + end + write_png(surface, index, batch.dir, batch.prefix, batch.count_format) end # :nodoc: diff --git a/spec/args/save_batch_spec.rb b/spec/args/save_batch_spec.rb index 086cc6b..91557d5 100644 --- a/spec/args/save_batch_spec.rb +++ b/spec/args/save_batch_spec.rb @@ -5,17 +5,47 @@ describe Squib::Args::SaveBatch do subject(:save_batch) {Squib::Args::SaveBatch.new} context 'dir' do - it 'is created if not exists (and warns)' do opts = {dir: 'tocreate'} Dir.chdir(output_dir) do FileUtils.rm_rf('tocreate', secure: true) expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once save_batch.load! opts - expect(save_batch).to have_attributes({dir: 'tocreate'}) + expect(save_batch).to have_attributes({dir: ['tocreate']}) expect(Dir.exists? 'tocreate').to be true end end + end + + context 'rotate' do + it 'does nothing by default' do + opts = {} + save_batch.load! opts + expect(save_batch[0]).to have_attributes({rotate: false, angle: 0}) + end + + it 'rotates by pi/2 with true' do + opts = {rotate: true} + save_batch.load! opts + expect(save_batch[0]).to have_attributes({rotate: true, angle: Math::PI / 2}) + end + it 'rotates by pi/2' do + opts = {rotate: :clockwise} + save_batch.load! opts + expect(save_batch[0]).to have_attributes({rotate: true, angle: Math::PI / 2}) + end + + it 'rotates by pi/2 with counterclockwise' do + opts = {rotate: :counterclockwise} + save_batch.load! opts + expect(save_batch[0]).to have_attributes({rotate: true, angle: 3 * Math::PI / 2}) + end + + it 'raises error on a number' do + opts = {rotate: 5.0} + expect { save_batch.load!(opts) }.to raise_error('invalid option to rotate: only [true, false, :clockwise, :counterclockwise]') + end end + end