Class: Yacl::Loader::YamlFile
- Inherits:
-
Yacl::Loader
- Object
- Yacl::Loader
- Yacl::Loader::YamlFile
- Defined in:
- lib/yacl/loader/yaml_file.rb
Overview
YamlFile loads a Properites instance from a single yaml file.
Examples:
yd = YamlFile.new( :filename => "./config/database.yml" )
props = yd.properties
props.adapter #=> 'postgres'
You may also create a YamlFile and have it use the propety from a Properites instance to indicate the directory to load from.
other_props.config.file # => ./config/database.yml
yd = YamFile.new( :properties => other_props, :parameter => 'config.file' )
props = yd.properties
props.adapter # => postgre
Defined Under Namespace
Classes: Error
Instance Attribute Summary
Attributes inherited from Yacl::Loader
Class Method Summary collapse
-
.load_properties(filename, scope) ⇒ Object
Internal: load a Properties from the given filename.
-
.scoped(p, scope, filename) ⇒ Object
Internal: scope the given Properties that are loaded from the given filename.
-
.validate_and_load_properties(filename, scope = nil) ⇒ Object
Internal: load a Properties from the given filename.
-
.validate_file(path) ⇒ Object
Internal: Validate that the give Pathname is a valid, readable file.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ YamlFile
constructor
A new instance of YamlFile.
- #properties ⇒ Object
Methods inherited from Yacl::Loader
extract_path, mapify_key, #reference_properties
Constructor Details
#initialize(opts = {}) ⇒ YamlFile
Returns a new instance of YamlFile.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/yacl/loader/yaml_file.rb', line 24 def initialize( opts = {} ) super @path = YamlFile.extract_path( ) @scope = .fetch( :scope, nil ) @parameter = YamlFile.mapify_key( [:parameter] ) if (not @path) and (reference_properties and @parameter) then @path = Pathname.new( reference_properties.get( *@parameter ) ) end YamlFile.validate_file( @path ) end |
Class Method Details
.load_properties(filename, scope) ⇒ Object
Internal: load a Properties from the given filename.
filename - The name of the yaml file to load scope - scope the loaded Properties by the given scope
Returns a Properties instance.
77 78 79 80 81 82 83 |
# File 'lib/yacl/loader/yaml_file.rb', line 77 def load_properties( filename, scope ) loaded = ::YAML.load_file( filename ) raise Error, "#{filename} does not contain a top level hash" unless loaded.kind_of?( Hash ) p = Yacl::Properties.new( loaded ) return scoped( p, scope, filename ) end |
.scoped(p, scope, filename) ⇒ Object
Internal: scope the given Properties that are loaded from the given filename.
p - The Properties to scope by scope scope - The scope to apply to p filename - The filename from which Properties was loaded
Raises Error if the scope does not exist
Returns a new Properties instance.
95 96 97 98 99 |
# File 'lib/yacl/loader/yaml_file.rb', line 95 def scoped( p, scope, filename ) return p unless scope raise Error, "#{filename} does not contain a top level scope of '#{scope}'. Options are #{p.scopes.join(", ")}" unless p.has_scope?( scope ) return p.scoped_by( scope ) end |
.validate_and_load_properties(filename, scope = nil) ⇒ Object
Internal: load a Properties from the given filename.
filename - The name of the yaml file to load scope - scope the loaded Properties by the given scope
Returns a Properties instance.
49 50 51 52 |
# File 'lib/yacl/loader/yaml_file.rb', line 49 def validate_and_load_properties( filename, scope = nil ) validate_file( filename ) YamlFile.load_properties( filename, scope ) end |
.validate_file(path) ⇒ Object
Internal: Validate that the give Pathname is a valid, readable file
path - the Patname we are going to check
Validates:
1) that path has a value 1) that the path exists 2) that the path is readable
Raises an error if either of the above failes
65 66 67 68 69 |
# File 'lib/yacl/loader/yaml_file.rb', line 65 def validate_file( path ) raise Error, "No path specified" unless path raise Error, "#{path} does not exist" unless path.exist? raise Error, "#{path} is not readable" unless path.readable? end |
Instance Method Details
#properties ⇒ Object
38 39 40 |
# File 'lib/yacl/loader/yaml_file.rb', line 38 def properties YamlFile.validate_and_load_properties( @path, @scope ) end |