initials 0.2.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +6 -2
- data/.gitignore +5 -0
- data/Gemfile +2 -0
- data/README.md +13 -5
- data/demo.png +0 -0
- data/lib/initials/svg.rb +45 -8
- data/lib/initials/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2967ee83aead20c8869af14e57d6a573298453f296187a64b8f2480e626e1d6c
|
4
|
+
data.tar.gz: 16577212cb755c24cd7d824c9dd1dd2f4be699173ffdf0d09edbc2c97228a070
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 894c0090dd2b77e3da58894a8350a4309d49bb2c88ef6d57ebb95b140e2bacd2b5a39887b600edc1bfeac967994b9ef34b5575e9d9273efc20a05d0a343b48a0
|
7
|
+
data.tar.gz: ed742e7bc42d1edc4528e28cc553b4c8afa0399ec0dc84d521eca0fc15a0423e9f9cd31fa206b4bc5a6ad2a4b52606b4ed4317141eafffd47f1871b42bd4bd7f
|
data/.github/workflows/ruby.yml
CHANGED
@@ -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
|
-
|
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
data/Gemfile
CHANGED
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
|
-
```
|
50
|
-
user_avatar(current_user.name,
|
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
|
-
|
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
|
-
|
3
|
+
HUE_WHEEL = 360
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
15
|
-
|
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
|
-
|
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,
|
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
|
data/lib/initials/version.rb
CHANGED
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.
|
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-
|
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
|