lib/load_file/loader.rb in load_file-1.0.0 vs lib/load_file/loader.rb in load_file-1.1.0
- old
+ new
@@ -4,12 +4,13 @@
module LoadFile
# A class to load `file` into Hash, find or create constant by `constant_name`.
#
# @return [Hash] parsed YAML or JSON content
class Loader < Hash
- def initialize(file, constant_name)
+ def initialize(file, constant_name, namespace: Object)
@file = file
+ @namespace = namespace
@constant = find_or_define(constant_name)
update parsed_content
end
@@ -23,16 +24,16 @@
each { |key, value| constant[key] = value }
end
private
- attr_reader :file, :constant
+ attr_reader :file, :constant, :namespace
def find_or_define(constant_name)
- if Object.const_defined?(constant_name)
- Object.const_get(constant_name)
+ if namespace.const_defined?(constant_name)
+ namespace.const_get(constant_name)
else
- Object.const_set(constant_name, {})
+ namespace.const_set(constant_name, {})
end
end
def parsed_content
case File.extname(file)