Module: Emojidex::Data::EmojiAssetInformation

Included in:
Emoji
Defined in:
lib/emojidex/data/emoji/asset_information.rb

Overview

Asset information for emoji

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#checksumsObject

Returns the value of attribute checksums.



8
9
10
# File 'lib/emojidex/data/emoji/asset_information.rb', line 8

def checksums
  @checksums
end

#pathsObject

Returns the value of attribute paths.



8
9
10
# File 'lib/emojidex/data/emoji/asset_information.rb', line 8

def paths
  @paths
end

#remote_checksumsObject

Returns the value of attribute remote_checksums.



8
9
10
# File 'lib/emojidex/data/emoji/asset_information.rb', line 8

def remote_checksums
  @remote_checksums
end

Instance Method Details

#blank_checksumsObject



23
24
25
26
27
28
29
30
31
# File 'lib/emojidex/data/emoji/asset_information.rb', line 23

def blank_checksums
  @checksums = {}
  @checksums[:svg] = nil
  @checksums[:png] = {}
  Emojidex::Defaults.sizes.keys.each do |size|
    @checksums[:png][size] = nil
  end
  @remote_checksums = @checksums.dup
end

#blank_pathsObject



51
52
53
54
55
56
57
58
# File 'lib/emojidex/data/emoji/asset_information.rb', line 51

def blank_paths
  @paths = {}
  @paths[:svg] = nil
  @paths[:png] = {}
  Emojidex::Defaults.sizes.keys.each do |size|
    @paths[:png][size] = nil
  end
end

#cache(format, sizes = nil) ⇒ Object

Caches a file of the specified format in the specified sizes



90
91
92
93
94
95
96
97
98
# File 'lib/emojidex/data/emoji/asset_information.rb', line 90

def cache(format, sizes = nil)
  generate_checksums
  case format
  when :png
    _cache_png(sizes) unless sizes.nil?
  when :svg
    _cache_svg
  end
end

#checksum?(format, size = nil) ⇒ Boolean

returns asset checksum

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/emojidex/data/emoji/asset_information.rb', line 17

def checksum?(format, size = nil)
  puts @checksums
  return @checksums[format][size] unless size.nil?
  @checksums[format]
end

#fill_checksums(checksums) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/emojidex/data/emoji/asset_information.rb', line 42

def fill_checksums(checksums)
  @checksums[:svg] = checksums[:svg] if checksums.include? :svg
  return unless checksums.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @checksums[:png][size] = checksums[:png][size] if checksums[:png].include? size
  end
  @checksums
end

#fill_paths(paths) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/emojidex/data/emoji/asset_information.rb', line 60

def fill_paths(paths)
  @paths[:svg] = paths[:svg] if paths.include? :svg
  return unless paths.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @paths[:png][size] = paths[:png][size] if paths[:png].include? size
  end
  @combinations.each { |combo| combo.fill_paths(paths) }
  @paths
end

#fill_remote_checksums(checksums) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/emojidex/data/emoji/asset_information.rb', line 33

def fill_remote_checksums(checksums)
  @remote_checksums[:svg] = checksums[:svg] if checksums.include? :svg
  return unless checksums.include? :png
  Emojidex::Defaults.sizes.keys.each do |size|
    @remote_checksums[:png][size] = checksums[:png][size] if checksums[:png].include? size
  end
  @remote_checksums
end

#generate_checksum(format, size = nil) ⇒ Object

Generates a checksum for each locally cached file



101
102
103
104
105
106
107
108
109
# File 'lib/emojidex/data/emoji/asset_information.rb', line 101

def generate_checksum(format, size = nil)
  case format
  when :png
    return @checksums[:png][size] = _checksum_for_file(@paths[:png][size])
  when :svg
    return @checksums[:svg] = _checksum_for_file(@paths[:svg])
  end
  nil
end

#generate_checksumsObject



111
112
113
114
115
116
117
118
# File 'lib/emojidex/data/emoji/asset_information.rb', line 111

def generate_checksums
  @checksums[:svg] = _checksum_for_file(@paths[:svg])
  @paths[:png].keys.each do |size|
    @checksums[:png][size] = _checksum_for_file(@paths[:png][size])
  end
  @combinations.each { |combo| combo.generate_checksums }
  @checksums
end

#init_asset_info(details) ⇒ Object



10
11
12
13
14
# File 'lib/emojidex/data/emoji/asset_information.rb', line 10

def init_asset_info(details)
  blank_paths
  blank_checksums
  fill_remote_checksums(details[:checksums]) if details.include? :checksums
end

#path(format, size = nil) ⇒ Object

Acquires path and caches the target file if not found or out of date



71
72
73
74
75
# File 'lib/emojidex/data/emoji/asset_information.rb', line 71

def path(format, size = nil)
  fp = path?(format, size)
  cache(format, [size]) unless !fp.nil? && File.exist?(fp)
  fp
end

#path?(format, size = nil) ⇒ Boolean

returns asset path

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
# File 'lib/emojidex/data/emoji/asset_information.rb', line 78

def path?(format, size = nil)
  case format
  when :svg
    return @paths[format] if File.exist?(@paths[format])
  when :png
    return nil if size.nil?
    return @paths[format][size] if File.exist?(@paths[format][size])
  end
  nil
end