-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
333 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2016] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
module Yast | ||
module Storage | ||
class DevicesList | ||
include Enumerable | ||
|
||
class << self | ||
attr_accessor :device_class | ||
attr_accessor :default_delegate | ||
|
||
def list_of(klass) | ||
self.device_class = klass | ||
end | ||
|
||
def by_default_delegate_to(list) | ||
self.default_delegate = list | ||
end | ||
end | ||
|
||
attr_reader :devicegraph | ||
|
||
def initialize(devicegraph, list: nil) | ||
@devicegraph = devicegraph | ||
@list = list || full_list | ||
end | ||
|
||
def with(attrs = {}) | ||
new_list = list.select do |element| | ||
attrs.all? { |attr, value| match?(element, attr, value) } | ||
end | ||
if block_given? | ||
new_list.select!(&Proc.new) | ||
end | ||
self.class.new(devicegraph, list: new_list) | ||
end | ||
|
||
def each(&block) | ||
@list.each(&block) | ||
end | ||
|
||
def dup | ||
self.class.new(devicegraph, @list.dup) | ||
end | ||
|
||
# Returns true if the list contains no elements | ||
# | ||
# @return [Boolean] | ||
def empty? | ||
list.empty? | ||
end | ||
|
||
# Number of elements in the list | ||
# | ||
# @return [Fixnum] | ||
def length | ||
list.length | ||
end | ||
alias_method :size, :length | ||
|
||
def method_missing(meth, *args, &block) | ||
delegate_list = self.class.default_delegate | ||
if delegate_list | ||
delegate_list.send(meth, *args, &block) | ||
else | ||
super | ||
end | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :list | ||
|
||
def full_list | ||
self.class.device_class.all(devicegraph).to_a | ||
end | ||
|
||
def match?(element, attr, value) | ||
real_value = element.send(attr) | ||
return true if real_value == value | ||
value.is_a?(Array) && value.include?(real_value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2016] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
require "storage" | ||
require "storage/devices_list" | ||
require "storage/partitions_list" | ||
require "storage/filesystems_list" | ||
#require "storage/free_disk_spaces_list" | ||
require "storage/refinements/disk" | ||
|
||
module Yast | ||
module Storage | ||
class DisksList < DevicesList | ||
list_of ::Storage::Disk | ||
by_default_delegate_to :partitions | ||
|
||
using Refinements::Disk | ||
|
||
def partitions | ||
part_list = list.reduce([]) { |sum, disk| sum + disk.all_partitions } | ||
PartitionsList.new(devicegraph, list: part_list) | ||
end | ||
|
||
def filesystems | ||
fs_list = partitions.filesystems.to_a | ||
# Add filesystems not included in #partitions (directly on disk) | ||
list.each do |disk| | ||
fs_list << disk.filesystem if !disk.partition_table && disk.filesystem | ||
end | ||
FilesystemsList.new(devicegraph, list: fs_list) | ||
end | ||
|
||
def free_disk_spaces | ||
spaces_list = list.reduce([]) { |sum, disk| sum + disk.free_spaces } | ||
FreeDiskSpacesList.new(devicegraph, list: spaces_list) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2016] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
require "storage" | ||
require "storage/devices_list" | ||
|
||
module Yast | ||
module Storage | ||
class FilesystemsList < DevicesList | ||
list_of ::Storage::Filesystem | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2016] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
require "storage" | ||
require "storage/devices_list" | ||
require "storage/filesystems_list" | ||
|
||
module Yast | ||
module Storage | ||
class PartitionsList < DevicesList | ||
|
||
list_of ::Storage::Partition | ||
by_default_delegate_to :filesystems | ||
|
||
def filesystems | ||
FilesystemsList.new(devicegraph, list: list.map(&:filesystem).compact) | ||
end | ||
|
||
protected | ||
|
||
def full_list | ||
# There is no ::Storage::Partition.all | ||
devicegraph.all_disks.to_a.reduce([]) do |sum, disk| | ||
if disk.partition_table | ||
sum + disk.partition_table.partitions.to_a | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.