class Asteroid::FileReference

Attributes

data[RW]

Public Class Methods

new(fname) click to toggle source
# File lib/asteroid/file_reference.rb, line 23
def initialize(fname)
  if path_is_absolute?(fname)
    @filename = fname
  else 
    # Scan search paths looking for exact match
    self.class.search_paths.each do |search|
      break unless @filename.nil?
      proposed_filename = File.join(search, fname)
      if File.exists?(proposed_filename)
        @filename = proposed_filename
      end
    end

    # Scan search paths looking for partial match
    self.class.search_paths.each do |search|
      break unless @filename.nil?
      glob = File.join(search, fname + "*")
      matches = Dir[glob]
      if matches.size == 1
        @filename = matches.first
      elsif matches.size > 1
        raise "Ambiguous filename #{fname}"
      end
    end
  end

  if @filename.nil?
    raise "Could not find file #{fname}"
  end
end
search_paths() click to toggle source
# File lib/asteroid/file_reference.rb, line 8
def search_paths
  @search_paths ||= []
end

Public Instance Methods

exists?() click to toggle source
# File lib/asteroid/file_reference.rb, line 62
def exists?
  # should always be yes
  File.exists?(@filename)
end
filename() click to toggle source
# File lib/asteroid/file_reference.rb, line 76
def filename
  @filename
end
name() click to toggle source
# File lib/asteroid/file_reference.rb, line 54
def name
  File.basename @filename
end
render(extra = nil) click to toggle source
# File lib/asteroid/file_reference.rb, line 93
def render(extra = nil)
  template_data = read
  rendered_template = Template.new(:erb).render(
    template_data, 
    data.merge(extra)
  )
end
rendered_filename() click to toggle source
# File lib/asteroid/file_reference.rb, line 80
def rendered_filename
  if template?
    @rendered_filename ||= begin
      file = Tempfile.new('config')
      file.write render
      file.rewind
      file.path
    end
  else
    filename
  end
end
template?() click to toggle source
# File lib/asteroid/file_reference.rb, line 67
def template?
  last = @filename.split('.').last
  if last
    Asteroid::Config.template_engines.keys.include?(last.to_sym)
  else
    false
  end
end
type() click to toggle source
# File lib/asteroid/file_reference.rb, line 58
def type
  name.split('.').last.to_sym
end

Private Instance Methods

path_is_absolute?(path) click to toggle source
# File lib/asteroid/file_reference.rb, line 15
def path_is_absolute?(path)
  Pathname.new(path).absolute?
end