Class: Assette::File

Inherits:
File
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/assette/file.rb

Direct Known Subclasses

Template

Defined Under Namespace

Classes: UnknownDependency

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_code_for(*args) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/assette/file.rb', line 218

def all_code_for *args
  start = Time.now
  f = open(*args)
  code = f.all_code
  code << "\n"
  code << f.comment_str % "Time taken to generate: #{Time.now-start}s"
end

.rack_resp_if_exists(path, opts = {}) ⇒ Object

This code is aweful. gotta fix it shitty



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/assette/file.rb', line 228

def rack_resp_if_exists path, opts = {}
  return unless File.exist?(path)
  start = Time.now
  f = open(path)
  begin    
    if opts[:deparr]
      code = f.path_array
      
      resp = {:dependencies => code, :target_type => f.target_class.mime_type, :target_extension => f.extension}
      
      resp[:generation_time] = Time.now-start
      
      return [200,{"Content-Type" => 'application/json'}, [resp.to_json]]
    end
    
    if opts[:nodep]
      code = [f.code]
      type = f.target_class.mime_type
    elsif opts[:dev]

      code = f.path_array.collect do |path|
        path = "http://#{opts[:env]['HTTP_HOST']}#{path}"
        f.target_class.include(path)
      end

      type = f.target_class.mime_type

    else
      code = f.all_code_array
      type = f.target_class.mime_type
    end
    
    code << "\n"
    code << f.comment_str % "Time taken to generate: #{Time.now-start}s"
    
    [200,{"Content-Type" => type.content_type},code]
  rescue Assette::CompilationError => e
    target = f.target_class
    [200,{"Content-Type" => target.mime_type.content_type},target.error(e.to_s,path)]
  end
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/assette/file.rb', line 112

def == other
  if other.instance_of?(self.class)
    other.path == path
  else
    super
  end
end

#all_codeObject



75
76
77
78
79
80
81
# File 'lib/assette/file.rb', line 75

def all_code
  if target_class.respond_to? :compile_array
    target_class.compile_array all_code_array
  else
    all_code_array.join("\n")
  end
end

#all_code_arrayObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/assette/file.rb', line 61

def all_code_array
  dep = Assette::CompiledFile.new(self)
  
  if !templates.empty?
    dep << template_set.compile
  end
  
  dependencies.each {|d| dep.add_dependency(d)}
  
  dep.add_dependency(self)
  
  dep
end

#check_config_flag(f) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/assette/file.rb', line 194

def check_config_flag f
  flag = false
  reg = /@#{f}/i
  read_config do |line|
    flag = !!(line =~ reg)
    break if flag
  end
  flag
end

#codeObject



43
44
45
46
47
# File 'lib/assette/file.rb', line 43

def code
  reader_class.new(self).compile
rescue => e
  target_class.error(e.to_s,path)
end

#comment_str(str = nil) ⇒ Object



38
39
40
41
# File 'lib/assette/file.rb', line 38

def comment_str(str = nil)
  comm = target_class.comment_str
  str ? (comm % str) : comm
end

#dependenciesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/assette/file.rb', line 124

def dependencies
  return @dependencies if @dependencies
  
  @dependencies = []
  
  read_config do |l|
    m = l.match /@require(?:s)?\s+([\w\.\/-]+)/
    next unless m
    
    p = ::File.expand_path(::File.join(dirname,m[1]))
    
    Assette.logger.info("Dependecy Checking") {p}
    
    # Check for _filename if filename doesn't exist
    unless ::File.exist?(p)
      p2 = p.gsub /(.*\/)?(.+)/, '\1_\2'
      Assette.logger.info("Dependecy Checking") {p2}
      if ::File.exist?(p2)
        p = p2
      else
        raise UnknownDependency, "Cannot find dependancy #{p} or #{p2} as required in #{path}"
      end
    end
    
    f = Assette::File.open(p)
    
    @dependencies << f
    f.dependencies.each do |d|
      @dependencies << d unless @dependencies.include?(d)
    end
    
  end
  
  Assette.logger.debug('Dependencies') {"For: #{path}\n#{@dependencies.pretty_inspect}"}
  
  @dependencies
end

#dev_tagObject



100
101
102
# File 'lib/assette/file.rb', line 100

def dev_tag
  target_class.tag "#{relative_target_path}?nodep"
end

#dirnameObject



83
84
85
# File 'lib/assette/file.rb', line 83

def dirname
  File.dirname(path)
end

#extensionObject



8
9
10
11
# File 'lib/assette/file.rb', line 8

def extension
  m = filename.match(/\.(\w+)$/)
  m[1] if m
end

#filenameObject



104
105
106
# File 'lib/assette/file.rb', line 104

def filename
  File.basename(path)
end

#mime_typeObject



34
35
36
# File 'lib/assette/file.rb', line 34

def mime_type
  @mime_type ||= MIME::Types.type_for(path).first
end

#minify?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/assette/file.rb', line 53

def minify?
  check_config_flag 'minify'
end

#never_minify?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/assette/file.rb', line 57

def never_minify?
  check_config_flag(/never[\s_]?minify/i)
end

#path_arrayObject



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/assette/file.rb', line 204

def path_array
  code = []
  
  code << "/__templates/#{templates.join(':')}" unless templates.empty?
  
  code += dependencies.collect do |d|
    d.relative_target_path
  end
  
  code << relative_target_path
end

#puts(*args) ⇒ Object



108
109
110
# File 'lib/assette/file.rb', line 108

def puts *args
  STDOUT.puts *args
end

#readObject



120
121
122
# File 'lib/assette/file.rb', line 120

def read
  @read ||= super
end

#read_configObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/assette/file.rb', line 183

def read_config
  started = nil
  @text = read
  @text.each_line do |l|
    next unless started ||= (l =~ /#{Assette::CONFIG_WRAPPER}/)
    break if l =~ /\/#{Assette::CONFIG_WRAPPER}/
    
    yield(l)
  end
end

#reader_classObject



13
14
15
16
17
18
19
# File 'lib/assette/file.rb', line 13

def reader_class
  if klass = Assette::Reader::ALL[extension]
    klass
  else
    raise Assette::Reader::UnknownReader, "Can't find reader class Assette::Reader::#{extension.capitalize}"
  end
end

#relative_target_pathObject



91
92
93
94
95
96
97
98
# File 'lib/assette/file.rb', line 91

def relative_target_path
  tp = target_path
  Assette.config.file_paths.each do |fp|
    f = File.expand_path(fp)
    tp.gsub! f, ''
  end
  tp
end

#target_classObject

Raises:

  • (Exception)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/assette/file.rb', line 21

def target_class
  return @target_class if @target_class
  ex = reader_class.outputs
  
  raise(Exception, "Define @outputs for #{reader_class.class}") unless ex
  
  if @target_class = Assette::Reader::ALL[ex.to_s]
    @target_class
  else
    raise Assette::Reader::UnknownReader, "Can't find reader class Assette::Reader::#{ex.capitalize}"
  end
end

#target_pathObject



87
88
89
# File 'lib/assette/file.rb', line 87

def target_path
  File.expand_path(File.join(dirname,filename.gsub(reader_class.extension,target_class.extension)))
end

#template_setObject



49
50
51
# File 'lib/assette/file.rb', line 49

def template_set
  Assette::TemplateSet.new(templates)
end

#templatesObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/assette/file.rb', line 162

def templates
  return @templates if @templates
  
  @templates = []
  
  read_config do |line|
    m = line.match /@template(?:s)?\s+(\w+)/
    next unless m
    
    @templates << m[1]
  end
  
  dependencies.each do |dep|
    dep.templates.each do |template|
      @templates << template unless @templates.include?(template)
    end
  end
  
  @templates
end

#textObject



4
5
6
# File 'lib/assette/file.rb', line 4

def text
  @text || (dependencies && @text)
end