Class: Configureasy::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/configureasy/config_parser.rb

Overview

Load and parse content of config (YAML). Raise exception if config file not found

parser = Configureasy::ConfigParser.new('./config/config.yml')
# => <# Configureasy::ConfigParser ... >
parser.parse
# => {:hash => :content}

Set environment to load specific part of config

parser = Configureasy::ConfigParser.new('./config/environments.yml', 'development')
# => <# Configureasy::ConfigParser ... >
parser.parse
# => {:development => :content}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, environment = nil) ⇒ ConfigParser

Initialize ConfigParser instance. Params:

[+filename+]:: name of config file.
[+environment+]:: load specific environment config (*optional*).

If config file not found raise an exeception.

Returns instance of [Configureasy::ConfigParser]



31
32
33
34
35
# File 'lib/configureasy/config_parser.rb', line 31

def initialize(filename, environment = nil)
  raise "Config #{filename} not found" unless File.exist? filename
  @filename = filename
  @environment = environment || current_environment
end

Instance Attribute Details

#environmentObject (readonly)

Get environment to load data



21
22
23
# File 'lib/configureasy/config_parser.rb', line 21

def environment
  @environment
end

#filenameObject (readonly)

Get config filename



19
20
21
# File 'lib/configureasy/config_parser.rb', line 19

def filename
  @filename
end

Instance Method Details

#as_configObject

Returns config content as [Configureasy::Config]



46
47
48
# File 'lib/configureasy/config_parser.rb', line 46

def as_config
  Configureasy::Config.new self.parse
end

#parseObject

Returns config content



38
39
40
41
42
43
# File 'lib/configureasy/config_parser.rb', line 38

def parse
  content = YAML.load_file(filename)
  content.has_key?(self.environment) ? content[self.environment] : content
rescue
  raise "Invalid config content for #{filename}"
end