Browse Source

cleanup module definitions

dev
Andy Meneely 6 years ago
parent
commit
21bee3a3cb
  1. 13
      lib/squib/args/color_validator.rb
  2. 50
      lib/squib/args/coords.rb
  3. 29
      lib/squib/args/csv_opts.rb
  4. 19
      lib/squib/args/dir_validator.rb
  5. 27
      lib/squib/args/embed_adjust.rb
  6. 17
      lib/squib/args/embed_key.rb
  7. 47
      lib/squib/args/hand_special.rb
  8. 51
      lib/squib/args/import.rb
  9. 1
      lib/squib/args/paint.rb
  10. 186
      lib/squib/args/paragraph.rb
  11. 93
      lib/squib/args/save_batch.rb
  12. 57
      lib/squib/args/sprue_file.rb
  13. 180
      lib/squib/args/typographer.rb

13
lib/squib/args/color_validator.rb

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

50
lib/squib/args/coords.rb

@ -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
include ArgLoader
def self.parameters class Coords
{ x: 0, y: 0, include ArgLoader
x1: 100, y1: 100,
x2: 150, y2: 150,
x3: 100, y3: 150,
cx1: 0 , cy1: 0,
cx2: 0 , cy2: 0,
inner_radius: 50, outer_radius: 100,
radius: 100,
n: 5,
arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false,
}
end
def self.expanding_parameters def self.parameters
parameters.keys # all of them { x: 0, y: 0,
end x1: 100, y1: 100,
x2: 150, y2: 150,
x3: 100, y3: 150,
cx1: 0 , cy1: 0,
cx2: 0 , cy2: 0,
inner_radius: 50, outer_radius: 100,
radius: 100,
n: 5,
arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false,
}
end
def self.params_with_units 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 end
end end
end end

29
lib/squib/args/csv_opts.rb

@ -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) def initialize(opts)
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k} opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required) @hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
end end
def to_hash
@hash
end
private def to_hash
@hash
end
def required private
{ headers: true, converters: :numeric }
end
def required
{ headers: true, converters: :numeric }
end end
end end
end end

19
lib/squib/args/dir_validator.rb

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

27
lib/squib/args/embed_adjust.rb

@ -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
parameters.keys # all of them
end
def self.params_with_units 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 end
end end
end end

17
lib/squib/args/embed_key.rb

@ -1,17 +1,12 @@
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
end end

47
lib/squib/args/hand_special.rb

@ -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
{
angle_range: (Math::PI / -4.0)..(Math::PI / 4),
radius: :auto
}
end
def self.expanding_parameters def self.parameters
[] # none of them {
end angle_range: (Math::PI / -4.0)..(Math::PI / 4),
radius: :auto
}
end
def self.params_with_units def self.expanding_parameters
[ :radius ] [] # none of them
end end
def validate_radius(arg) def self.params_with_units
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto' [ :radius ]
arg end
end
def validate_radius(arg)
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto'
arg
end end
end end
end end

51
lib/squib/args/import.rb

@ -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
[] # none of them
end
def self.params_with_units def self.expanding_parameters
[] # none of them [] # none of them
end end
def validate_strip(arg) def self.params_with_units
raise 'Strip must be true or false' unless arg == true || arg == false [] # none of them
arg end
end
def validate_explode(arg) def validate_strip(arg)
arg raise 'Strip must be true or false' unless arg == true || arg == false
end arg
end
def strip? def validate_explode(arg)
strip arg
end end
def strip?
strip
end end
end end
end end

1
lib/squib/args/paint.rb

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

186
lib/squib/args/paragraph.rb

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

93
lib/squib/args/save_batch.rb

@ -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 def initialize
{ end
angle: 0,
count_format: '%02d',
dir: '_output',
prefix: 'card_',
rotate: false,
trim_radius: 0,
trim: 0,
}
end
def self.expanding_parameters def self.parameters
self.parameters.keys # all of them {
end angle: 0,
count_format: '%02d',
dir: '_output',
prefix: 'card_',
rotate: false,
trim_radius: 0,
trim: 0,
}
end
def self.params_with_units def self.expanding_parameters
[:trim, :trim_radius] self.parameters.keys # all of them
end end
def validate_dir(arg, _i) def self.params_with_units
ensure_dir_created(arg) [:trim, :trim_radius]
end end
def validate_rotate(arg, i) def validate_dir(arg, _i)
case arg ensure_dir_created(arg)
when true, :clockwise end
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) def validate_rotate(arg, i)
"#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png" 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
def summary def full_filename(i)
"#{dir[0]}/#{prefix[0]}_*" "#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png"
end end
def summary
"#{dir[0]}/#{prefix[0]}_*"
end end
end end
end end

57
lib/squib/args/sprue_file.rb

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

180
lib/squib/args/typographer.rb

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

Loading…
Cancel
Save