Class: Jekyll::Importmap::MappedDir

Inherits:
Mappable
  • Object
show all
Defined in:
lib/jekyll-importmap/mapped_dir.rb

Overview

MappedFile = Struct.new(:name, :path, :preload, keyword_init: true) MappedDir = Struct.new(:dir, :under, :path, :preload, keyword_init: true)

Instance Attribute Summary collapse

Attributes inherited from Mappable

#keyword_init, #path, #preload

Instance Method Summary collapse

Methods inherited from Mappable

#absolute_root_path, #calculate_absolute_root_path, #resolved_path

Constructor Details

#initialize(hash, resolver = Jekyll::Importmap::Resolver) ⇒ MappedDir



14
15
16
17
18
# File 'lib/jekyll-importmap/mapped_dir.rb', line 14

def initialize(hash, resolver=Jekyll::Importmap::Resolver)
    super
    @dir = hash[:dir]
    @under = hash[:under]
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



12
13
14
# File 'lib/jekyll-importmap/mapped_dir.rb', line 12

def dir
  @dir
end

#underObject (readonly)

Returns the value of attribute under.



12
13
14
# File 'lib/jekyll-importmap/mapped_dir.rb', line 12

def under
  @under
end

Instance Method Details

#expanded_pathsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-importmap/mapped_dir.rb', line 28

def expanded_paths
    javascript_files_in_tree.collect do |pathname|
        module_filename = pathname.relative_path_from(absolute_root_path)
        module_path = module_path_from(module_filename)
        module_name = module_name_from(module_filename)

        Jekyll::Importmap::MappedFile.new(
            name: module_name,
            path: module_path,
            preload: @preload
        )
    end
end

#javascript_files_in_treeObject



20
21
22
23
24
25
26
# File 'lib/jekyll-importmap/mapped_dir.rb', line 20

def javascript_files_in_tree
    selector = absolute_root_path.join("**/*.js{,m}")
    files = Dir[selector].sort
    files.collect do |file|
        Pathname.new(file)
    end.select(&:file?)
end

#module_name_from(filename) ⇒ Object



42
43
44
45
46
# File 'lib/jekyll-importmap/mapped_dir.rb', line 42

def module_name_from(filename)
    filename = filename.to_s.gsub(filename.extname, '').gsub(/\/?index$/, '')
    filename = nil if filename.length < 1
    [@under, filename].compact.join('/')
end

#module_path_from(filename) ⇒ Object



47
48
49
# File 'lib/jekyll-importmap/mapped_dir.rb', line 47

def module_path_from(filename)
    [@path || @under, filename.to_s].compact.reject(&:empty?).join('/')
end