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/meta_data.rb,
lib/key_tree/loader/nil.rb,
lib/key_tree/refinements.rb,
lib/key_tree/refine/deep_hash.rb
Overview
rubocop:disable Style/Documentation
Defined Under Namespace
Modules: Loader, MetaData, Refine, Refinements Classes: Forest, Path, Tree
Class Method Summary collapse
- .[](contents = {}) ⇒ Object
-
.load(type, serialization, prefix: nil) ⇒ Object
Load a KeyTree from some external serialization.
- .load_from_file(file, type, prefix) ⇒ Object
-
.open(file_name) {|keytree| ... } ⇒ Object
Open an external file and load contents into a KeyTree When the file basename begins with ‘prefix@’, the prefix is prepended to all keys in the filee.
-
.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
20 21 22 |
# File 'lib/key_tree.rb', line 20 def self.[](contents = {}) contents.to_key_wood end |
.load(type, serialization, prefix: nil) ⇒ Object
Load a KeyTree from some external serialization
load type: serialization load key_prefix, type: serialization
type is upcased to form a class name that should provide a .load class method (like YAML or JSON does).
If a key_prefix is given, it will be prepended to the loaded data.
Examples:
load(:yaml, "---\na: 1\n")
> => 1
load(:yaml, "---\nb: 2\n", prefix: 'a')
> => 2
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/key_tree.rb', line 41 def self.load(type, serialization, prefix: nil) type = type.to_sym unless type.nil? loader = Loader[type] contents = loader.load(serialization) contents = { prefix => contents } unless prefix.nil? contents.to_key_wood. do || << { load: { type: type, loader: loader } } << { load: { prefix: prefix } } unless prefix.nil? end end |
.load_from_file(file, type, prefix) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/key_tree.rb', line 85 def self.load_from_file(file, type, prefix) load(type, file.read, prefix: prefix). 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 When the file basename begins with ‘prefix@’, the prefix is prepended to all keys in the filee.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/key_tree.rb', line 56 def self.open(file_name) type = File.extname(file_name)[/[^.]+/] prefix = File.basename(file_name)[/(.+)@/, 1] keytree = File.open(file_name, mode: 'rb:utf-8') do |file| load_from_file(file, type, prefix) 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.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/key_tree.rb', line 70 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 |