Class: OsCtl::Lib::Zfs::DatasetTree
- Inherits:
-
Object
- Object
- OsCtl::Lib::Zfs::DatasetTree
- Defined in:
- lib/libosctl/zfs/dataset_tree.rb
Overview
Encapsulates a tree of datasets and their properties
Instance Attribute Summary collapse
- #datasets ⇒ Hash<String, Zfs::DatasetTree> readonly
-
#name ⇒ String?
readonly
Full name.
- #properties ⇒ Hash<String, String> readonly
Instance Method Summary collapse
-
#[](dataset) ⇒ Zfs::DatasetTree?
Lookup dataset in the subtree.
-
#add_property(dataset, property, value) ⇒ Object
Add property to a dataset.
- #as_dataset(base: '') ⇒ Zfs::Dataset
- #do_add_property(path, name, parts, property, value) ⇒ Object protected
-
#each_tree_dataset {|tree| ... } ⇒ Object
Iterate over all datasets in the subtree.
-
#initialize(name: nil) ⇒ DatasetTree
constructor
A new instance of DatasetTree.
-
#print(indent: 0) ⇒ Object
Print the tree to the console.
Constructor Details
#initialize(name: nil) ⇒ DatasetTree
Returns a new instance of DatasetTree.
14 15 16 17 18 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 14 def initialize(name: nil) @name = name @properties = {} @datasets = {} end |
Instance Attribute Details
#datasets ⇒ Hash<String, Zfs::DatasetTree> (readonly)
11 12 13 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 11 def datasets @datasets end |
#name ⇒ String? (readonly)
Returns full name.
5 6 7 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 5 def name @name end |
#properties ⇒ Hash<String, String> (readonly)
8 9 10 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 8 def properties @properties end |
Instance Method Details
#[](dataset) ⇒ Zfs::DatasetTree?
Lookup dataset in the subtree
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 32 def [](dataset) parts = dataset.split('/') tree = self parts.each do |name| tree = tree.datasets[name] return nil if tree.nil? end tree end |
#add_property(dataset, property, value) ⇒ Object
Add property to a dataset
24 25 26 27 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 24 def add_property(dataset, property, value) parts = dataset.split('/') do_add_property([], parts.first, parts[1..], property, value) end |
#as_dataset(base: '') ⇒ Zfs::Dataset
52 53 54 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 52 def as_dataset(base: '') Zfs::Dataset.new(name, base:, properties:) end |
#do_add_property(path, name, parts, property, value) ⇒ Object (protected)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 71 def do_add_property(path, name, parts, property, value) datasets[name] ||= self.class.new(name: (path + [name]).join('/')) if parts.empty? datasets[name].properties[property] = value else datasets[name].do_add_property( path + [name], parts.first, parts[1..], property, value ) end end |
#each_tree_dataset {|tree| ... } ⇒ Object
Iterate over all datasets in the subtree
46 47 48 49 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 46 def each_tree_dataset(&block) block.call(self) datasets.each_value { |ds| ds.each_tree_dataset(&block) } end |
#print(indent: 0) ⇒ Object
Print the tree to the console
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/libosctl/zfs/dataset_tree.rb', line 57 def print(indent: 0) puts "#{' ' * indent}#{name}:" properties.each do |k, v| puts "#{' ' * indent} #{k}=#{v}" end datasets.each_value do |tree| tree.print(indent: indent + 2) end end |