Class: Pec::ConfigFile
- Inherits:
-
Object
- Object
- Pec::ConfigFile
- Defined in:
- lib/pec/config_file.rb
Instance Attribute Summary collapse
-
#config_name ⇒ Object
Returns the value of attribute config_name.
Instance Method Summary collapse
-
#initialize(config_name) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #load ⇒ Object
- #read_file(file_name) ⇒ Object
Constructor Details
#initialize(config_name) ⇒ ConfigFile
Returns a new instance of ConfigFile.
5 6 7 |
# File 'lib/pec/config_file.rb', line 5 def initialize(config_name) self.config_name = File.exist?("#{config_name}.erb") ? "#{config_name}.erb" : config_name end |
Instance Attribute Details
#config_name ⇒ Object
Returns the value of attribute config_name.
4 5 6 |
# File 'lib/pec/config_file.rb', line 4 def config_name @config_name end |
Instance Method Details
#load ⇒ Object
9 10 11 12 13 14 |
# File 'lib/pec/config_file.rb', line 9 def load base = read_file(config_name) include_files = YAML.load(base).to_hash.find{|k,v| k.match(/^includes$/) && !v.nil? } inc = include_files ? include_files[1].map {|f| read_file(f)}.join("\n") : "" YAML.load(base + inc) end |
#read_file(file_name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pec/config_file.rb', line 16 def read_file(file_name) if File.exist?(file_name) case when file_name.match(/.erb$/) erb = ERB.new(File.read(file_name), nil, '%-') erb.result when file_name.match(/.yaml$/) || file_name.match(/.yml$/) File.read(file_name) else raise "not match file type must be yaml or erb" end else raise " #{file_name} not exiets!" end end |