Class: Asteroid::FileReference

Inherits:
Object
  • Object
show all
Defined in:
lib/asteroid/file_reference.rb

Direct Known Subclasses

ScriptReference

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ FileReference

Returns a new instance of FileReference.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



21
22
23
# File 'lib/asteroid/file_reference.rb', line 21

def data
  @data
end

Class Method Details

.search_pathsObject



8
9
10
# File 'lib/asteroid/file_reference.rb', line 8

def search_paths
  @search_paths ||= []
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/asteroid/file_reference.rb', line 62

def exists?
  # should always be yes
  File.exists?(@filename)
end

#filenameObject



76
77
78
# File 'lib/asteroid/file_reference.rb', line 76

def filename
  @filename
end

#nameObject



54
55
56
# File 'lib/asteroid/file_reference.rb', line 54

def name
  File.basename @filename
end

#render(extra = nil) ⇒ Object



93
94
95
96
97
98
99
# 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_filenameObject



80
81
82
83
84
85
86
87
88
89
90
91
# 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?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# 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

#typeObject



58
59
60
# File 'lib/asteroid/file_reference.rb', line 58

def type
  name.split('.').last.to_sym
end