From 10890be8a17a15a1f3ab8c124dc9b8265fde5ac3 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Mon, 28 Jul 2014 16:01:46 -0400 Subject: [PATCH] Putting together a global `set` method for defaults --- lib/squib/api/settings.rb | 18 +++++++++++++++++- lib/squib/api/text.rb | 10 ---------- lib/squib/constants.rb | 2 +- lib/squib/deck.rb | 2 +- samples/basic.rb | 2 +- spec/api/api_text_spec.rb | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 spec/api/api_text_spec.rb diff --git a/lib/squib/api/settings.rb b/lib/squib/api/settings.rb index 9245542..b2475a0 100644 --- a/lib/squib/api/settings.rb +++ b/lib/squib/api/settings.rb @@ -6,12 +6,28 @@ module Squib # Setting a hint to nil or to :off will disable hints. @see samples/text.rb # # @param [Color] text the color of the text hint. To turn off use nil or :off. @see API.md - # @return nil + # @return [nil] Returns nothing # @api public def hint(text: nil) text = nil if text == :off @text_hint = text end + # Sets various defaults for this deck. Defaults can be overriden by the commands themselves + # @example + # set font: 'Arial 26' + # text 'blah' # in Arial 26 + # text 'blah24', font: 'Arial 24' # in Arial 24 + # set font: :default # Back to Squib-wide default + # + # @param opts: the hash of options. + # @option font: the font string to set as default. Can also be set to `:default` to use the Squib-wide default. + # @return [nil] Returns nothing + # @api public + def set(opts = {}) + opts = needs(opts, [:font]) + @font = opts[:font] + end + end end diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index 8a78fe0..d4a005a 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -1,16 +1,6 @@ module Squib class Deck - # @api private todo - def font(type: 'Arial', size: 12, **options) - raise 'Not implemented!' - end - - # @api private todo - def set_font(type: 'Arial', size: 12, **options) - raise 'Not implemented!' - end - # Renders a string at a given location, width, alignment, font, etc. # Unix-like newlines are interpreted even on Windows. # diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index 1b6bc4e..f96225e 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -8,7 +8,7 @@ module Squib :fill_color => '#0000', :stroke_color => :black, :stroke_width => 2.0, - :font => 'Arial, Sans 36', + :font => :use_set, :sheet => 0, :x => 0, :y => 0, diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index f787fad..d29f2a6 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -24,7 +24,7 @@ module Squib def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', &block) @width=width; @height=height @dpi = dpi - @font = 'Sans 36' + @font = Squib::SYSTEM_DEFAULTS[:font] @cards = [] cards.times{ @cards << Squib::Card.new(self, width, height) } load_config(config) diff --git a/samples/basic.rb b/samples/basic.rb index f7e641d..f316dfb 100644 --- a/samples/basic.rb +++ b/samples/basic.rb @@ -5,7 +5,7 @@ data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} Squib::Deck.new(width: 825, height: 1125, cards: 3) do - puts background color: :white + background color: :white rect x: 38, y: 38, width: 750, height: 1050, radius: 38 rect x: 75, y: 75, width: 128, height: 128, radius: 25 diff --git a/spec/api/api_text_spec.rb b/spec/api/api_text_spec.rb new file mode 100644 index 0000000..d1c3bb6 --- /dev/null +++ b/spec/api/api_text_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' +require 'squib' + +describe Squib::Deck, '#text' do + + context "when working with fonts" do + it"should use the default font when #text and #set_font don't specify" do + card = instance_double(Squib::Card) + expect(card).to receive(:text).with('a', Squib::SYSTEM_DEFAULTS[:font], anything, anything, anything, anything).once + Squib::Deck.new do + @cards = [card] + text str: 'a' + end + end + + it "should use the #set_font when #text doesn't specify" do + card = instance_double(Squib::Card) + expect(card).to receive(:text).with('a', 'Times New Roman 16', anything, anything, anything, anything).once + Squib::Deck.new do + @cards = [card] + set font: 'Times New Roman 16' + text str: 'a' + end + end + + it "should use the specified font no matter what" do + card = instance_double(Squib::Card) + expect(card).to receive(:text).with('a', 'Arial 18', anything, anything, anything, anything).once + Squib::Deck.new do + @cards = [card] + set font: 'Times New Roman 16' + text str: 'a', font: 'Arial 18' + end + end + end + +end \ No newline at end of file