Sha256: 6c6727bd4395fa4d5e46d7459af55be44e8e2b6632f08f7b0f72941783c10ef0

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

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

  class_attribute :instance
  attr_accessor :data, :mtime

  class << self
    def load_instance(path = file_path)
      # Assume production is 100% cached
      return if 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

      self.instance = new(path)
    end

    def file_path
      raise FileLoaderError.new("Subclass of WebpackerHelpers::FileLoader should override this method")
    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

1 entries across 1 versions & 1 rubygems

Version Path
webpacker_helpers-3.0.0.beta.1 lib/webpacker_helpers/file_loader.rb