Browse Source

built a nifty, useful error message thingy

dev
Andy Meneely 6 years ago
parent
commit
871674c909
  1. 15
      lib/squib/api/background.rb
  2. 5
      lib/squib/deck.rb
  3. 35
      lib/squib/dsl/background.rb
  4. 0
      lib/squib/errors_warnings/caller_finder.rb
  5. 12
      lib/squib/errors_warnings/warn_unexpected_params.rb
  6. 4
      samples/colors/_switch_color.rb

15
lib/squib/api/background.rb

@ -1,15 +0,0 @@
require_relative '../args/card_range'
require_relative '../args/draw'
module Squib
class Deck
# DSL method. See http://squib.readthedocs.io
def background(opts = {})
range = Args::CardRange.new(opts[:range], deck_size: size)
draw = Args::Draw.new(custom_colors).load!(opts, expand_by: size, layout: layout, dpi: dpi)
range.each { |i| @cards[i].background(draw.color[i]) }
end
end
end

5
lib/squib/deck.rb

@ -33,7 +33,7 @@ module Squib
:img_dir, :prefix, :text_hint, :typographer
# :nodoc:
# @api private
attr_reader :layout, :conf, :dpi, :font
attr_reader :layout, :conf, :dpi, :font, :caller_loc
#
# deck.size is really just @cards.size
@ -69,6 +69,7 @@ module Squib
@height = Args::UnitConversion.parse height, dpi
cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
@layout = LayoutParser.new(dpi).load_layout(layout)
@caller_loc = caller_locations[0] # useful for error messages
enable_groups_from_env!
if block_given?
instance_eval(&block) # here we go. wheeeee!
@ -102,7 +103,7 @@ module Squib
##################
### PUBLIC API ###
##################
require_relative 'api/background'
require_relative 'dsl/background'
require_relative 'api/data'
require_relative 'api/groups'
require_relative 'api/image'

35
lib/squib/dsl/background.rb

@ -0,0 +1,35 @@
require_relative '../errors_warnings/warn_unexpected_params'
module Squib
class Deck
def background(opts = {}) # DSL method. See http://squib.readthedocs.io
BackgroundDSLMethod.new(self).run(opts)
end
end
class BackgroundDSLMethod
include WarnUnexpectedParam
def initialize(deck)
@error_cxt = <<~EOS.split("\n").join(' ').strip
to Squib method '#{caller_locations[1].label}'
from #{caller_locations[2].path}:#{caller_locations[2].lineno}
EOS
@deck = deck
end
def accepted_params
[
:range,
:color
]
end
def run(opts)
warn_unexpected_params(opts)
range = Args::CardRange.new(opts[:range], deck_size: @deck.size)
draw = Args::Draw.new(@deck.custom_colors).load!(opts, expand_by: @deck.size, layout: @deck.layout, dpi: @deck.dpi)
range.each { |i| @deck.cards[i].background(draw.color[i]) }
end
end
end

0
lib/squib/errors_warnings/caller_finder.rb

12
lib/squib/errors_warnings/warn_unexpected_params.rb

@ -0,0 +1,12 @@
require_relative 'caller_finder'
module Squib::WarnUnexpectedParam
def warn_unexpected_params(opts)
unexpected = opts.keys - accepted_params
unexpected.each do |key|
Squib.logger.warn do
"Unexpected option '#{key}' #{@error_cxt} ...ignoring"
end
end
end
end

4
samples/colors/_switch_color.rb

@ -1,4 +1,4 @@
require 'squib'
require_relative '../../lib/squib'
# Choose between black and white color theme for type snake
# * Allow using white snake cards with black text or
@ -16,7 +16,7 @@ Squib::Deck.new cards: cards['Type'].size do
"white"
end
end
background color: background_color
background color: background_color, foo: 'hi'
text_color = cards['Type'].map do |t|
if color == 'black' && t == "Snake" then

Loading…
Cancel
Save