Skip to content

Commit

Permalink
Rename form "path" (eg. ".root.foo.bar") to "locator"
Browse files Browse the repository at this point in the history
To distinguish it from Yast::Path
and filesystem paths (sometimes Pathname, sometimes String)
  • Loading branch information
mvidner committed Dec 20, 2018
1 parent 0f07227 commit 00a4431
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 125 deletions.
16 changes: 8 additions & 8 deletions src/lib/y2configuration_management/salt/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.class_for(type)
#
# scalar values, groups and collections
class FormElement
PATH_DELIMITER = ".".freeze
LOCATOR_DELIMITER = ".".freeze
# @return [String] the key for the pillar
attr_reader :id
# @return [Symbol]
Expand Down Expand Up @@ -124,14 +124,14 @@ def initialize(id, spec, parent:)
@parent = parent
end

# Return the absolute path of this form element in the actual form
# Return the absolute locator of this form element in the actual form
#
# FIXME: possible implementation of the form element path
# FIXME: possible implementation of the form element locator
#
# @return [String]
def path
prefix = parent ? parent.path : ""
"#{prefix}#{PATH_DELIMITER}#{id}"
def locator
prefix = parent ? parent.locator : ""
"#{prefix}#{LOCATOR_DELIMITER}#{id}"
end

private
Expand Down Expand Up @@ -183,11 +183,11 @@ def initialize(id, spec, parent:)

# Recursively looks for a particular {FormElement}
#
# @example look for a FormElement by a specific name, path or id
# @example look for a FormElement by a specific name, locator or id
#
# f = Y2ConfigurationManagemenet.from_file("form.yml")
# f.find_element_by(name: "subnets") #=> <Collection @name="subnets"
# f.find_element_by(path: ".root.dhcpd") #=> <Container @name="dhcpd"
# f.find_element_by(locator: ".root.dhcpd") #=> <Container @name="dhcpd"
# f.find_element_by(id: "hosts") #=> <Container @id="hosts"
#
# @param arg [Hash]
Expand Down
46 changes: 23 additions & 23 deletions src/lib/y2configuration_management/salt/form_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,39 @@ def show_main_dialog

# Convenience method for returning the value of a given element
#
# @param path [String] Path to the element
# @param index [Integer] Element's index when path refers to a collection
def get(path, index = nil)
@data.get(path, index)
# @param locator [String] Locator of the element
# @param index [Integer] Element's index when locator refers to a collection
def get(locator, index = nil)
@data.get(locator, index)
end

# Opens a new dialog in order to add a new element to a collection
#
# @param path [String] Collection's path
def add(path)
result = edit_item(path, nil)
# @param locator [String] Collection's locator
def add(locator)
result = edit_item(locator, nil)
return if result.nil?
@data.add_item(path, result.values.first)
@data.add_item(locator, result.values.first)
refresh_main_form
end

# Opens a new dialog in order to edit an element within a collection
#
# @param path [String] Collection's path
# @param locator [String] Collection's locator
# @param index [Integer] Element's index
def edit(path, index)
result = edit_item(path, get(path, index))
def edit(locator, index)
result = edit_item(locator, get(locator, index))
return if result.nil?
@data.update_item(path, index, result.values.first)
@data.update_item(locator, index, result.values.first)
refresh_main_form
end

# Removes an element from a collection
#
# @param path [String] Collection's path
# @param locator [String] Collection's locator
# @param index [Integer] Element's index
def remove(path, index)
@data.remove_item(path, index)
def remove(locator, index)
@data.remove_item(locator, index)
refresh_main_form
end

Expand All @@ -131,25 +131,25 @@ def form_builder
def main_form
return @main_form if @main_form
@main_form = form_builder.build(form.root.elements)
@main_form.value = get(form.root.path)
@main_form.value = get(form.root.locator)
@main_form
end

# Refreshes the main form content
def refresh_main_form
data.update(form.root.path, main_form.current_values)
main_form.refresh(get(form.root.path))
data.update(form.root.locator, main_form.current_values)
main_form.refresh(get(form.root.locator))
end

# Displays a form to edit a given item
#
# @param path [String] Collection path
# @param locator [String] Collection locator
# @param item [Object] Item to edit
# @return [Hash,nil] edited data; `nil` when the user cancels the dialog
def edit_item(path, item)
element = form.find_element_by(path: path)
def edit_item(locator, item)
element = form.find_element_by(locator: locator)
widget_form = form_builder.build(element.prototype)
wid = path[1..-1].split(".").last
wid = locator[1..-1].split(".").last
widget_form.value = { wid => item }
show_popup(element.name, widget_form)
end
Expand All @@ -168,7 +168,7 @@ def show_popup(title, widget)
# version.
def next_handler
main_form.store
data.update(form.root.path, main_form.result)
data.update(form.root.locator, main_form.result)
pillar.data = data.to_h.fetch("root", {})
puts pillar.dump
true
Expand Down
50 changes: 25 additions & 25 deletions src/lib/y2configuration_management/salt/form_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Salt
# @todo The support for collections is rather simple and nesting collections is not supported.
# We might consider using JSON Patch to modify the data.
class FormData
PATH_DELIMITER = ".".freeze
LOCATOR_DELIMITER = ".".freeze
# @return [Y2ConfigurationManagement::Salt::Form] Form
attr_reader :form
# @return [Y2ConfigurationManagement::Salt::Pillar] Pillar
Expand All @@ -42,18 +42,18 @@ def initialize(form, pillar = Pillar.new(data: {}))

# Returns the value of a given element
#
# @param path [String] Path to the element
def get(path, index = nil)
value = @data.dig(*path_to_parts(path)) || default_for(path)
# @param locator [String] Locator of the element
def get(locator, index = nil)
value = @data.dig(*locator_to_parts(locator)) || default_for(locator)
index ? value.at(index) : value
end

# Updates an element's value
#
# @param path [String] Path to the collection
# @param locator [String] Locator of the collection
# @param value [Object] New value
def update(path, value)
parts = path_to_parts(path)
def update(locator, value)
parts = locator_to_parts(locator)
parent_parts = parts[0..-2]
parent = @data
parent = parent.dig(* parent_parts) unless parent_parts.empty?
Expand All @@ -62,27 +62,27 @@ def update(path, value)

# Adds an element to a collection
#
# @param path [String] Path to the collection
# @param locator [String] Locator of the collection
# @param value [Hash] Value to add
def add_item(path, value)
collection = get(path)
def add_item(locator, value)
collection = get(locator)
collection.push(value)
end

# @param path [String] Path to the collection
# @param locator [String] Locator of the collection
# @param index [Integer] Position of the element to remove
# @param value [Object] New value
def update_item(path, index, value)
collection = get(path)
def update_item(locator, index, value)
collection = get(locator)
collection[index] = value
end

# Removes an element from a collection
#
# @param path [String] Path to the collection
# @param locator [String] Locator of the collection
# @param index [Integer] Position of the element to remove
def remove_item(path, index)
collection = get(path)
def remove_item(locator, index)
collection = get(locator)
collection.delete_at(index)
end

Expand All @@ -97,17 +97,17 @@ def to_h

# Default value for a given element
#
# @param path [String] Element path
def default_for(path)
element = form.find_element_by(path: path)
# @param locator [String] Element locator
def default_for(locator)
element = form.find_element_by(locator: locator)
element ? element.default : nil
end

# Split the path into different parts
# Split the locator into different parts
#
# @param path [String] Element path
def path_to_parts(path)
path[1..-1].split(PATH_DELIMITER)
# @param locator [String] Element locator
def locator_to_parts(locator)
locator[1..-1].split(LOCATOR_DELIMITER)
end

# Builds a hash to keep the form data
Expand All @@ -129,8 +129,8 @@ def data_for_element(element, data)
defaults = element.elements.reduce({}) { |a, e| a.merge(data_for_element(e, data)) }
{ element.id => defaults }
else
# TODO: we probably should remove the .root path prefix
value = data.dig(*path_to_parts(element.path.gsub(/^\.(root)?/, "")))
# TODO: we probably should remove the .root locator prefix
value = data.dig(*locator_to_parts(element.locator.gsub(/^\.(root)?/, "")))
{ element.id => value.nil? ? element.default : value }
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/lib/y2configuration_management/widgets/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Boolean < ::CWM::CheckBox
attr_reader :label
# @return [Boolean] Default value
attr_reader :default
# @return [String] Form path
attr_reader :path
# @return [String] Form locator
attr_reader :locator
# @return [String] Form element id
attr_reader :id

Expand All @@ -41,7 +41,7 @@ def initialize(spec, controller)
@label = spec.label
@default = spec.default == true # nil -> false
@controller = controller
@path = spec.path
@locator = spec.locator
@id = spec.id
self.widget_id = "boolean:#{spec.id}"
end
Expand Down
16 changes: 8 additions & 8 deletions src/lib/y2configuration_management/widgets/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Widgets
# This widget uses a table to display a collection of elements and offers
# buttons to add, remove and edit them.
class Collection < ::CWM::CustomWidget
attr_reader :label, :min_items, :max_items, :controller, :path, :id, :headers
attr_reader :label, :min_items, :max_items, :controller, :locator, :id, :headers

# @return [Array<String>] Headers for the collection table
attr_reader :headers
Expand All @@ -45,7 +45,7 @@ def initialize(spec, controller)
@min_items = spec.min_items
@max_items = spec.max_items
@controller = controller
@path = spec.path # form element path
@locator = spec.locator # form element locator
@id = spec.id
@headers, @headers_ids = headers_from_prototype(spec.prototype)
self.widget_id = "collection:#{spec.id}"
Expand All @@ -58,7 +58,7 @@ def initialize(spec, controller)
def contents
VBox(
Table(
Id("table:#{path}"),
Id("table:#{locator}"),
Opt(:notify, :immediate),
Header(*headers),
[]
Expand All @@ -76,7 +76,7 @@ def contents
#
# @param items_list [Array<Hash>] Collection items
def value=(items_list)
Yast::UI.ChangeWidget(Id("table:#{path}"), :Items, format_items(items_list))
Yast::UI.ChangeWidget(Id("table:#{locator}"), :Items, format_items(items_list))
@value = items_list
end

Expand All @@ -95,11 +95,11 @@ def handle_all_events
def handle(event)
case event["ID"]
when "#{widget_id}_add".to_sym
controller.add(path)
controller.add(locator)
when "#{widget_id}_edit".to_sym
controller.edit(path, selected_row) if selected_row
controller.edit(locator, selected_row) if selected_row
when "#{widget_id}_remove".to_sym
controller.remove(path, selected_row) if selected_row
controller.remove(locator, selected_row) if selected_row
end

nil
Expand All @@ -114,7 +114,7 @@ def handle(event)
#
# @return [Integer,nil] Index of the selected row or nil if no row is selected
def selected_row
row_id = Yast::UI.QueryWidget(Id("table:#{path}"), :CurrentItem)
row_id = Yast::UI.QueryWidget(Id("table:#{locator}"), :CurrentItem)
row_id ? row_id.to_i : nil
end

Expand Down
6 changes: 3 additions & 3 deletions src/lib/y2configuration_management/widgets/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module Widgets
class Group < ::CWM::CustomWidget
# @return [String] Widget label
attr_reader :label
# @return [String] Form element path
attr_reader :path
# @return [String] Form element locator
attr_reader :locator
# @return [Array<CWM::AbstractWidget>] Widgets which are included in the group
attr_reader :children
attr_reader :id
Expand All @@ -41,7 +41,7 @@ def initialize(spec, children, controller)
@label = spec.label
@children = children
@controller = controller
@path = spec.path
@locator = spec.locator
@id = spec.id
self.widget_id = "group:#{spec.id}"
end
Expand Down
6 changes: 3 additions & 3 deletions src/lib/y2configuration_management/widgets/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Select < ::CWM::ComboBox
attr_reader :items
# @return [String,nil] Default value
attr_reader :default
# @return [String] Form element path
attr_reader :path
# @return [String] Form element locator
attr_reader :locator
# @return [String] Form element id
attr_reader :id

Expand All @@ -42,7 +42,7 @@ def initialize(spec, controller)
@label = spec.label
@items = spec.values.map { |v| [v, v] }
@default = spec.default
@path = spec.path
@locator = spec.locator
@id = spec.id
@controller = controller
self.widget_id = "select:#{spec.id}"
Expand Down
6 changes: 3 additions & 3 deletions src/lib/y2configuration_management/widgets/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Text < ::CWM::InputField
attr_reader :label
# @return [String] Default value
attr_reader :default
# @return [String] Form path
attr_reader :path
# @return [String] Form locator
attr_reader :locator
# @return [String] Form element id
attr_reader :id

Expand All @@ -41,7 +41,7 @@ def initialize(spec, controller)
@label = spec.label
@default = spec.default.to_s
@controller = controller
@path = spec.path
@locator = spec.locator
@id = spec.id
self.widget_id = "text:#{spec.id}"
end
Expand Down

0 comments on commit 00a4431

Please sign in to comment.