From c6e85b0e4614473f51ce616299c899c112e9aeb5 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Sun, 2 Nov 2014 18:43:11 -0500 Subject: [PATCH] Refactoring to use case more, fixed build --- lib/squib/graphics/text.rb | 55 +++++++++++++---------------- spec/graphics/graphics_text_spec.rb | 10 +++++- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 7f3f66f..8bc3775 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -23,39 +23,35 @@ module Squib # :nodoc: # @api private - def ellipsize(layout, ellipsize) - unless ellipsize.nil? - h = { :none => Pango::Layout::ELLIPSIZE_NONE, - :start => Pango::Layout::ELLIPSIZE_START, - :middle => Pango::Layout::ELLIPSIZE_MIDDLE, - :end => Pango::Layout::ELLIPSIZE_END, - true => Pango::Layout::ELLIPSIZE_END, - false => Pango::Layout::ELLIPSIZE_NONE - } - layout.ellipsize = h[ellipsize] + def set_ellipsize!(layout, ellipsize) + case ellipsize.to_s.downcase + when 'none', 'false' + layout.ellipsize = Pango::Layout::ELLIPSIZE_NONE + when 'start' + layout.ellipsize = Pango::Layout::ELLIPSIZE_START + when 'middle' + layout.ellipsize = Pango::Layout::ELLIPSIZE_MIDDLE + when 'end', 'true' + layout.ellipsize = Pango::Layout::ELLIPSIZE_END end - layout end # :nodoc: # @api private - def wrap(layout, wrap) - layout.wrap = case wrap.to_s - when 'word' - Pango::Layout::WRAP_WORD - when 'char' - Pango::Layout::WRAP_CHAR - when 'word_char', true - Pango::Layout::WRAP_WORD_CHAR - else - nil - end - layout + def set_wrap!(layout, wrap) + case wrap.to_s.downcase + when 'word' + layout.wrap = Pango::Layout::WRAP_WORD + when 'char' + layout.wrap = Pango::Layout::WRAP_CHAR + when 'word_char', 'true' + layout.wrap = Pango::Layout::WRAP_WORD_CHAR + end end # :nodoc: # @api private - def align(layout, align) + def set_align!(layout, align) case align.to_s when 'left' layout.alignment = Pango::ALIGN_LEFT @@ -64,7 +60,6 @@ module Squib when 'center' layout.alignment = Pango::ALIGN_CENTER end - layout end # :nodoc: @@ -83,7 +78,7 @@ module Squib # :nodoc: # @api private - def setwh(layout, width, height) + def set_wh!(layout, width, height) layout.width = width * Pango::SCALE unless width.nil? || width == :native layout.height = height * Pango::SCALE unless height.nil? || height == :native layout @@ -107,10 +102,10 @@ module Squib layout.font_description = font_desc layout.text = str.to_s layout.markup = str.to_s if markup - layout = setwh(layout, width, height) - layout = wrap(layout, wrap) - layout = ellipsize(layout, ellipsize) - layout = align(layout, align) + set_wh!(layout, width, height) + set_wrap!(layout, wrap) + set_ellipsize!(layout, ellipsize) + set_align!(layout, align) layout.justify = justify unless justify.nil? layout.spacing = spacing * Pango::SCALE unless spacing.nil? cc.update_pango_layout(layout) diff --git a/spec/graphics/graphics_text_spec.rb b/spec/graphics/graphics_text_spec.rb index 04c704f..cbf2bfc 100644 --- a/spec/graphics/graphics_text_spec.rb +++ b/spec/graphics/graphics_text_spec.rb @@ -23,7 +23,6 @@ describe Squib::Card, '#text' do expect(@layout).to receive(:text=).with('foo').once expect(@layout).to receive(:width=).with(20 * Pango::SCALE).once expect(@layout).to receive(:height=).with(25 * Pango::SCALE).once - expect(@layout).to receive(:wrap=).with(nil).once expect(@layout).to receive(:ellipsize=).with(Pango::Layout::ELLIPSIZE_NONE).once expect(@layout).to receive(:alignment=).with(Pango::Layout::ALIGN_LEFT).once expect(@layout).to receive(:justify=).with(false).once @@ -94,5 +93,14 @@ describe Squib::Card, '#text' do 1.0, :left, :top, nil, 0.0) end + it 'sets wrap to word_char with true' do + card = Squib::Card.new(@deck, 100, 150) + expect(@layout).to receive(:wrap=).with(Pango::Layout::WRAP_WORD_CHAR).once + card.text('foo', 'Sans 12', nil, '#abc', + 10, 15, 20, 50, + nil, false, true, false, + 1.0, :left, :top, nil, 0.0) + end + end end