initials 0.2.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cf0672cb32ca9c0578082938d688e501b860f82f11f5fc5f9da391f600d7b67
4
- data.tar.gz: cd64e5021b2d8546a1e83875139be27a266d776ec01f825fffd14e66122f0bc1
3
+ metadata.gz: 2967ee83aead20c8869af14e57d6a573298453f296187a64b8f2480e626e1d6c
4
+ data.tar.gz: 16577212cb755c24cd7d824c9dd1dd2f4be699173ffdf0d09edbc2c97228a070
5
5
  SHA512:
6
- metadata.gz: 715df7ec0c73a894d9cb88e00be05c90d479dc489bb7dd72f2940d0d92543e02c9acba5e70e4ae3e569a099efeaad85368ee3e74d1e1a65bbbee7d6452fe4f3a
7
- data.tar.gz: 7fe165889d701dc17d80b32b03744aa6a299b23eac88b901aea1c754f4af7d2f5143ce989ca7ae1992ff9a8d513c373cc29f01e2eb7f27bcbfb7e22cb155118a
6
+ metadata.gz: 894c0090dd2b77e3da58894a8350a4309d49bb2c88ef6d57ebb95b140e2bacd2b5a39887b600edc1bfeac967994b9ef34b5575e9d9273efc20a05d0a343b48a0
7
+ data.tar.gz: ed742e7bc42d1edc4528e28cc553b4c8afa0399ec0dc84d521eca0fc15a0423e9f9cd31fa206b4bc5a6ad2a4b52606b4ed4317141eafffd47f1871b42bd4bd7f
@@ -31,5 +31,9 @@ jobs:
31
31
  with:
32
32
  ruby-version: ${{ matrix.ruby-version }}
33
33
  bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
- - name: Run tests
35
- run: bundle exec rake
34
+ - name: Run tests and publish code coverage
35
+ uses: paambaati/codeclimate-action@v2.7.5
36
+ env:
37
+ CC_TEST_REPORTER_ID: 9c690ab71ab481ac45785c4010ff30f93a45774e5ad6f03ec448b6e2b5bc39b4
38
+ with:
39
+ coverageCommand: bundle exec rake
data/.gitignore CHANGED
@@ -9,3 +9,8 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ # SimpleCov reports
14
+ coverage
15
+
16
+ Gemfile.lock
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in initials.gemspec
6
6
  gemspec
7
+
8
+ gem 'simplecov', require: false, group: :test
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
- # Initials
1
+ # Initials [![Maintainability](https://api.codeclimate.com/v1/badges/fb865ec4adcd0671dc48/maintainability)](https://codeclimate.com/github/thutterer/initials/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/fb865ec4adcd0671dc48/test_coverage)](https://codeclimate.com/github/thutterer/initials/test_coverage)
2
2
 
3
3
  Don't want to implement user avatar uploads but still have basic avatars to distinguish users and brigthen up your app?
4
4
 
5
5
  Use colorful SVGs as user avatars in any Ruby and Rails application.
6
6
 
7
+ ![demo](demo.png)
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
@@ -23,6 +25,7 @@ Or install it yourself as:
23
25
  ## Usage
24
26
 
25
27
  Call `Initials.svg("Morty Smith")` anywhere to get a colorful initials avatar SVG as string.
28
+ The avatar's background color is based on the provided name and the font-size is adjusted to fit between one and three character initials.
26
29
 
27
30
  ### Rails
28
31
 
@@ -30,7 +33,7 @@ No special configuration is required to work with Ruby on Rails, but for your co
30
33
 
31
34
  ```ruby
32
35
  def user_avatar(name, **options)
33
- Initials.svg(name, options)
36
+ Initials.svg(name, **options)
34
37
  end
35
38
  ```
36
39
 
@@ -46,11 +49,16 @@ Initials automatically marks its created SVG strings as `html_safe`.
46
49
 
47
50
  You can pass the following options into `Initials.svg` or your `user_avatar` helper:
48
51
 
49
- ```rb
50
- user_avatar(current_user.name, size: 96)
52
+ ```ruby
53
+ user_avatar(current_user.name,
54
+ colors: 8, # number of different colors, default: 12
55
+ limit: 1, # maximal initials length, default: 3
56
+ shape: :rect, # background shape, default: :cirlce
57
+ size: 96 # SVG height and width in pixel, default: 32
58
+ )
51
59
  ```
52
60
 
53
- This sets `width` and `height` to `96px` in the SVG. Of course, you can also use CSS to make the SVG have different sizes in different places of your HTML.
61
+ Of course, you can also use CSS to make the SVG have different sizes in different places of your HTML.
54
62
 
55
63
  ## Development
56
64
 
data/demo.png ADDED
Binary file
data/lib/initials/svg.rb CHANGED
@@ -1,18 +1,32 @@
1
1
  module Initials
2
2
  class SVG
3
- attr_reader :name, :size
3
+ HUE_WHEEL = 360
4
4
 
5
- def initialize(name, size: 32)
6
- raise Initials::Error.new("Name is not a string or empty") unless (name.respond_to?(:to_s) && name.to_s.length > 0)
7
- @name = name
5
+ attr_reader :name, :colors, :limit, :shape, :size
6
+
7
+ def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32)
8
+ @name = name.to_s.strip
9
+ @colors = colors
10
+ @limit = limit
11
+ @shape = shape
8
12
  @size = size
13
+
14
+ raise Initials::Error.new("Colors must be a divider of 360 e.g. 24 but not 16.") unless valid_colors?
15
+ raise Initials::Error.new("Size is not a positive integer.") unless valid_size?
16
+ end
17
+
18
+ def name
19
+ @name.empty? ? "?" : @name
9
20
  end
10
21
 
11
22
  def to_s
12
23
  svg = [
13
24
  "<svg width='#{size}' height='#{size}'>",
14
- "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
15
- "<text x='50%' y='50%' fill='white' fill-opacity='0.75' dominant-baseline='central' text-anchor='middle' style='font: bold #{font_size}px sans-serif;'>",
25
+ shape == :rect ?
26
+ "<rect width='#{size}' height='#{size}' rx='#{size / 32}' ry='#{size / 32}' fill='#{fill}' />"
27
+ :
28
+ "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
29
+ "<text x='50%' y='50%' fill='white' fill-opacity='0.75' dominant-baseline='central' text-anchor='middle' style='font-size: #{font_size}px; font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif; user-select: none;'>",
16
30
  "#{initials}",
17
31
  "</text>",
18
32
  "</svg>"
@@ -22,7 +36,17 @@ module Initials
22
36
  end
23
37
 
24
38
  def fill
25
- hue = name.split("").sum { |c| c.ord } % 360
39
+ return "hsl(0, 0%, 67%)" if @name.empty?
40
+
41
+ hue_step = HUE_WHEEL / colors
42
+ char_sum = name.split("").sum do |c|
43
+ # Multiplication makes sure neighboring characters (like A and B) are one hue step apart.
44
+ c.ord * hue_step
45
+ end
46
+
47
+ # Spin the wheel!
48
+ hue = char_sum % HUE_WHEEL
49
+
26
50
  "hsl(#{hue}, 40%, 40%)"
27
51
  end
28
52
 
@@ -31,7 +55,20 @@ module Initials
31
55
  end
32
56
 
33
57
  def initials
34
- name.split(' ')[0, 3].map { |s| s[0].capitalize }.join
58
+ name.split(' ')[0, limit].map { |s| s[0].capitalize }.join
59
+ end
60
+
61
+ private
62
+
63
+ def valid_colors?
64
+ return false unless colors.respond_to?(:to_i)
65
+ return false unless colors > 0
66
+ HUE_WHEEL % colors == 0
67
+ end
68
+
69
+ def valid_size?
70
+ return false unless size.respond_to?(:to_i)
71
+ size.to_i > 0
35
72
  end
36
73
  end
37
74
  end
@@ -1,3 +1,3 @@
1
1
  module Initials
2
- VERSION = "0.2.1"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: initials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Hutterer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-12 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - Rakefile
70
70
  - bin/console
71
71
  - bin/setup
72
+ - demo.png
72
73
  - initials.gemspec
73
74
  - lib/initials.rb
74
75
  - lib/initials/svg.rb