Class: Sphere::Package::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sphere/package/base.rb

Direct Known Subclasses

Javascript, Stylesheet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sources) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/sphere/package/base.rb', line 11

def initialize(name, sources)
  @name    = name
  @sources = sources
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/sphere/package/base.rb', line 9

def name
  @name
end

Class Method Details

.extensionObject



5
6
7
# File 'lib/sphere/package/base.rb', line 5

def self.extension
  raise "Abstract Method"
end

Instance Method Details

#compile(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sphere/package/base.rb', line 28

def compile(options = {})
  unless out_of_date?
    STDOUT.puts "unchanged #{public_file_name}" if options[:verbose] == true
    return unless options[:force] == true
  end

  STDOUT.puts "\e[32m  compile #{public_file_name}\e[0m" if options[:verbose] == true
  content = ""
  expanded_sources.each do |source|
    content << open(source).read
  end
  content = compress(content) if Sphere.config.compress?

  FileUtils.mkdir_p(File.dirname(file))
  file.open('w') {|f| f.write(content) }
end

#fileObject



16
17
18
# File 'lib/sphere/package/base.rb', line 16

def file
  @file ||= Sphere.config.asset_root.join("#{name}.#{self.class.extension}")
end

#out_of_date?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/sphere/package/base.rb', line 24

def out_of_date?
  !file.exist? || mtime > file.mtime
end

#public_file_nameObject



20
21
22
# File 'lib/sphere/package/base.rb', line 20

def public_file_name
  file.to_s.sub "#{Sphere.config.public_path}/", ''
end

#source_filesObject



51
52
53
# File 'lib/sphere/package/base.rb', line 51

def source_files
  expanded_sources - source_urls
end

#source_urlsObject



55
56
57
# File 'lib/sphere/package/base.rb', line 55

def source_urls
  expanded_sources.select {|s| url?(s) }
end

#sourcesObject



45
46
47
48
49
# File 'lib/sphere/package/base.rb', line 45

def sources
  @sources.map do |source|
    url?(source) ? source : relative_paths(source)
  end.flatten
end