lib/key_tree.rb in key_tree-0.4.0 vs lib/key_tree.rb in key_tree-0.4.1
- old
+ new
@@ -1,8 +1,9 @@
require 'key_tree/version'
require 'key_tree/tree'
require 'key_tree/forest'
+require 'key_tree/loader'
# Manage a tree of keys
#
# Example:
# kt=KeyTree[a: 1, b: { c: 2 }]
@@ -38,22 +39,23 @@
unless typed_serialization.size == 1
raise ArgumentError, "pick one: #{typed_serialization.keys}"
end
type, serialization = typed_serialization.flatten
-
- loader = get_loader(type)
+ loader = Loader[type]
self[loader.load(serialization)].with_meta_data do |meta_data|
- meta_data << { load: { type: type.to_sym,
+ meta_data << { load: { type: type,
loader: loader } }
end
end
# Open an external file and load contents into a KeyTree
#
def self.open(file_name)
type = File.extname(file_name)[/[^.]+/]
+ type = type.to_sym unless type.nil?
+
keytree = File.open(file_name, mode: 'rb:utf-8') do |file|
load_from_file(file, type)
end
return keytree unless block_given?
@@ -74,19 +76,9 @@
result << open_all(path, follow_links: follow_links, recurse: true)
end
end
private_class_method
-
- # Get a class for loading external serialization for +type+
- # +require+s the class provider if necessary.
- #
- def self.get_loader(type)
- Class.const_get(type.upcase)
- rescue NameError
- require type.to_s
- retry
- end
def self.load_from_file(file, type)
load(type => file.read).with_meta_data do |meta_data|
file_path = file.path
meta_data << { file: { path: file_path,