diff --git a/lib/initials/svg.rb b/lib/initials/svg.rb index b3a340d..c5f0db0 100644 --- a/lib/initials/svg.rb +++ b/lib/initials/svg.rb @@ -4,13 +4,15 @@ class SVG attr_reader :name, :colors, :limit, :shape, :size - def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32) + def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32, font_size_multiplier: 1.0) @name = name.to_s.strip @colors = colors @limit = limit @shape = shape @size = size + @font_size_multiplier = font_size_multiplier + raise Initials::Error.new("Font size multiplier must be a number between 0 and 2, was: #{@font_size_multiplier}") unless valid_font_size_multiplier? raise Initials::Error.new("Colors must be a divider of 360 e.g. 24 but not 16.") unless valid_colors? raise Initials::Error.new("Size is not a positive integer.") unless valid_size? end @@ -51,7 +53,8 @@ def fill end def font_size - size/2 + size/16 - (initials.length * size/16) + default_font_size = size/2 + size/16 - (initials.length * size/16) + (@font_size_multiplier * default_font_size).round end def initials @@ -60,6 +63,10 @@ def initials private + def valid_font_size_multiplier? + (0..2) === @font_size_multiplier + end + def valid_colors? return false unless colors.respond_to?(:to_i) return false unless colors > 0 @@ -71,4 +78,4 @@ def valid_size? size.to_i > 0 end end -end \ No newline at end of file +end diff --git a/spec/svg_spec.rb b/spec/svg_spec.rb index 8a6172c..8438bcf 100644 --- a/spec/svg_spec.rb +++ b/spec/svg_spec.rb @@ -103,6 +103,26 @@ end end end + + describe "font_size_multiplier" do + let(:options) { {font_size_multiplier: 1.5} } + + it "changes SVG font-size" do + expect(subject.to_s).to match(/^/) + end + + [-1, -0.01, nil, 2.1].each do |font_size_multiplier| + it "validates positive integer (#{font_size_multiplier} is invalid)" do + expect { described_class.new("Rick", font_size_multiplier: font_size_multiplier) }.to raise_error Initials::Error + end + end + + [0.5, 1, 1.2].each do |font_size_multiplier| + it "validates positive integer (#{font_size_multiplier} is valid)" do + expect { described_class.new("Rick", font_size_multiplier: font_size_multiplier) }.not_to raise_error + end + end + end end describe "name handling" do