Class: TpCommon::AssetsLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_common/assets_loader.rb

Defined Under Namespace

Classes: AssetNotFound, LoaderConfiguration, ManifestFileBroken, ManifestNotFound, PackageIsNotLoaded

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ AssetsLoader


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tp_common/assets_loader.rb', line 68

def initialize(package_name)
  @package_name = package_name.to_sym
  file_path = File.join(self.class.asset_root_path, @package_name.to_s, 'assets', 'manifest.json')

  @manifest = begin
                JSON.parse(File.read(file_path))
              rescue SystemCallError
                raise ManifestNotFound.new(
                  "Unable to load manifest file for package #{@package_name} at: #{file_path}"
                )
              rescue StandardError
                raise ManifestFileBroken.new("Manifest file for package #{@package_name} is unable to load")
              end
  return true
end

Class Method Details

.[](package_name) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tp_common/assets_loader.rb', line 36

def self.[](package_name)
  package_name = package_name.to_sym

  unless packages.key?(package_name)
    if @config.autoload
      load(package_name)
    else
      raise PackageIsNotLoaded.new("Package #{package_name} is not loaded yet.")
    end
  end

  packages[package_name]
end

.asset_root_pathObject


50
51
52
# File 'lib/tp_common/assets_loader.rb', line 50

def self.asset_root_path
  @config.asset_root_path
end

.build(package_name, external_manifest_file) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/tp_common/assets_loader.rb', line 22

def self.build(package_name, external_manifest_file)
  # TODO: Implement
  # Skip if "/public/#{package_name}" exists
  # Download manifest.json from #{external_manifest_file}?version=#{@config.version}
  #                        to "/public/#{package_name}/assets/manifest.json"
  # Validate "/public/#{package_name}/assets/manifest.json"
end

.clear!Object

Clear all config + loaded package Use for test only, but feel free to use in app if need


13
14
15
16
# File 'lib/tp_common/assets_loader.rb', line 13

def self.clear!
  @config = LoaderConfiguration.new(false, '/public', :latest)
  @packages = {}
end

.configure {|config| ... } ⇒ Object

Yields:

  • (config)

18
19
20
# File 'lib/tp_common/assets_loader.rb', line 18

def self.configure
  yield(config)
end

.load(package_name) ⇒ Object


30
31
32
33
34
# File 'lib/tp_common/assets_loader.rb', line 30

def self.load(package_name)
  package_name = package_name.to_sym
  packages[package_name] = new(package_name)
  packages
end

Instance Method Details

#[](asset_name) ⇒ Object


84
85
86
87
88
89
90
# File 'lib/tp_common/assets_loader.rb', line 84

def [](asset_name)
  @manifest[asset_name].tap do |asset|
    if asset.nil?
      raise AssetNotFound.new("Asset #{@package_name}/#{asset_name} is not found.")
    end
  end
end