Sha256: 86b9ff502635658216833f5e3279dce10881c7ca23dae3567c13634731a983e4

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# Provides a base singleton-configuration pattern for loading a file, given a path
class Webpacker::FileLoader
  class NotFoundError < StandardError; end
  class FileLoaderError < StandardError; end

  class_attribute :instance
  attr_accessor :data, :mtime, :path

  class << self
    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

    def file_path
      raise FileLoaderError.new("Subclass of Webpacker::FileLoader should override this method")
    end

    def reset
      self.instance = nil
      load_instance
    end

    private

    # Prefer the NODE_ENV to the rails env.
    def env
      ENV["NODE_ENV"].presence || Rails.env
    end
  end

  private
    def initialize(path)
      @path = path
      @mtime = File.exist?(path) ? File.mtime(path) : nil
      @data = load_data
    end

    def load_data
      {}.freeze
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webpacker-react-on-rails-3.0.0.rc.1 lib/webpacker/file_loader.rb
webpacker-react-on-rails-2.0 lib/webpacker/file_loader.rb