Class: Rpictogrify::Pictogram
- Inherits:
-
Object
- Object
- Rpictogrify::Pictogram
- Defined in:
- lib/rpictogrify/pictogram.rb
Constant Summary collapse
- VERSION =
BUMP UP if avatar algorithm changes
1
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
Class Method Summary collapse
Instance Method Summary collapse
- #base64 ⇒ Object
- #generate ⇒ Object
-
#initialize(text, options = {}) ⇒ Pictogram
constructor
A new instance of Pictogram.
- #path ⇒ Object
- #size ⇒ Object
- #svg ⇒ Object
- #text_hash ⇒ Object
Constructor Details
#initialize(text, options = {}) ⇒ Pictogram
Returns a new instance of Pictogram.
12 13 14 15 16 17 |
# File 'lib/rpictogrify/pictogram.rb', line 12 def initialize(text, = {}) @text = text.to_s @options = @theme = Rpictogrify::Theme.find([:theme] || Rpictogrify.config.theme) @uid = generate_uid end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/rpictogrify/pictogram.rb', line 10 def @options end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
10 11 12 |
# File 'lib/rpictogrify/pictogram.rb', line 10 def text @text end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
10 11 12 |
# File 'lib/rpictogrify/pictogram.rb', line 10 def theme @theme end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
10 11 12 |
# File 'lib/rpictogrify/pictogram.rb', line 10 def uid @uid end |
Class Method Details
.base_path ⇒ Object
62 63 64 |
# File 'lib/rpictogrify/pictogram.rb', line 62 def base_path File.join (Rpictogrify.config.base_path || 'public/system'), 'rpictogrify', "#{VERSION}" end |
Instance Method Details
#base64 ⇒ Object
40 41 42 |
# File 'lib/rpictogrify/pictogram.rb', line 40 def base64 "data:image/svg+xml;base64,#{Base64.encode64(svg)[0...-1]}" end |
#generate ⇒ Object
19 20 21 22 |
# File 'lib/rpictogrify/pictogram.rb', line 19 def generate File.write(path, svg) unless File.exist?(path) path end |
#path ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rpictogrify/pictogram.rb', line 48 def path @path ||= begin dir = File.join(self.class.base_path, theme.ident) FileUtils.mkdir_p(dir) File.join(dir, "#{text_hash}.svg") end end |
#size ⇒ Object
44 45 46 |
# File 'lib/rpictogrify/pictogram.rb', line 44 def size @size ||= theme.width || 300 end |
#svg ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rpictogrify/pictogram.rb', line 24 def svg includes = symbols.map do |shape, symbol| fillable = fill[shape] ? "fill='#{fill[shape]}'" : '' "<svg class='#{shape}' #{fillable} xmlns='http://www.w3.org/2000/svg'>#{symbol}</svg>" end <<-XML.strip <svg viewBox="0 0 #{size} #{size}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <rect fill="#{colors['background']}" x="0" y="0" width="#{size}" height="#{size}"></rect> #{includes.join("\n")} </g> </svg> XML end |
#text_hash ⇒ Object
57 58 59 |
# File 'lib/rpictogrify/pictogram.rb', line 57 def text_hash @text_hash ||= XXhash.xxh32(text) end |