rpictogrify 0.1.0 → 0.2.0

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: c9fc80fdfcac7707fd1a41b0ba12e25b8f1c2290417433f305772022cbbc61c5
4
- data.tar.gz: c1ab1094c3392d322769580a5355a7d5a69c4c8ce4884c338e174ebe24e4ed80
3
+ metadata.gz: 3664e3a68fee566a6aaf97ca874adf0cf8b1a084dc73604ea9481ec3925a96b1
4
+ data.tar.gz: 68d39dc71dd312d76679d1e653ce76ecfd532a1c9db85c832504a2bf70eb144f
5
5
  SHA512:
6
- metadata.gz: 611b151a621cb1255ea2abe3debae7152ba6ab6308b92d3b236fc93a2efe04d3497203661cc8b53c6844a57192577b298e5714e6bf6fccd79e464a14189addc6
7
- data.tar.gz: 6865400755633cf561f26b09e25781dc3645527bb785b64d41922cb7a3385ecee0bf39307b7af8f202011b43b6fa019586a6b16e8ccebf696766ef24e1dacce1
6
+ metadata.gz: 1e520ae74555f68ab1f9d2a247e750c3cc3e428b0ba5ffab14394a5c17f2d72f2289cf5168caa937029ff56bb2f78f85911e83356931e24e314b5aadbfea117e
7
+ data.tar.gz: ae95c442fa4e84d9cfb5f9303c178b73af325ce497b16efec9ccbeddd28c2e92eda52140e249ae770c2531ba3426016329c6847d2adc929400e0d10c19548f31
data/README.md CHANGED
@@ -42,8 +42,44 @@ end
42
42
  ## Usage
43
43
 
44
44
  ```ruby
45
- Rpictogrify.generate 'jim.cheung' #=> public/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg
46
- Rpictogrify.generate 'jim.cheung', theme: :avataars_male #=> public/system/rpictogrify/1/avataars_male/jim.cheung-2935966159678137421.svg
45
+ Rpictogrify.generate 'jim.cheung' #=> 'public/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg'
46
+ Rpictogrify.generate 'jim.cheung', theme: :avataars_male #=> 'public/system/rpictogrify/1/avataars_male/jim.cheung-2935966159678137421.svg'
47
+ ```
48
+
49
+ ### Controllers / Views
50
+
51
+ There is a helper for this, you need to include `Rpictogrify::Helper` in your controller or helper. e.g. `ApplicationHelper`
52
+
53
+ ```ruby
54
+ include Rpictogrify::Helper
55
+ ```
56
+
57
+ Then you can use the following methods in views.
58
+
59
+ ```ruby
60
+ rpictogrify_for('jim', theme: :monsters) #=> 'public/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg'
61
+ rpictogrify_url('jim') #=> '/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg'
62
+ rpictogrify_tag('jim', html: {class: :avatar}) #=> '<img class="avatar" src="/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg" alt="jim" />'
63
+ ```
64
+
65
+ ### Model
66
+
67
+ You can include `Rpictogrify::Extension` in your model class. e.g. `User` model
68
+
69
+ ```ruby
70
+ class User
71
+ include Rpictogrify::Extension
72
+
73
+ rpictogrify_on :username, theme: -> { gender == :male ? :avataars_male : :avataars_female }
74
+ # rpictogrify_on :username, theme: :avataars_male
75
+ end
76
+ ```
77
+
78
+ Then you have the following methods in user
79
+
80
+ ```ruby
81
+ @user.rpictogrify_path #=> 'public/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg'
82
+ @user.rpictogrify_url #=> '/system/rpictogrify/1/monsters/jim.cheung-1512422874962937463.svg'
47
83
  ```
48
84
 
49
85
  ## Contributing
@@ -8,6 +8,8 @@ require 'base64'
8
8
  require 'rpictogrify/configuration'
9
9
  require 'rpictogrify/generator'
10
10
  require 'rpictogrify/inflector'
11
+ require 'rpictogrify/helper'
12
+ require 'rpictogrify/extension'
11
13
 
12
14
  module Rpictogrify
13
15
  class << self
@@ -36,5 +38,9 @@ module Rpictogrify
36
38
  def themes_assets_path
37
39
  @themes_assets_path ||= assets_path.join('themes')
38
40
  end
41
+
42
+ def path_to_url(path)
43
+ path.to_s.sub('public/', '/')
44
+ end
39
45
  end
40
46
  end
@@ -0,0 +1,49 @@
1
+ module Rpictogrify
2
+ module Extension
3
+ def self.included(base)
4
+ base.send :include, InstanceMethods
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def rpictogrify_on(textable, options = {})
10
+ rpictogrify_settings[:textable] = textable
11
+ rpictogrify_settings[:options] = options
12
+ end
13
+
14
+ def rpictogrify_settings
15
+ @rpictogrify_settings ||= {}
16
+ end
17
+ end
18
+
19
+ module InstanceMethods
20
+ def rpictogrify_path(text = nil, options = {})
21
+ Rpictogrify.generate(text || rpictogrify_text, rpictogrify_options.merge(options || {}))
22
+ end
23
+
24
+ def rpictogrify_url(text = nil, options = {})
25
+ Rpictogrify.path_to_url(rpictogrify_path(text, options))
26
+ end
27
+
28
+ private
29
+
30
+ def rpictogrify_text
31
+ textable = self.class.rpictogrify_settings[:textable]
32
+ return unless textable && respond_to?(textable)
33
+ self.send(textable)
34
+ end
35
+
36
+ def rpictogrify_options
37
+ options = self.class.rpictogrify_settings[:options] || {}
38
+ if options[:theme]
39
+ if options[:theme].respond_to?(:call)
40
+ options[:theme] = self.instance_exec(&options[:theme])
41
+ elsif respond_to?(options[:theme])
42
+ options[:theme] = self.send(options[:theme])
43
+ end
44
+ end
45
+ options
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rpictogrify
4
+ module Helper
5
+ def rpictogrify_for(text, options = {})
6
+ Rpictogrify.generate(text, options)
7
+ end
8
+
9
+ def rpictogrify_url(text, options = {})
10
+ Rpictogrify.path_to_url(rpictogrify_for(text, options))
11
+ end
12
+
13
+ def rpictogrify_tag(text, options = {})
14
+ html_opts = {alt: text}.merge(options.delete(:html) || {})
15
+ if defined?(ActionView::Helpers::AssetTagHelper)
16
+ extend ActionView::Helpers::AssetTagHelper
17
+ image_tag(rpictogrify_url(text, options), html_opts)
18
+ else
19
+ tag = "<img class='#{html_opts[:class]}' src='#{rpictogrify_url(text, options)}' alt='#{html_opts[:alt]}' width='#{html_opts[:width]}' height='#{html_opts[:height]}' />"
20
+ tag.respond_to?(:html_safe) ? tag.html_safe : tag
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rpictogrify
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpictogrify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Cheung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-10 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -57,7 +57,9 @@ files:
57
57
  - assets/themes/monsters/resource.svg
58
58
  - lib/rpictogrify.rb
59
59
  - lib/rpictogrify/configuration.rb
60
+ - lib/rpictogrify/extension.rb
60
61
  - lib/rpictogrify/generator.rb
62
+ - lib/rpictogrify/helper.rb
61
63
  - lib/rpictogrify/inflector.rb
62
64
  - lib/rpictogrify/pictogram.rb
63
65
  - lib/rpictogrify/theme.rb