Skip to content

Commit

Permalink
added fixup_param() to auto-convert DiskSize params
Browse files Browse the repository at this point in the history
  • Loading branch information
shundhammer committed Feb 22, 2016
1 parent 7c700be commit 596fa5f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
41 changes: 35 additions & 6 deletions src/lib/storage/abstract_device_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ module Storage
# - valid_toplevel()
# - valid_param()
#
# Optional:
# - fixup_param()
#
# From the outside, use load_yaml_file() or build_tree() to start
# generating the tree.
#
Expand Down Expand Up @@ -100,7 +103,6 @@ def build_tree(obj)
# @param content [Any] parameters and sub-products of 'name'
#
def build_tree_recursive(parent, name, content)
# puts("build_tree_recursive #{name}")
raise HierarchyError, "Don't know how to create a #{name}" unless factory_products.include?(name)

case content
Expand All @@ -112,6 +114,10 @@ def build_tree_recursive(parent, name, content)
sub_prod = content.select{ |k,v| factory_products.include?(k) }
param = content.select{ |k,v| !factory_products.include?(k) }

# Call subclass-defined fixup method if available
# to convert known value types to a better usable type
param = fixup_param(name, param ) if respond_to?(:fixup_param, true)

# Create the factory product itself: Call the corresponding create_ method
child = call_create_method(parent, name, param)

Expand All @@ -130,6 +136,9 @@ def build_tree_recursive(parent, name, content)
end
end
else # Simple value, no Hash or Array
# Intentionally not calling fixup_param() here since that method would
# not get any useful information what about the value to convert
# (since there is no hash key to compare to).
call_create_method(parent, name, content)
end
end
Expand Down Expand Up @@ -167,7 +176,7 @@ def build_tree_recursive(parent, name, content)
# def valid_param
# VALID_PARAM
# end

# Factory method to create a disk.
#
# @return [::Storage::Disk]
Expand All @@ -177,6 +186,25 @@ def build_tree_recursive(parent, name, content)
# nil
# end

# Fix up parameters to the create_xy() methods. This can be used to
# convert common parameter value types to something that is better to
# handle, possibly based on the parameter name (e.g., "size"). The name
# of the factory product is also passed to possibly narrow down where to
# do that kind of conversion.
#
# This method is optional. The base class checks with respond_to? if it
# is implemented before it is called. It is only called if 'param' is a
# hash, not if it's just a plain scalar value.
#
# @param name [String] factory product name
# @param param [Hash] create_xy() parameters
#
# @return [Hash or Scalar] changed parameters
#
# def fixup_param(name, param)
# param
# end

private

# Return the factory methods of this factory: All methods that start with
Expand All @@ -196,7 +224,7 @@ def factory_methods
def factory_products
if @factory_products_cache == nil
@factory_products_cache = factory_methods.map { |m| m.to_s.gsub(/^create_/, "") }

# For some of the products there might not be a create_ method, so
# let's add the valid hierarchy description
@factory_products_cache += valid_hierarchy.keys + valid_hierarchy.values.flatten
Expand All @@ -210,7 +238,7 @@ def factory_products
#
# @param obj [Hash]
# @return [String, Object] hash key and hash content
#
#
def break_up_hash(obj)
name = obj.keys.first.to_s
raise HierarchyError, "Expected hash, not #{obj}" unless obj.is_a?(Hash)
Expand Down Expand Up @@ -255,11 +283,12 @@ def check_hierarchy(parent, child)
#
def call_create_method(parent, name, arg)
create_method = "create_#{name}".to_sym

if respond_to?(create_method, true)
log.info("#{create_method}( #{arg} )")
self.send(create_method, parent, arg)
else
log.warn("No method #{create_method} defined")
log.warn("WARNING: No method #{create_method}() defined")
nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/lib/storage/fake-devicegraphs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ disk:
label: root

- partition:
size: remaining
size: unlimited
name: /dev/sda4
type: extended

Expand Down
20 changes: 19 additions & 1 deletion src/lib/storage/fake_device_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require "yast"
require "storage"
require_relative "abstract_device_factory.rb"
require_relative "disk_size.rb"
require "pp"

module Yast
Expand Down Expand Up @@ -94,14 +95,31 @@ def valid_param
VALID_PARAM
end

# Fix up parameters to the create_xy() methods. In this instance, this is
# used to convert any parameter called "size" to a DiskSize that can be
# used directly.
#
# This method is optional. The base class checks with respond_to? if it
# is implemented before it is called.
#
# @param name [String] factory product name
# @param param [Hash] create_xy() parameters
#
# @return [Hash or Scalar] changed parameters
#
def fixup_param(name, param)
# log.info("Fixing up #{param} for #{name}")
param.map { |key, value| [key, key == "size" ? DiskSize::parse(value) : value ] }.to_h
end

#
# Factory methods
#
# The AbstractDeviceFactory base class will collect all methods starting
# with "create_" via Ruby introspection (methods()) and use them for
# creating factory products.
#

# Factory method to create a disk.
#
# @return [::Storage::Disk]
Expand Down

0 comments on commit 596fa5f

Please sign in to comment.