From 5c6149ad5a3aec9dcab4e9df9f857eea31f93423 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 29 Jul 2014 23:50:56 -0400 Subject: [PATCH] Better handling of empty files --- lib/squib/api/image.rb | 4 ++-- lib/squib/graphics/image.rb | 2 ++ lib/squib/input_helpers.rb | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index c0d5205..36ae0f0 100644 --- a/lib/squib/api/image.rb +++ b/lib/squib/api/image.rb @@ -9,7 +9,7 @@ module Squib # png file: 'img.png', x: 50, y: 50 # # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} - # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files} + # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:API.md#Specifying+Files Specifying Files} # @option opts x [Integer] (0) the x-coordinate to place # @option opts y [Integer] (0) the y-coordinate to place # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:API.md#label-Custom+Layouts Custom Layouts} @@ -30,7 +30,7 @@ module Squib # svg 1..2, 'icon.svg', '#stone', x: 50, y:50 # # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} - # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files} + # @option opts file [String, Array] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:API.md#Specifying+Files Specifying Files} # @option opts x [Integer] (0) the x-coordinate to place # @option opts y [Integer] (0) the y-coordinate to place # @option opts width [Integer] (:native) the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document. diff --git a/lib/squib/graphics/image.rb b/lib/squib/graphics/image.rb index 22a1f6d..5119ce8 100644 --- a/lib/squib/graphics/image.rb +++ b/lib/squib/graphics/image.rb @@ -4,6 +4,7 @@ module Squib # :nodoc: # @api private def png(file, x, y, alpha) + return if file.nil? or file.eql? '' cc = cairo_context png = Cairo::ImageSurface.from_png(file) cc.set_source(png, x, y) @@ -13,6 +14,7 @@ module Squib # :nodoc: # @api private def svg(file, id, x, y, width, height) + return if file.nil? or file.eql? '' svg = RSVG::Handle.new_from_file(file) width = svg.width if width == :native height = svg.height if height == :native diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 50c6b30..ebe36c6 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -12,8 +12,8 @@ module Squib opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = rangeify(opts) if params.include? :range opts = fileify(opts) if params.include? :file - opts = fileify(opts, false, true) if params.include? :file_to_save - opts = fileify(opts, true) if params.include? :files + opts = fileify(opts, false, false) if params.include? :file_to_save + opts = fileify(opts, true, false) if params.include? :files opts = colorify(opts) if params.include? :color opts = colorify(opts, true) if params.include? :nillable_color opts = dirify(opts) if params.include? :dir @@ -68,11 +68,11 @@ module Squib # :nodoc: # @api private - def fileify(opts, expand_singletons=false, allow_non_exist=false) - opts[:file] = [opts[:file]] * @cards.size if expand_singletons + def fileify(opts, expand_singletons=false, file_must_exist=true) + opts[:file] = [opts[:file]] * @cards.size if expand_singletons && !(opts[:file].respond_to? :each) files = [opts[:file]].flatten files.each do |file| - unless File.exists?(file) || allow_non_exist + if file_must_exist and !File.exists?(file) raise "File #{File.expand_path(file)} does not exist!" end end