Module: Helpers::Assets

Defined in:
lib/helpers/assets.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rootObject

Since:

  • 0.0.2



16
17
18
19
20
21
22
23
24
# File 'lib/helpers/assets.rb', line 16

def self.root
  @@root
rescue NameError
  if Dir.exist?("public")
    MediaPath.new("public")
  else
    raise "You have to set Helpers::Assets.root!"
  end
end

.root=(root) ⇒ Object

Since:

  • 0.0.2



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/helpers/assets.rb', line 27

def self.root=(root)
  case root
  when String
    @@root = MediaPath.new(root)
  when Pathname
    @@root = MediaPath.new(root.to_s)
  when MediaPath
    @@root = root
  else
    raise ArgumentError, "Helpers::Assets.root has to be a MediaPath!"
  end
end

Instance Method Details

#image(basename, attrs = Hash.new) ⇒ Object



53
54
55
56
57
# File 'lib/helpers/assets.rb', line 53

def image(basename, attrs = Hash.new)
  path = Assets.root.join("images/#{basename}")
  default = {src: path.url, alt: path.basename}
  SelfCloseTag.new(:img, default.merge(attrs))
end

#javascript(basename) ⇒ Object

Since:

  • 0.0.2



41
42
43
44
# File 'lib/helpers/assets.rb', line 41

def javascript(basename)
  path = Assets.root.join("javascripts/#{basename}")
  Tag.new(:script, src: path.url, type: "text/javascript")
end

#javascripts(*names) ⇒ Object

Since:

  • 0.0.2



60
61
62
# File 'lib/helpers/assets.rb', line 60

def javascripts(*names)
  names.map { |name| self.javascript(name) }.join("\n")
end

#stylesheet(basename, attrs = Hash.new) ⇒ Object

Since:

  • 0.0.2



47
48
49
50
51
# File 'lib/helpers/assets.rb', line 47

def stylesheet(basename, attrs = Hash.new)
  path = Assets.root.join("stylesheets/#{basename}")
  default = {href: path.url, media: "screen", rel: "stylesheet", type: "text/css"}
  SelfCloseTag.new(:link, default.merge(attrs))
end

#stylesheets(*names) ⇒ Object

Since:

  • 0.0.2



65
66
67
# File 'lib/helpers/assets.rb', line 65

def stylesheets(*names)
  names.map { |name| self.stylesheet(name) }.join("\n")
end