Class: Cornucopia::Util::FileAsset

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/util/file_asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset_name) ⇒ FileAsset

Returns a new instance of FileAsset.



23
24
25
# File 'lib/cornucopia/util/file_asset.rb', line 23

def initialize(asset_name)
  @asset_name = asset_name
end

Class Method Details

.asset(asset_name) ⇒ Object



17
18
19
20
# File 'lib/cornucopia/util/file_asset.rb', line 17

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

Instance Method Details

#add_file(output_location) ⇒ Object



44
45
46
47
48
# File 'lib/cornucopia/util/file_asset.rb', line 44

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

#body ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cornucopia/util/file_asset.rb', line 31

def body
  unless @asset_body
    self.source_file = path
  end

  @asset_body
end

#body=(asset_body) ⇒ Object



27
28
29
# File 'lib/cornucopia/util/file_asset.rb', line 27

def body=(asset_body)
  @asset_body = asset_body
end

#create_file(output_location) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/cornucopia/util/file_asset.rb', line 50

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

#path ⇒ Object



60
61
62
# File 'lib/cornucopia/util/file_asset.rb', line 60

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

#source_file=(source_file_name) ⇒ Object



39
40
41
42
# File 'lib/cornucopia/util/file_asset.rb', line 39

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