Class: Juicer::DependencyResolver

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/juicer/dependency_resolver/dependency_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DependencyResolver

Constructor



7
8
9
10
# File 'lib/juicer/dependency_resolver/dependency_resolver.rb', line 7

def initialize(options = {})
  @files = []
  @options = options
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



4
5
6
# File 'lib/juicer/dependency_resolver/dependency_resolver.rb', line 4

def files
  @files
end

Instance Method Details

#each(&block) ⇒ Object

Yield files recursively. Resolve dependencies first, then call each, or any other enumerable methods.



28
29
30
# File 'lib/juicer/dependency_resolver/dependency_resolver.rb', line 28

def each(&block)
  @files.each(&block)
end

#resolve(file, &block) ⇒ Object

Resolve dependencies. This method accepts an optional block. The block will receive each file in succession. The file is included in the returned collection if the block is true for the given file. Without a block every found file is returned.



19
20
21
22
# File 'lib/juicer/dependency_resolver/dependency_resolver.rb', line 19

def resolve(file, &block)
  @files = []
  _resolve(file, &block)
end

#resolve_path(path, reference) ⇒ Object

Resolves a path relative to another. If the path is absolute (ie it starts with a protocol or /) the :document_root options has to be set as well.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/juicer/dependency_resolver/dependency_resolver.rb', line 37

def resolve_path(path, reference)
  # Absolute URL
  if path =~ %r{^(/|[a-z]+:)}
    if @options[:document_root].nil?
      msg = "Cannot resolve absolute path '#{path}' without document root option"
      raise ArgumentError.new(msg)
    end

    path.sub!(%r{^[a-z]+://[^/]+/}, '')
    return File.expand_path(File.join(@options[:document_root], path))
  end

  File.expand_path(File.join(File.dirname(reference), path))
end