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.



12
13
14
# File 'lib/cornucopia/util/file_asset.rb', line 12

def initialize(asset_name)
  @asset_name = asset_name
end

Class Method Details

.asset(asset_name) ⇒ Object



5
6
7
8
9
# File 'lib/cornucopia/util/file_asset.rb', line 5

def asset(asset_name)
  @@asset_list                    ||= {}
  @@asset_list[asset_name.to_sym] = FileAsset.new(asset_name) unless @@asset_list[asset_name.to_sym]
  @@asset_list[asset_name.to_sym]
end

Instance Method Details

#add_file(output_location) ⇒ Object



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

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

#body=(asset_body) ⇒ Object



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

def body=(asset_body)
  @asset_body = asset_body
end

#create_file(output_location) ⇒ Object



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

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



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

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

#source_file=(source_file_name) ⇒ Object



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

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