Sha256: 31aac3591822491e3fd593f1ba93b70d752e0b9a39399c4b86ae01243519ba74

Contents?: true

Size: 1.5 KB

Versions: 17

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require "singleton"

module Cornucopia
  module Util
    class FileAssetCache
      include Singleton

      attr_accessor :asset_cache

      def initialize
        @asset_cache = {}
      end
    end

    class FileAsset
      class << self
        def asset(asset_name)
          Cornucopia::Util::FileAssetCache.instance.asset_cache[asset_name.to_sym] ||= FileAsset.new(asset_name)
          Cornucopia::Util::FileAssetCache.instance.asset_cache[asset_name.to_sym]
        end
      end

      def initialize(asset_name)
        @asset_name = asset_name
        @asset_body = nil
      end

      def body=(asset_body)
        @asset_body = asset_body
      end

      def body
        unless @asset_body
          self.source_file = path
        end

        @asset_body
      end

      def source_file=(source_file_name)
        # We read the file into memory in case the file moves or is temporary.
        @asset_body = File.read(source_file_name)
      end

      def add_file(output_location)
        unless (File.exist?(output_location))
          create_file(output_location)
        end
      end

      def create_file(output_location)
        if @asset_body
          File.open(output_location, "w+") do |write_file|
            write_file << @asset_body
          end
        else
          FileUtils.cp path, output_location
        end
      end

      def path
        File.absolute_path(File.join(File.dirname(__FILE__), "../source_files/", @asset_name))
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cornucopia-0.2.0 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.56 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.55 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.54 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.53 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.52 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.51 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.50 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.49 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.48 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.47 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.46 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.45 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.44 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.43 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.42 lib/cornucopia/util/file_asset.rb
cornucopia-0.1.41 lib/cornucopia/util/file_asset.rb