class Crypticons::Crypticon

Attributes

height[R]
keywords[R]
options[R]
path[R]
symbol[R]
width[R]

Public Class Methods

new(symbol, options = {}) click to toggle source
# File lib/crypticons/crypticon.rb, line 6
def initialize(symbol, options = {})
  @symbol = symbol.to_s
  if crypticon = Crypticons::CRYPTICON_SYMBOLS[@symbol]

    @path = crypticon["path"]
    @width = crypticon["width"].to_i
    @height = crypticon["height"].to_i

    @keywords = crypticon["keywords"]

    @options = options
    @options.merge!({
      class:   classes,
      viewBox: viewbox,
      version: "1.1"
    })
    @options.merge!(size)
    @options.merge!(a11y)
  else
    raise "Couldn't find crypticon symbol for #{@symbol.inspect}"
  end
end

Public Instance Methods

to_svg() click to toggle source

Returns an string representing a <svg> tag

# File lib/crypticons/crypticon.rb, line 30
def to_svg
  "<svg #{html_attributes}>#{@path}</svg>"
end

Private Instance Methods

a11y() click to toggle source

add some accessibility features to svg

# File lib/crypticons/crypticon.rb, line 43
def a11y
  accessible = {}

  if @options[:'aria-label'].nil?
    accessible[:'aria-hidden'] = "true"
  else
    accessible[:role] = "img"
  end

  accessible
end
calculate_height(width) click to toggle source
# File lib/crypticons/crypticon.rb, line 84
def calculate_height(width)
  (width.to_i * @height) / @width
end
calculate_width(height) click to toggle source
# File lib/crypticons/crypticon.rb, line 80
def calculate_width(height)
  (height.to_i * @width) / @height
end
classes() click to toggle source

prepare the crypticon class

# File lib/crypticons/crypticon.rb, line 56
def classes
  "crypticon crypticon-#{@symbol} #{@options[:class]} ".strip
end
html_attributes() click to toggle source
# File lib/crypticons/crypticon.rb, line 36
def html_attributes
  attrs = ""
  @options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
  attrs.strip
end
size() click to toggle source

determine the height and width of the crypticon based on :size option

# File lib/crypticons/crypticon.rb, line 65
def size
  size = {
    width:  @width,
    height: @height
  }

  # Specific size
  unless @options[:width].nil? && @options[:height].nil?
    size[:width]  = @options[:width].nil?  ? calculate_width(@options[:height]) : @options[:width]
    size[:height] = @options[:height].nil? ? calculate_height(@options[:width]) : @options[:height]
  end

  size
end
viewbox() click to toggle source
# File lib/crypticons/crypticon.rb, line 60
def viewbox
  "0 0 #{@width} #{@height}"
end