Class: Config::Sources::YAMLSource
- Inherits:
-
Object
- Object
- Config::Sources::YAMLSource
- Defined in:
- lib/config/sources/yaml_source.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, namespace = nil) ⇒ YAMLSource
constructor
A new instance of YAMLSource.
-
#load ⇒ Object
returns a config hash from the YML file.
Constructor Details
#initialize(path, namespace = nil) ⇒ YAMLSource
Returns a new instance of YAMLSource.
9 10 11 12 |
# File 'lib/config/sources/yaml_source.rb', line 9 def initialize(path, namespace=nil) @path = path @namespace = namespace end |
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/config/sources/yaml_source.rb', line 7 def namespace @namespace end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/config/sources/yaml_source.rb', line 7 def path @path end |
Instance Method Details
#load ⇒ Object
returns a config hash from the YML file
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/config/sources/yaml_source.rb', line 15 def load if @path and File.exist?(@path.to_s) result = YAML.load(ERB.new(IO.read(@path.to_s)).result) end if self.namespace namespaces = self.namespace.split("/") result = namespaces.reverse.inject(result || {}) { |acc, space| { space => acc } } end result || {} rescue Psych::SyntaxError => e raise "YAML syntax error occurred while parsing #{@path}. " \ "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ "Error: #{e.}" end |