Class: Wake::Assets::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/wake/assets/renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(assets, options) ⇒ Renderer

Returns a new instance of Renderer.



5
6
7
8
9
10
# File 'lib/wake/assets/renderer.rb', line 5

def initialize(assets, options)
  @assets = assets
  @builds = options.fetch(:builds, {})
  @hosts  = options.fetch(:hosts, [])
  @inline = options[:inline]
end

Instance Method Details

#include_css(*names) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wake/assets/renderer.rb', line 12

def include_css(*names)
  names, options = extract_options(names)

  tags = if options.fetch(:inline, @inline)
           paths_for(CSS, names, options).map do |path|
             %Q{#{tag :style, options, :type => 'text/css'}#{File.read path}</style>}
           end
         else
           urls_for(CSS, names, options).map do |url|
             tag :link, options, :rel => 'stylesheet', :type => 'text/css', :href => url
           end
         end

  html_safe(tags * '')
end

#include_image(*names) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wake/assets/renderer.rb', line 28

def include_image(*names)
  names, options = extract_options(names)

  tags = if options.fetch(:inline, @inline)
           paths_for(IMG, names, options).map do |path|
             tag :img, options, :src => base64_file(path)
           end
         else
           urls_for(IMG, names, options).map { |url| tag :img, options, :src => url }
         end

  html_safe(tags * '')
end

#include_js(*names) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wake/assets/renderer.rb', line 42

def include_js(*names)
  names, options = extract_options(names)

  tags = if options.fetch(:inline, @inline)
           paths_for(JS, names, options).map do |path|
             %Q{#{tag :script, options, :type => 'text/javascript'}#{File.read path}</script>}
           end
         else
           urls_for(JS, names, options).map do |url|
             %Q{#{tag :script, options, :type => 'text/javascript', :src => url}</script>}
           end
         end

  html_safe(tags * '')
end

#paths_for(type, names, options = {}) ⇒ Object



58
59
60
# File 'lib/wake/assets/renderer.rb', line 58

def paths_for(type, names, options = {})
  @assets.paths_for(type, names, {:build => @builds[type]}.merge(options))
end

#urls_for(type, names, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/wake/assets/renderer.rb', line 62

def urls_for(type, names, options = {})
  paths = paths_for(type, names, options).map { |p| @assets.relative(p) }

  return paths unless @hosts.any?

  paths.map do |path|
    @hosts[path.hash % @hosts.size].gsub(/\/*$/, '') + path
  end
end