Class: Juicer::Asset::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/juicer/asset/path_resolver.rb

Overview

Factory class that creates Juicer::Asset::Path objects from a common set of options. Also facilitates asset host cycling on a set of asset paths.

path_resolver = Juicer::Asset::PathResolver.new(
                  :document_root => "/var/www",
                  :hosts => ["assets1.mysite.com", "assets2.mysite.com"]
                )

asset = path_resolver.resolve("../images/logo.png")
asset.document_root
#=> "/var/www"

asset.absolute_path(path_resolver.cycle_hosts)
#=> "http://assets1.mysite.com/images/logo.png"

asset = path_resolver.resolve("/favicon.ico")
asset.absolute_path(path_resolver.cycle_hosts)
#=> "http://assets2.mysite.com/favicon.ico"
Author

Christian Johansen ([email protected])

Copyright

Copyright © 2009 Christian Johansen

License

BSD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PathResolver

Initialize resolver. All options set on the resolver will be carried on to the resolved assets.


38
39
40
41
42
43
44
45
# File 'lib/juicer/asset/path_resolver.rb', line 38

def initialize(options = {})
  options[:base] ||= Dir.pwd
  @options = options
  @base = options[:base]
  @hosts = Juicer::Asset::Path.hosts_with_scheme(options[:hosts]) || []
  @current_host = 0
  @document_root = @options[:document_root]
end

Instance Attribute Details

#baseObject

Returns the value of attribute base


32
33
34
# File 'lib/juicer/asset/path_resolver.rb', line 32

def base
  @base
end

#document_rootObject (readonly)

Returns the value of attribute document_root


32
33
34
# File 'lib/juicer/asset/path_resolver.rb', line 32

def document_root
  @document_root
end

#hostsObject (readonly)

Returns the value of attribute hosts


32
33
34
# File 'lib/juicer/asset/path_resolver.rb', line 32

def hosts
  @hosts
end

Instance Method Details

#cycle_hostsObject Also known as: host

Cycle asset hosts. Returns an asset host


67
68
69
70
71
72
73
74
# File 'lib/juicer/asset/path_resolver.rb', line 67

def cycle_hosts
  return nil if @hosts.length == 0

  host = @hosts[@current_host % @hosts.length]
  @current_host += 1

  host
end

#resolve(path) ⇒ Object

Returns a Juicer::Asset::Path object for the given path, and the options set on the resolver.


51
52
53
# File 'lib/juicer/asset/path_resolver.rb', line 51

def resolve(path)
  Juicer::Asset::Path.new(path, @options)
end