Class: Esvg::Symbol

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/esvg/symbol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#attributes, #compress, #dasherize, #sort, #sub_path

Constructor Details

#initialize(path, config = {}) ⇒ Symbol

Returns a new instance of Symbol.



9
10
11
12
13
14
15
# File 'lib/esvg/symbol.rb', line 9

def initialize(path, config={})
  @config  = config
  @path    = path
  @last_checked = 0
  load_data
  read
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def content
  @content
end

#defsObject (readonly)

Returns the value of attribute defs.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def defs
  @defs
end

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def id
  @id
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def mtime
  @mtime
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def name
  @name
end

#optimizedObject (readonly)

Returns the value of attribute optimized.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def optimized
  @optimized
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def size
  @size
end

Instance Method Details

#attrObject



64
65
66
# File 'lib/esvg/symbol.rb', line 64

def attr
  { id: @id, 'data-name' => @name }.merge @size
end

#changed?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/esvg/symbol.rb', line 138

def changed?
  last_modified != mtime
end

#dataObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/esvg/symbol.rb', line 48

def data
  {
    path: @path,
    id: @id,
    name: @name,
    group: @group,
    mtime: @mtime,
    size: @size,
    content: @content,
    defs: @defs,
    optimized: @optimized,
    optimized_at: @optimized_at,
    svgo_optimized: svgo? && @svgo_optimized
  }
end

#heightObject



44
45
46
# File 'lib/esvg/symbol.rb', line 44

def height
  @size[:height]
end

#optimizeObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/esvg/symbol.rb', line 116

def optimize
  read if changed?

  # Only optimize again if the file has changed
  return @optimized if @optimized && @optimized_at && @optimized_at > @mtime

  @optimized = @content

  if svgo? 
    response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -})
    if !response[0].empty? && response[2].success?
      @optimized = response[0]
      @svgo_optimized = true
    end
  end

  post_optimize
  @optimized_at = Time.now.to_i

  @optimized
end

#readObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/esvg/symbol.rb', line 17

def read
  return if !File.exist?(@path)

  # Ensure that cache optimization matches current optimization settings
  # If config has changed name, reset optimized build (name gets baked in)
  if changed? || @svgo_optimized != svgo? || name != file_name
    @optimized = nil
    @optimized_at = nil
  end

  @group = dir_key
  @name  = file_name
  @id    = file_id file_key

  if changed?
    @content = prep_defs pre_optimize File.read(@path)
    @mtime   = last_modified
    @size    = dimensions
  end

  self
end

#svgo?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/esvg/symbol.rb', line 112

def svgo?
  @config[:optimize] && !!Esvg.node_module('svgo')
end

#use(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/esvg/symbol.rb', line 68

def use(options={})
  options.delete(:fallback)
  content = options.delete(:content) || ''

  if desc   = options.delete(:desc)
    content = "<desc>#{desc}</desc>#{content}"
  end
  if title  = options.delete(:title)
    content = "<title>#{title}</title>#{content}"
  end

  use_attr = options.delete(:use) || {}

  svg_attr = {
    class: [@config[:class], @config[:prefix]+"-"+@name, options.delete(:class)].compact.join(' '),
    viewBox: @size[:viewBox],
    role: 'img'
  }.merge(options)

  # If user doesn't pass a size or set scale: true
  if svg_attr[:width].nil? && svg_attr[:height].nil? && !svg_attr[:scale]
    svg_attr[:width]  = width
    svg_attr[:height] = height
  end

  svg_attr.delete(:scale)

  %Q{<svg #{attributes(svg_attr)}>#{use_tag(use_attr)}#{content}</svg>}
end

#use_tag(options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/esvg/symbol.rb', line 98

def use_tag(options={})
  options["xlink:href"] = "##{@id}"

  # If user doesn't pass a size or set scale: true
  if options[:width].nil? && options[:height].nil? && !options[:scale] && !@config[:scale]
    options[:width]  ||= width
    options[:height] ||= height
  end

  options.delete(:scale)

  %Q{<use #{attributes(options)}/>}
end

#widthObject



40
41
42
# File 'lib/esvg/symbol.rb', line 40

def width
  @size[:width]
end