Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/initials/svg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -71,4 +78,4 @@ def valid_size?
size.to_i > 0
end
end
end
end
20 changes: 20 additions & 0 deletions spec/svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^<svg .*font-size: 21px.+<\/svg>/)
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
Expand Down