Class: ConfigFile::File
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- ConfigFile::File
- Defined in:
- lib/config_file/file.rb
Instance Attribute Summary collapse
-
#raise_on_missing_key ⇒ Object
writeonly
Sets the attribute raise_on_missing_key.
Instance Method Summary collapse
- #__data__ ⇒ Object (also: #data)
-
#initialize(file) ⇒ File
constructor
A new instance of File.
- #inspect ⇒ Object
- #method_missing(key, *args) ⇒ Object
Methods inherited from BlankSlate
find_hidden_method, hide, reveal
Constructor Details
#initialize(file) ⇒ File
Returns a new instance of File.
5 6 7 8 |
# File 'lib/config_file/file.rb', line 5 def initialize file @file = file @raise_on_missing_key = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/config_file/file.rb', line 10 def method_missing(key,*args) @data = nil if ConfigFile.dev? if val = (data[key] || data[key.to_s]) val else @raise_on_missing_key ? raise(UnknownKey, "#{key} couldn't be found in #{@file}") : nil end end |
Instance Attribute Details
#raise_on_missing_key=(value) ⇒ Object (writeonly)
Sets the attribute raise_on_missing_key
3 4 5 |
# File 'lib/config_file/file.rb', line 3 def raise_on_missing_key=(value) @raise_on_missing_key = value end |
Instance Method Details
#__data__ ⇒ Object Also known as: data
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/config_file/file.rb', line 23 def __data__ @data ||= begin d = nil ::File.open(@file) do |f| d = YAML.load ERB.new(f.read).result end d = d[ConfigFile.env] if d.has_key? ConfigFile.env d end end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/config_file/file.rb', line 19 def inspect %Q{#<ConfigFile::File #{::File.basename(@file)} #{__data__}>} end |