Class: Visage::Config::File
- Inherits:
-
Object
- Object
- Visage::Config::File
- Defined in:
- lib/visage-app/config/file.rb
Constant Summary collapse
- @@config_directories =
[]
Class Method Summary collapse
- .find(filename, opts = {}) ⇒ Object
- .load(filename, opts = {}) ⇒ Object
- .open(filename, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(filename, opts = {}) ⇒ File
constructor
A new instance of File.
- #to_s ⇒ Object
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_s ⇒ Object
51 52 53 |
# File 'lib/visage-app/config/file.rb', line 51 def to_s @file.path end |