Class: Pec::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/pec/config_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

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

#loadObject



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 "not file exiets! #{file_name}"
  end
end