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.



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

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

Class Method Details

.asset(asset_name) ⇒ Object



19
20
21
22
# File 'lib/cornucopia/util/file_asset.rb', line 19

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



47
48
49
50
51
# File 'lib/cornucopia/util/file_asset.rb', line 47

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

#bodyObject



34
35
36
37
38
39
40
# File 'lib/cornucopia/util/file_asset.rb', line 34

def body
  unless @asset_body
    self.source_file = path
  end

  @asset_body
end

#body=(asset_body) ⇒ Object



30
31
32
# File 'lib/cornucopia/util/file_asset.rb', line 30

def body=(asset_body)
  @asset_body = asset_body
end

#create_file(output_location) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/cornucopia/util/file_asset.rb', line 53

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

#pathObject



63
64
65
# File 'lib/cornucopia/util/file_asset.rb', line 63

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

#source_file=(source_file_name) ⇒ Object



42
43
44
45
# File 'lib/cornucopia/util/file_asset.rb', line 42

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