Class: Visage::Config::File

Inherits:
Object
  • Object
show all
Defined in:
lib/visage-app/config/file.rb

Constant Summary collapse

@@config_directories =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, opts = {}) ⇒ File

Returns a new instance of File.



43
44
45
46
47
48
49
# File 'lib/visage-app/config/file.rb', line 43

def initialize(filename, opts={})
  if not ::File.exists?(filename)
    path = @@config_directories.first.join(filename)
    FileUtils.touch(path)
  end
  @file = ::File.open(filename, 'r+')
end

Class Method Details

.find(filename, opts = {}) ⇒ Object



15
16
17
18
19
# File 'lib/visage-app/config/file.rb', line 15

def self.find(filename, opts={})
  range = opts[:ignore_bundled] ? (0..-2) : (0..-1)
  potential_filenames = @@config_directories[range].map {|d| d.join(filename)}
  potential_filenames.find { |f| ::File.exists?(f) }
end

.load(filename, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/visage-app/config/file.rb', line 21

def self.load(filename, opts={})
  if not path = self.find(filename, opts)
    if opts[:create]
      path = @@config_directories.first.join(filename)
      begin
        FileUtils.touch(path)
      rescue Errno::EACCES => e
        raise Errno::EACCES, "Couldn't write #{path}. Do you have CONFIG_PATH set?"
      end
    end
  end

  YAML::load_file(path)
end

.open(filename, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/visage-app/config/file.rb', line 36

def self.open(filename, &block)
  path = self.find(filename)
  ::File.open(path, 'r+') do |f|
    block.call(f)
  end
end

Instance Method Details

#to_sObject



51
52
53
# File 'lib/visage-app/config/file.rb', line 51

def to_s
  @file.path
end