Class: Distil::SourceFile

Inherits:
Object
  • Object
show all
Includes:
ErrorReporter
Defined in:
lib/distil/source-file.rb

Direct Known Subclasses

CssFile, HtmlFile, JavascriptFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorReporter

error, #has_errors?, #has_warnings?, #ignore_warnings, #ignore_warnings=, #report, #total_error_count, #total_warning_count, warning

Constructor Details

#initialize(filepath, project) ⇒ SourceFile

Returns a new instance of SourceFile.



14
15
16
17
18
19
# File 'lib/distil/source-file.rb', line 14

def initialize(filepath, project)
  @full_path= File.expand_path(filepath)
  @project= project
  @dependencies=[]
  project.cache_file(self)
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/distil/source-file.rb', line 6

def dependencies
  @dependencies
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



6
7
8
# File 'lib/distil/source-file.rb', line 6

def full_path
  @full_path
end

#is_assetObject

Returns the value of attribute is_asset.



7
8
9
# File 'lib/distil/source-file.rb', line 7

def is_asset
  @is_asset
end

#languageObject

Returns the value of attribute language.



7
8
9
# File 'lib/distil/source-file.rb', line 7

def language
  @language
end

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/distil/source-file.rb', line 6

def project
  @project
end

Instance Method Details

#add_asset(file) ⇒ Object



101
102
103
104
# File 'lib/distil/source-file.rb', line 101

def add_asset(file)
  file.is_asset=true
  assets << file
end

#add_dependency(file) ⇒ Object



92
93
94
95
# File 'lib/distil/source-file.rb', line 92

def add_dependency(file)
  return if @dependencies.include?(file)
  @dependencies << file
end

#assetsObject



97
98
99
# File 'lib/distil/source-file.rb', line 97

def assets
  @assets||=Set.new
end

#basename(suffix = "") ⇒ Object



60
61
62
# File 'lib/distil/source-file.rb', line 60

def basename(suffix="")
  File.basename(@full_path, suffix)
end

#contentObject



72
73
74
# File 'lib/distil/source-file.rb', line 72

def content
  @content ||= File.read(full_path)
end

#content_typeObject



68
69
70
# File 'lib/distil/source-file.rb', line 68

def content_type
  @content_type || self.class.content_type || File.extname(full_path)[1..-1]
end

#copy_to(folder, prefix) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/distil/source-file.rb', line 106

def copy_to(folder, prefix)
  relative= path_relative_to_folder(prefix)
  target_path= File.join(folder, relative)
  FileUtils.mkdir_p final_target_folder
  FileUtils.cp self.full_path, File.dirname(target_path)
  target_path
end

#dirnameObject



56
57
58
# File 'lib/distil/source-file.rb', line 56

def dirname
  File.dirname(@full_path)
end

#error(message, line = nil) ⇒ Object



25
26
27
# File 'lib/distil/source-file.rb', line 25

def error(message, line=nil)
  super(message, self, line)
end

#extensionObject



64
65
66
# File 'lib/distil/source-file.rb', line 64

def extension
  @extension || self.class.extension || File.extname(full_path)[1..-1]
end

#last_modifiedObject



80
81
82
# File 'lib/distil/source-file.rb', line 80

def last_modified
  @last_modified ||= File.stat(@full_path).mtime
end

#minified_content(source = content) ⇒ Object



84
85
86
# File 'lib/distil/source-file.rb', line 84

def minified_content(source=content)
  return source
end

#output_pathObject



29
30
31
32
33
# File 'lib/distil/source-file.rb', line 29

def output_path
  # SourceFiles get copied (or symlinked) into the output folder so that
  # their path is the same as that relative to the source folder
  @output_path ||= File.join(project.output_path, relative_path)
end

#path_relative_to(path) ⇒ Object



44
45
46
# File 'lib/distil/source-file.rb', line 44

def path_relative_to(path)
  Project.path_relative_to_folder(full_path, path)
end

#path_relative_to_folder(folder) ⇒ Object



88
89
90
# File 'lib/distil/source-file.rb', line 88

def path_relative_to_folder(folder)
  Project.path_relative_to_folder(@full_path, folder)
end

#relative_pathObject



35
36
37
38
39
40
41
42
# File 'lib/distil/source-file.rb', line 35

def relative_path
  return @relative_path if @relative_path
  if full_path.starts_with?(project.output_path)
    @relative_path= Project.path_relative_to_folder(full_path, project.output_path)
  else
    @relative_path=Project.path_relative_to_folder(full_path, project.source_path)
  end
end

#rewrite_content_relative_to_path(path) ⇒ Object



76
77
78
# File 'lib/distil/source-file.rb', line 76

def rewrite_content_relative_to_path(path)
  content
end

#to_sObject



48
49
50
# File 'lib/distil/source-file.rb', line 48

def to_s
  @full_path
end

#to_strObject



52
53
54
# File 'lib/distil/source-file.rb', line 52

def to_str
  @full_path
end

#warning(message, line = nil) ⇒ Object



21
22
23
# File 'lib/distil/source-file.rb', line 21

def warning(message, line=nil)
  super(message, self, line)
end