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/loader.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: Loader, MetaData Classes: Forest, Path, Tree
Constant Summary collapse
- VERSION =
GVB.version.freeze
- DATE =
GVB.date.freeze
Class Method Summary collapse
- .[](contents = {}) ⇒ Object
-
.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
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/key_tree.rb', line 16 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 |
.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
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/key_tree.rb', line 38 def self.load(typed_serialization = {}) unless typed_serialization.size == 1 raise ArgumentError, "pick one: #{typed_serialization.keys}" end type, serialization = typed_serialization.flatten loader = Loader[type] self[loader.load(serialization)]. do || << { load: { type: type, loader: loader } } end end |
.load_from_file(file, type) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/key_tree.rb', line 82 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 62 63 |
# File 'lib/key_tree.rb', line 53 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? 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.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/key_tree.rb', line 67 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 |