Class: Swift::Playground::Asset

Inherits:
Object
  • Object
show all
Includes:
Util::SourceIO
Defined in:
lib/swift/playground/asset.rb

Direct Known Subclasses

Javascript, Stylesheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::SourceIO

#derived_filename, #source_as_io

Constructor Details

#initialize(content, options = {}) ⇒ Asset

Returns a new instance of Asset.



21
22
23
24
25
26
27
# File 'lib/swift/playground/asset.rb', line 21

def initialize(content, options = {})
  pathname_or_content = source_as_io(content)
  self.content = pathname_or_content.read

  filename = options[:filename] || derived_filename(pathname_or_content)
  @filename = filename || default_filename
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



19
20
21
# File 'lib/swift/playground/asset.rb', line 19

def content
  @content
end

Instance Method Details

#filename(number) ⇒ Object



29
30
31
# File 'lib/swift/playground/asset.rb', line 29

def filename(number)
  @filename % number
end

#save(destination_path, number) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/swift/playground/asset.rb', line 33

def save(destination_path, number)
  destination_path = Pathname.new(destination_path)

  expanded_filename = filename(number)
  path = destination_path.join(expanded_filename)

  FileUtils.mkdir_p path.dirname
  path.open('w') do |file|
    file.write content
  end
end