Class: EL::AssetsMapper

Inherits:
Object
  • Object
show all
Includes:
TagFactory
Defined in:
lib/el/assets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TagFactory

#comment_tag, #comment_tag!, #css_tag, #doctype_tag, #js_tag

Constructor Details

#initialize(baseurl, opts = {}, &proc) ⇒ AssetsMapper

Returns a new instance of AssetsMapper.

Examples:


assets_mapper :vendor do

  js_tag :jquery

  chdir 'jquery-ui'
  js_tag 'js/jquery-ui.min'
  css_tag 'css/jquery-ui.min'

  cd '../bootstrap'
  js_tag 'js/bootstrap.min'
  css_tag 'css/bootstrap'
end

#=> <script src="/vendor/jquery.js" ...
#=> <script src="/vendor/jquery-ui/js/jquery-ui.min.js" ...
#=> <link href="/vendor/jquery-ui/css/jquery-ui.min.css" ...
#=> <script src="/vendor/bootstrap/js/bootstrap.min.js" ...
#=> <link href="/vendor/bootstrap/css/bootstrap.css" ...


28
29
30
31
32
33
34
35
# File 'lib/el/assets.rb', line 28

def initialize baseurl, opts = {}, &proc
  @opts = Hash[opts]
  @suffix = @opts.delete(:suffix) || ''
  baseurl = baseurl.to_s.dup.strip
  baseurl.empty? ? baseurl = nil : (baseurl =~ /\/\Z/ || baseurl << '/')
  @baseurl, @wd = baseurl.freeze, nil
  proc && self.instance_exec(&proc)
end

Instance Attribute Details

#baseurlObject (readonly)

Returns the value of attribute baseurl.



5
6
7
# File 'lib/el/assets.rb', line 5

def baseurl
  @baseurl
end

#wdObject (readonly)

Returns the value of attribute wd.



5
6
7
# File 'lib/el/assets.rb', line 5

def wd
  @wd
end

Instance Method Details

#chdir(path = nil) ⇒ Object Also known as: cd



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

def chdir path = nil
  return @wd = nil unless path
  wd = []
  if (path = path.to_s) =~ /\A\//
    path = path.sub(/\A\/+/, '')
    path = path.empty? ? [] : [path]
  else
    dirs_back, path = path.split(/\/+/).partition { |c| c == '..' }
    if @wd
      wd_chunks = @wd.split(/\/+/)
      wd = wd_chunks[0, wd_chunks.size - dirs_back.size] || []
    end
  end
  @wd = (wd + path << '').compact.join('/').freeze
end