Class: Gemstar::ImportmapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstar/importmap_file.rb

Defined Under Namespace

Classes: Pin

Constant Summary collapse

IMPORTMAP_PACKAGE_METADATA_PATH =
File.expand_path("data/importmap_package_metadata.json", __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, content: nil, vendor_reader: nil) ⇒ ImportmapFile

Returns a new instance of ImportmapFile.



21
22
23
24
25
# File 'lib/gemstar/importmap_file.rb', line 21

def initialize(path: nil, content: nil, vendor_reader: nil)
  @path = path
  @vendor_reader = vendor_reader
  @pins = parse_content(content || File.read(path))
end

Instance Attribute Details

#pinsObject (readonly)

Returns the value of attribute pins.



27
28
29
# File 'lib/gemstar/importmap_file.rb', line 27

def pins
  @pins
end

Class Method Details

.package_metadataObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gemstar/importmap_file.rb', line 9

def self.
  @package_metadata ||= begin
    JSON.parse(File.read(IMPORTMAP_PACKAGE_METADATA_PATH)).transform_values do |attributes|
      attributes.each_with_object({}) do |(key, value), |
        [key.to_sym] = value
      end
    end
  rescue Errno::ENOENT, JSON::ParserError
    {}
  end
end

Instance Method Details

#source_for(name) ⇒ Object



33
34
35
# File 'lib/gemstar/importmap_file.rb', line 33

def source_for(name)
  pins[name]&.source
end

#specsObject



29
30
31
# File 'lib/gemstar/importmap_file.rb', line 29

def specs
  pins.transform_values(&:target)
end