Module: KeyTree
- Defined in:
- lib/key_tree.rb,
lib/key_tree/path.rb,
lib/key_tree/tree.rb,
lib/key_tree/forest.rb,
lib/key_tree/version.rb,
lib/key_tree/meta_data.rb
Overview
Manage a tree of keys
Example:
kt=KeyTree[a: 1, b: { c: 2 }]
kt["a"]
-> 1
kt["b.c"]
-> 2
Defined Under Namespace
Modules: MetaData Classes: Forest, Path, Tree
Constant Summary collapse
- VERSION =
GVB.version.freeze
- DATE =
GVB.date.freeze
Class Method Summary collapse
- .[](contents = {}) ⇒ Object
-
.get_loader(type) ⇒ Object
Get a class for loading external serialization for
typerequires the class provider if necessary. -
.load(typed_serialization = {}) ⇒ Object
Load a KeyTree from some external serialization.
- .load_from_file(file, type) ⇒ Object
-
.open(file_name) {|keytree| ... } ⇒ Object
Open an external file and load contents into a KeyTree.
-
.open_all(dir_name, follow_links: false, recurse: false) ⇒ Object
Open all files in a directory and load their contents into a Forest of Trees, optionally following symlinks, and recursing.
Class Method Details
.[](contents = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/key_tree.rb', line 15 def self.[](contents = {}) case contents when Hash KeyTree::Tree[contents] when Array KeyTree::Forest[*contents] else raise ArgumentError, "can't load #{contents.class} into a KeyTree" end end |
.get_loader(type) ⇒ Object
Get a class for loading external serialization for type requires the class provider if necessary.
83 84 85 86 87 88 |
# File 'lib/key_tree.rb', line 83 def self.get_loader(type) Class.const_get(type.upcase) rescue NameError require type.to_s retry end |
.load(typed_serialization = {}) ⇒ Object
Load a KeyTree from some external serialization
load type: serialization
type is upcased to form a class name that should provide a .load class method (like YAML or JSON does).
Example:
load(yaml: "---\na: 1\n")
> => 1
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/key_tree.rb', line 37 def self.load(typed_serialization = {}) unless typed_serialization.size == 1 raise ArgumentError, "pick one: #{typed_serialization.keys}" end type, serialization = typed_serialization.flatten loader = get_loader(type) self[loader.load(serialization)]. do || << { load: { type: type.to_sym, loader: loader } } end end |
.load_from_file(file, type) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/key_tree.rb', line 90 def self.load_from_file(file, type) load(type => file.read). do || file_path = file.path << { file: { path: file_path, name: File.basename(file_path), dir: File.dirname(file_path) } } end end |
.open(file_name) {|keytree| ... } ⇒ Object
Open an external file and load contents into a KeyTree
53 54 55 56 57 58 59 60 61 |
# File 'lib/key_tree.rb', line 53 def self.open(file_name) type = File.extname(file_name)[/[^.]+/] keytree = File.open(file_name, mode: 'rb:utf-8') do |file| load_from_file(file, type) end return keytree unless block_given? yield(keytree) end |
.open_all(dir_name, follow_links: false, recurse: false) ⇒ Object
Open all files in a directory and load their contents into a Forest of Trees, optionally following symlinks, and recursing.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/key_tree.rb', line 65 def self.open_all(dir_name, follow_links: false, recurse: false) Dir.children(dir_name).reduce(KeyTree::Forest.new) do |result, file| path = File.join(dir_name, file) next result if File.symlink?(path) && !follow_links stat = File.stat(path) # rubocop:disable Security/Open next result << open(path) if stat.file? # rubocop:enable Security/Open next result unless recurse && stat.directory? result << open_all(path, follow_links: follow_links, recurse: true) end end |