class Webpacker::FileLoader

Provides a base singleton-configuration pattern for loading a file, given a path

Attributes

data[RW]
mtime[RW]
path[RW]

Public Class Methods

file_path() click to toggle source
# File lib/webpacker/file_loader.rb, line 20
def file_path
  raise FileLoaderError.new("Subclass of Webpacker::FileLoader should override this method")
end
load_instance(path = file_path) click to toggle source
# File lib/webpacker/file_loader.rb, line 10
def load_instance(path = file_path)
  # Assume production is 100% cached and don't reload if file's mtime not changed
  cached = self.instance && # if we have a singleton
    (env == "production" || # skip if production bc always cached
      (File.exist?(path) && self.instance.mtime == File.mtime(path))) # skip if mtime not changed

  return if cached
  self.instance = new(path)
end
new(path) click to toggle source
# File lib/webpacker/file_loader.rb, line 38
def initialize(path)
  @path = path
  @mtime = File.exist?(path) ? File.mtime(path) : nil
  @data = load_data
end
reset() click to toggle source
# File lib/webpacker/file_loader.rb, line 24
def reset
  self.instance = nil
  load_instance
end

Private Class Methods

env() click to toggle source

Prefer the NODE_ENV to the rails env.

# File lib/webpacker/file_loader.rb, line 32
def env
  ENV["NODE_ENV"].presence || Rails.env
end

Private Instance Methods

load_data() click to toggle source
# File lib/webpacker/file_loader.rb, line 44
def load_data
  {}.freeze
end