Class: Iconmap::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/iconmap/map.rb

Defined Under Namespace

Classes: InvalidFile, MappedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



12
13
14
15
# File 'lib/iconmap/map.rb', line 12

def initialize
  @packages = {}
  @cache = {}
end

Instance Attribute Details

#packagesObject (readonly)

Returns the value of attribute packages.



6
7
8
# File 'lib/iconmap/map.rb', line 6

def packages
  @packages
end

Instance Method Details

#cache_sweeper(watches: nil) ⇒ Object

Returns an instance of ActiveSupport::EventedFileUpdateChecker configured to clear the cache of the map when the directories passed on initialization via watches: have changes. This is used in development and test to ensure the map caches are reset when icon files are changed.



40
41
42
43
44
45
46
47
48
49
# File 'lib/iconmap/map.rb', line 40

def cache_sweeper(watches: nil)
  if watches
    @cache_sweeper =
      Rails.application.config.file_watcher.new([], Array(watches).collect { |dir| [dir.to_s, 'svg'] }.to_h) do
        clear_cache
      end
  else
    @cache_sweeper
  end
end

#draw(path = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iconmap/map.rb', line 17

def draw(path = nil, &)
  if path && File.exist?(path)
    begin
      instance_eval(File.read(path), path.to_s)
    rescue StandardError => e
      Rails.logger.error "Unable to parse icon map from #{path}: #{e.message}"
      raise InvalidFile, "Unable to parse icon map from #{path}: #{e.message}"
    end
  elsif block_given?
    instance_eval(&)
  end

  self
end

#pin(name) ⇒ Object



32
33
34
35
# File 'lib/iconmap/map.rb', line 32

def pin(name)
  clear_cache
  @packages[name] = MappedFile.new(name: name)
end