class LoadFile::Loader
A class to load `file` into Hash, find or create constant by `constant_name`.
@return [Hash] parsed YAML or JSON content
Attributes
constant[R]
file[R]
namespace[R]
Public Class Methods
new(file, constant_name, namespace: Object)
click to toggle source
# File lib/load_file/loader.rb, line 9 def initialize(file, constant_name, namespace: Object) @file = file @namespace = namespace @constant = find_or_define(constant_name) update parsed_content end
Public Instance Methods
set_constant()
click to toggle source
Set `values` into `constant` if not exists.
# File lib/load_file/loader.rb, line 18 def set_constant each { |key, value| constant[key] ||= value } end
set_constant!()
click to toggle source
Override `values` into `constant`.
# File lib/load_file/loader.rb, line 23 def set_constant! each { |key, value| constant[key] = value } end
Private Instance Methods
content()
click to toggle source
# File lib/load_file/loader.rb, line 56 def content IO.read(file) end
find_or_define(constant_name)
click to toggle source
# File lib/load_file/loader.rb, line 31 def find_or_define(constant_name) if namespace.const_defined?(constant_name) namespace.const_get(constant_name) else namespace.const_set(constant_name, {}) end end
parsed_content()
click to toggle source
# File lib/load_file/loader.rb, line 39 def parsed_content case File.extname(file) when ".yml", ".yaml" Parser.yaml(content) when ".json" Parser.json(content) else raise_parser_error("don't know how to parse #{file}") end rescue Parser::ParserError raise_parser_error("#{file} format is invalid") end
raise_parser_error(message)
click to toggle source
# File lib/load_file/loader.rb, line 52 def raise_parser_error(message) raise Parser::ParserError, message end