cleanup module definitions

dev
Andy Meneely 2020-03-16 17:08:10 -04:00
parent 77a694f797
commit 21bee3a3cb
13 changed files with 399 additions and 439 deletions

View File

@ -1,12 +1,7 @@
module Squib module Squib::Args::ColorValidator
# @api private
module Args
module ColorValidator
def colorify(color, custom_colors = {}) def colorify(color, custom_colors = {})
custom_colors[color.to_s] || color.to_s custom_colors[color.to_s] || color.to_s
end
end
end end
end end

View File

@ -1,35 +1,35 @@
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private module_function def extract_coord(opts, deck)
module Args Box.new.extract!(opts, deck)
end
class Coords class Coords
include ArgLoader include ArgLoader
def self.parameters def self.parameters
{ x: 0, y: 0, { x: 0, y: 0,
x1: 100, y1: 100, x1: 100, y1: 100,
x2: 150, y2: 150, x2: 150, y2: 150,
x3: 100, y3: 150, x3: 100, y3: 150,
cx1: 0 , cy1: 0, cx1: 0 , cy1: 0,
cx2: 0 , cy2: 0, cx2: 0 , cy2: 0,
inner_radius: 50, outer_radius: 100, inner_radius: 50, outer_radius: 100,
radius: 100, radius: 100,
n: 5, n: 5,
arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false, arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false,
} }
end end
def self.expanding_parameters def self.expanding_parameters
parameters.keys # all of them parameters.keys # all of them
end end
def self.params_with_units
parameters.keys # all of them
end
def self.params_with_units
parameters.keys # all of them
end end
end end
end end

View File

@ -1,25 +1,22 @@
require 'csv' require 'csv'
module Squib module Squib::Args
# @api private class CSV_Opts
module Args
class CSV_Opts
def initialize(opts)
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
end
def to_hash
@hash
end
private
def required
{ headers: true, converters: :numeric }
end
def initialize(opts)
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
end end
def to_hash
@hash
end
private
def required
{ headers: true, converters: :numeric }
end
end end
end end

View File

@ -1,16 +1,11 @@
module Squib module Squib::Args::DirValidator
# @api private
module Args
module DirValidator
def ensure_dir_created(dir)
unless Dir.exists?(dir)
Squib.logger.warn "Dir '#{dir}' does not exist, creating it."
FileUtils.mkdir_p dir
end
return dir
end
def ensure_dir_created(dir)
unless Dir.exists?(dir)
Squib.logger.warn "Dir '#{dir}' does not exist, creating it."
FileUtils.mkdir_p dir
end end
return dir
end end
end end

View File

@ -1,25 +1,22 @@
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private
module Args
class EmbedAdjust class EmbedAdjust
include ArgLoader include ArgLoader
def self.parameters def self.parameters
{ dx: 0, dy: 0 } { dx: 0, dy: 0 }
end end
def self.expanding_parameters def self.expanding_parameters
parameters.keys # all of them parameters.keys # all of them
end end
def self.params_with_units
parameters.keys # all of them
end
def self.params_with_units
parameters.keys # all of them
end end
end end
end end

View File

@ -1,16 +1,11 @@
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private class EmbedKey
module Args
class EmbedKey
# Validate the embed lookup key
def validate_key(str)
str.to_s
end
# Validate the embed lookup key
def validate_key(str)
str.to_s
end end
end end

View File

@ -1,37 +1,34 @@
require 'cairo' require 'cairo'
module Squib module Squib::Args
# @api private
module Args
class HandSpecial class HandSpecial
include ArgLoader include ArgLoader
def initialize(card_height) def initialize(card_height)
@card_height = card_height @card_height = card_height
end end
def self.parameters def self.parameters
{ {
angle_range: (Math::PI / -4.0)..(Math::PI / 4), angle_range: (Math::PI / -4.0)..(Math::PI / 4),
radius: :auto radius: :auto
} }
end end
def self.expanding_parameters def self.expanding_parameters
[] # none of them [] # none of them
end end
def self.params_with_units def self.params_with_units
[ :radius ] [ :radius ]
end end
def validate_radius(arg)
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto'
arg
end
def validate_radius(arg)
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto'
arg
end end
end end
end end

View File

@ -1,40 +1,37 @@
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private
module Args
class Import class Import
include ArgLoader include ArgLoader
def self.parameters def self.parameters
{ strip: true, { strip: true,
explode: 'qty' explode: 'qty'
} }
end end
def self.expanding_parameters def self.expanding_parameters
[] # none of them [] # none of them
end end
def self.params_with_units def self.params_with_units
[] # none of them [] # none of them
end end
def validate_strip(arg) def validate_strip(arg)
raise 'Strip must be true or false' unless arg == true || arg == false raise 'Strip must be true or false' unless arg == true || arg == false
arg arg
end end
def validate_explode(arg) def validate_explode(arg)
arg arg
end end
def strip?
strip
end
def strip?
strip
end end
end end
end end

View File

@ -6,6 +6,7 @@ module Squib::Args
module_function def extract_paint(opts, deck) module_function def extract_paint(opts, deck)
Paint.new(deck.custom_colors).extract!(opts, deck) Paint.new(deck.custom_colors).extract!(opts, deck)
end end
class Paint class Paint
include ArgLoader include ArgLoader
include ColorValidator include ColorValidator

View File

@ -1,118 +1,114 @@
require_relative '../constants' require_relative '../constants'
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private
module Args
class Paragraph class Paragraph
include ArgLoader include ArgLoader
def self.parameters
{ align: :left,
str: 'Hello, World!',
font: :use_set,
font_size: nil,
markup: false,
justify: false,
wrap: true,
ellipsize: :end,
spacing: nil,
valign: :top,
hint: :off
}
end
def self.parameters def self.expanding_parameters
{ align: :left, parameters.keys # all of them
str: 'Hello, World!', end
font: :use_set,
font_size: nil, def self.params_with_units
markup: false, [] # none of them
justify: false, end
wrap: true,
ellipsize: :end, def initialize(deck_font)
spacing: nil, @deck_font = deck_font
valign: :top, end
hint: :off
} def validate_str(arg, _i)
arg.to_s
end
def validate_font(arg, _i)
arg = @deck_font if arg == :use_set
arg = Squib::DEFAULT_FONT if arg == :default
arg
end
def validate_align(arg, _i)
case arg.to_s.downcase.strip
when 'left'
Pango::Alignment::LEFT
when 'right'
Pango::Alignment::RIGHT
when 'center'
Pango::Alignment::CENTER
else
raise ArgumentError, 'align must be one of: center, left, right'
end end
end
def self.expanding_parameters def validate_wrap(arg, _i)
parameters.keys # all of them case arg.to_s.downcase.strip
when 'word'
Pango::WrapMode::WORD
when 'char', 'false'
Pango::WrapMode::CHAR
when 'word_char', 'true'
Pango::WrapMode::WORD_CHAR
else
raise ArgumentError, 'wrap must be one of: word, char, word_char, true, or false'
end end
end
def self.params_with_units def validate_ellipsize(arg, _i)
[] # none of them case arg.to_s.downcase.strip
when 'none', 'false'
Pango::EllipsizeMode::NONE
when 'start'
Pango::EllipsizeMode::START
when 'middle'
Pango::EllipsizeMode::MIDDLE
when 'end', 'true'
Pango::EllipsizeMode::END
when 'autoscale'
:autoscale
else
raise ArgumentError, 'ellipsize must be one of: none, start, middle, end, true, false or autoscale'
end end
end
def initialize(deck_font) def validate_justify(arg, _i)
@deck_font = deck_font case arg
end when nil, true, false
def validate_str(arg, _i)
arg.to_s
end
def validate_font(arg, _i)
arg = @deck_font if arg == :use_set
arg = DEFAULT_FONT if arg == :default
arg arg
else
raise ArgumentError, 'justify must be one of: nil, true, or false'
end end
end
def validate_align(arg, _i) def validate_spacing(arg, _i)
case arg.to_s.downcase.strip return nil if arg.nil?
when 'left' raise ArgumentError, 'spacing must be a number or nil' unless arg.respond_to? :to_f
Pango::Alignment::LEFT arg.to_f * Pango::SCALE
when 'right' end
Pango::Alignment::RIGHT
when 'center' def validate_valign(arg, _i)
Pango::Alignment::CENTER if %w(top middle bottom).include? arg.to_s.downcase
else arg.to_s.downcase
raise ArgumentError, 'align must be one of: center, left, right' else
end raise ArgumentError, 'valign must be one of: top, middle, bottom'
end end
def validate_wrap(arg, _i)
case arg.to_s.downcase.strip
when 'word'
Pango::WrapMode::WORD
when 'char', 'false'
Pango::WrapMode::CHAR
when 'word_char', 'true'
Pango::WrapMode::WORD_CHAR
else
raise ArgumentError, 'wrap must be one of: word, char, word_char, true, or false'
end
end
def validate_ellipsize(arg, _i)
case arg.to_s.downcase.strip
when 'none', 'false'
Pango::EllipsizeMode::NONE
when 'start'
Pango::EllipsizeMode::START
when 'middle'
Pango::EllipsizeMode::MIDDLE
when 'end', 'true'
Pango::EllipsizeMode::END
when 'autoscale'
:autoscale
else
raise ArgumentError, 'ellipsize must be one of: none, start, middle, end, true, false or autoscale'
end
end
def validate_justify(arg, _i)
case arg
when nil, true, false
arg
else
raise ArgumentError, 'justify must be one of: nil, true, or false'
end
end
def validate_spacing(arg, _i)
return nil if arg.nil?
raise ArgumentError, 'spacing must be a number or nil' unless arg.respond_to? :to_f
arg.to_f * Pango::SCALE
end
def validate_valign(arg, _i)
if %w(top middle bottom).include? arg.to_s.downcase
arg.to_s.downcase
else
raise ArgumentError, 'valign must be one of: top, middle, bottom'
end
end
end end
end end
end end

View File

@ -1,63 +1,60 @@
require_relative 'arg_loader' require_relative 'arg_loader'
require_relative 'dir_validator' require_relative 'dir_validator'
module Squib module Squib::Args
# @api private class SaveBatch
module Args include ArgLoader
class SaveBatch include DirValidator
include ArgLoader
include DirValidator
def initialize
end
def self.parameters
{
angle: 0,
count_format: '%02d',
dir: '_output',
prefix: 'card_',
rotate: false,
trim_radius: 0,
trim: 0,
}
end
def self.expanding_parameters
self.parameters.keys # all of them
end
def self.params_with_units
[:trim, :trim_radius]
end
def validate_dir(arg, _i)
ensure_dir_created(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
def full_filename(i)
"#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png"
end
def summary
"#{dir[0]}/#{prefix[0]}_*"
end
def initialize
end end
def self.parameters
{
angle: 0,
count_format: '%02d',
dir: '_output',
prefix: 'card_',
rotate: false,
trim_radius: 0,
trim: 0,
}
end
def self.expanding_parameters
self.parameters.keys # all of them
end
def self.params_with_units
[:trim, :trim_radius]
end
def validate_dir(arg, _i)
ensure_dir_created(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
def full_filename(i)
"#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png"
end
def summary
"#{dir[0]}/#{prefix[0]}_*"
end
end end
end end

View File

@ -1,44 +1,41 @@
require_relative 'arg_loader' require_relative 'arg_loader'
module Squib module Squib::Args
# @api private class SprueFile
module Args include ArgLoader
class SprueFile
include ArgLoader
def initialize(dsl_method_default = {}) def initialize(dsl_method_default = {})
@dsl_method_default = dsl_method_default @dsl_method_default = dsl_method_default
end end
def self.parameters def self.parameters
{ {
sprue: nil sprue: nil
} }
end end
def self.expanding_parameters def self.expanding_parameters
[] []
end end
def self.params_with_units def self.params_with_units
[] # none of them [] # none of them
end end
def validate_template_file(arg) def validate_template_file(arg)
return nil if arg.nil? return nil if arg.nil?
thefile = File.exist?(arg) ? arg : builtin(arg) thefile = File.exist?(arg) ? arg : builtin(arg)
raise "File #{File.expand_path(arg)} does not exist!" unless raise "File #{File.expand_path(arg)} does not exist!" unless
File.exist? thefile File.exist? thefile
File.expand_path(thefile) File.expand_path(thefile)
end end
private private
def builtin(file) def builtin(file)
"#{File.dirname(__FILE__)}/../builtin/sprues/#{file}" "#{File.dirname(__FILE__)}/../builtin/sprues/#{file}"
end
end end
end end
end end

View File

@ -1,119 +1,115 @@
require_relative '../constants' require_relative '../constants'
module Squib module Squib::Args
# @api private # Internal class for handling arguments
module Args class Typographer
# Internal class for handling arguments
# @api private
class Typographer
def initialize(config = Conf::DEFAULTS) def initialize(config = Squib::Conf::DEFAULTS)
%w(lsquote ldquote rsquote rdquote smart_quotes %w(lsquote ldquote rsquote rdquote smart_quotes
em_dash en_dash ellipsis).each do |var| em_dash en_dash ellipsis).each do |var|
instance_variable_set("@#{var}", config[var]) instance_variable_set("@#{var}", config[var])
end
end end
def process(str)
str = explicit_replacements(str.to_s)
str = smart_quotes(str) if @smart_quotes
str
end
def explicit_replacements(str)
[ :left_curly, :right_curly, :apostraphize,
:ellipsificate, :em_dash, :en_dash ].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
end
str
end
def smart_quotes(str)
[ :single_inside_double_quote,
:right_double_quote,
:left_double_quote,
:right_single_quote,
:left_single_quote].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
end
str
end
# Iterate over each non-tag for processing
# Allows us to ignore anything inside < and >
def each_non_tag(str)
full_str = ''
tag_delimit = /(<(?:(?!<).)*>)/ # use non-capturing group w/ negative lookahead
str.split(tag_delimit).each do |token|
if token.start_with? '<'
full_str << token # don't process tags
else
full_str << yield(token)
end
end
return full_str
end
# Straightforward replace
def left_curly(str)
str.gsub('``', @ldquote)
end
# Straightforward replace
def right_curly(str)
str.gsub(%{''}, @rdquote)
end
# A quote between two letters is an apostraphe
def apostraphize(str)
str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
end
# Straightforward replace
def ellipsificate(str)
str.gsub('...', @ellipsis)
end
# Straightforward replace
def en_dash(str)
str.gsub('--', @en_dash)
end
# Straightforward replace
def em_dash(str)
str.gsub('---', @em_dash)
end
# Quote next to non-whitespace curls
def right_double_quote(str)
str.gsub(/(\S)(\")/, '\1' + @rdquote)
end
# Quote next to non-whitespace curls
def left_double_quote(str)
str.gsub(/(\")(\S)/, @ldquote + '\2')
end
# Handle the cases where a double quote is next to a single quote
def single_inside_double_quote(str)
str.gsub(/(\")(\')(\S)/, @ldquote + @lsquote + '\3')
.gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
end
# Quote next to non-whitespace curls
def right_single_quote(str)
str.gsub(/(\S)(\')/, '\1' + @rsquote)
end
# Quote next to non-whitespace curls
def left_single_quote(str)
str.gsub(/(\')(\S)/, @lsquote + '\2')
end
end end
def process(str)
str = explicit_replacements(str.to_s)
str = smart_quotes(str) if @smart_quotes
str
end
def explicit_replacements(str)
[ :left_curly, :right_curly, :apostraphize,
:ellipsificate, :em_dash, :en_dash ].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
end
str
end
def smart_quotes(str)
[ :single_inside_double_quote,
:right_double_quote,
:left_double_quote,
:right_single_quote,
:left_single_quote].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
end
str
end
# Iterate over each non-tag for processing
# Allows us to ignore anything inside < and >
def each_non_tag(str)
full_str = ''
tag_delimit = /(<(?:(?!<).)*>)/ # use non-capturing group w/ negative lookahead
str.split(tag_delimit).each do |token|
if token.start_with? '<'
full_str << token # don't process tags
else
full_str << yield(token)
end
end
return full_str
end
# Straightforward replace
def left_curly(str)
str.gsub('``', @ldquote)
end
# Straightforward replace
def right_curly(str)
str.gsub(%{''}, @rdquote)
end
# A quote between two letters is an apostraphe
def apostraphize(str)
str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
end
# Straightforward replace
def ellipsificate(str)
str.gsub('...', @ellipsis)
end
# Straightforward replace
def en_dash(str)
str.gsub('--', @en_dash)
end
# Straightforward replace
def em_dash(str)
str.gsub('---', @em_dash)
end
# Quote next to non-whitespace curls
def right_double_quote(str)
str.gsub(/(\S)(\")/, '\1' + @rdquote)
end
# Quote next to non-whitespace curls
def left_double_quote(str)
str.gsub(/(\")(\S)/, @ldquote + '\2')
end
# Handle the cases where a double quote is next to a single quote
def single_inside_double_quote(str)
str.gsub(/(\")(\')(\S)/, @ldquote + @lsquote + '\3')
.gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
end
# Quote next to non-whitespace curls
def right_single_quote(str)
str.gsub(/(\S)(\')/, '\1' + @rsquote)
end
# Quote next to non-whitespace curls
def left_single_quote(str)
str.gsub(/(\')(\S)/, @lsquote + '\2')
end
end end
end end