-
Notifications
You must be signed in to change notification settings - Fork 4
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
4 changed files
with
232 additions
and
37 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
81 changes: 81 additions & 0 deletions
81
src/lib/y2configuration_management/salt/form_element_factory.rb
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,81 @@ | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2019] 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 "yast" | ||
require "y2configuration_management/salt/form" | ||
require "y2configuration_management/salt/form_element_helpers" | ||
|
||
module Y2ConfigurationManagement | ||
module Salt | ||
# It builds new {FormElement}s depending on its specification type | ||
class FormElementFactory | ||
include FormElementHelpers | ||
|
||
class << self | ||
# Builds a new FormElement object based on the element specification | ||
# | ||
# This is a convenience method which relies on {FormElementFactory#build}. | ||
# | ||
# @see FormElementFactory#build | ||
def build(id, spec, parent: nil) | ||
new.build(id, spec, parent: parent) | ||
end | ||
end | ||
|
||
# Builds a new FormElement object based on the element specification and | ||
# maintaining a reference to its parent | ||
# | ||
# @param id [String] | ||
# @param spec [Hash] | ||
# @param parent [FormElement] | ||
def build(id, spec, parent: nil) | ||
type = type_for(spec) | ||
class_for(type).new(id, spec, parent: parent) | ||
end | ||
|
||
private | ||
|
||
# @param type [String] | ||
# @return [FormElement] | ||
def class_for(type) | ||
case type | ||
when "namespace", "hidden-group", "group" | ||
Container | ||
when "edit-group" | ||
Collection | ||
else | ||
FormInput | ||
end | ||
end | ||
|
||
# Returns the type for a given form element specification | ||
# | ||
# When no type is specified, it tries to infer the right one. | ||
# | ||
# @param spec [Hash] Form element specification | ||
# @return [String] Form element type | ||
def type_for(spec) | ||
return spec["$type"] if spec.key?("$type") | ||
form_elements_in(spec).size > 1 ? "group" : "text" | ||
end | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
src/lib/y2configuration_management/salt/form_element_helpers.rb
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,38 @@ | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2019] 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 Y2ConfigurationManagement | ||
module Salt | ||
# This module is meant to include methods to work with form element specifications. | ||
module FormElementHelpers | ||
# Determines which part of the given spec refers to a form element | ||
# | ||
# Usually, all elements whose name starts with `$` are supposed to be metadata, except the | ||
# special `$key` element which is considered an form input. | ||
# | ||
# @param spec [Hash] form element specification | ||
# @return [Array<Hash>] | ||
def form_elements_in(spec) | ||
spec.select { |k, _v| !k.start_with?("$") || k == "$key" } | ||
end | ||
end | ||
end | ||
end |
109 changes: 109 additions & 0 deletions
109
test/y2configuration_management/salt/form_element_factory_test.rb
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,109 @@ | ||
#!/usr/bin/env rspec | ||
# encoding: utf-8 | ||
|
||
# Copyright (c) [2019] 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_relative "../../spec_helper" | ||
require "y2configuration_management/salt/form_element_factory" | ||
|
||
describe Y2ConfigurationManagement::Salt::FormElementFactory do | ||
subject(:factory) { described_class.new } | ||
let(:parent) { instance_double(Y2ConfigurationManagement::Salt::Container) } | ||
|
||
describe "#build" do | ||
let(:spec) do | ||
{ "$type" => "boolean" } | ||
end | ||
|
||
context "given a simple element spec" do | ||
it "returns a FormInput element" do | ||
element = factory.build("field", spec) | ||
expect(element).to be_a(Y2ConfigurationManagement::Salt::FormInput) | ||
expect(element.id).to eq("field") | ||
expect(element.type).to eq(:boolean) | ||
end | ||
end | ||
|
||
context "when a parent element is given" do | ||
it "sets the parent" do | ||
element = factory.build("field", spec, parent: parent) | ||
expect(element.parent).to eq(parent) | ||
end | ||
end | ||
|
||
context "given a group element spec" do | ||
let(:spec) do | ||
{ | ||
"$type" => "group", | ||
"name" => { "$type" => "text" } | ||
} | ||
end | ||
|
||
it "returns a container element" do | ||
element = factory.build("field", spec) | ||
expect(element).to be_a(Y2ConfigurationManagement::Salt::Container) | ||
expect(element.id).to eq("field") | ||
end | ||
end | ||
|
||
context "given a namespace spec" do | ||
let(:spec) do | ||
{ | ||
"$type" => "namespace", | ||
"name" => { "$type" => "text" } | ||
} | ||
end | ||
|
||
it "returns a container element" do | ||
element = factory.build("field", spec) | ||
expect(element).to be_a(Y2ConfigurationManagement::Salt::Container) | ||
expect(element.id).to eq("field") | ||
end | ||
end | ||
|
||
context "when the type is not specified" do | ||
context "and more than 1 field is defined" do | ||
let(:spec) do | ||
{ | ||
|
||
"name" => { "$type" => "text" }, | ||
"email" => { "$type" => "text" } | ||
} | ||
end | ||
|
||
it "returns a container element" do | ||
element = factory.build("field", spec) | ||
expect(element).to be_a(Y2ConfigurationManagement::Salt::Container) | ||
end | ||
end | ||
|
||
context "and just 1 field is defined" do | ||
let(:spec) do | ||
{ "name" => { "$type" => "text" } } | ||
end | ||
|
||
it "returns a FormInput element" do | ||
element = factory.build("field", spec) | ||
expect(element).to be_a(Y2ConfigurationManagement::Salt::FormInput) | ||
end | ||
end | ||
end | ||
end | ||
end |