Skip to content

Commit

Permalink
Merge 4aaf368 into b2690ef
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 23, 2019
2 parents b2690ef + 4aaf368 commit 320c843
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 37 deletions.
41 changes: 4 additions & 37 deletions src/lib/y2configuration_management/salt/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
require "yaml"
require "yast"
require "y2configuration_management/salt/form_element_locator"
require "y2configuration_management/salt/form_element_factory"
require "y2configuration_management/salt/form_element_helpers"

module Y2ConfigurationManagement
module Salt
Expand Down Expand Up @@ -74,32 +76,6 @@ def find_element_by(arg)
end
end

# It builds new {FormElement}s depending on its specification type
class FormElementFactory
# 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 self.build(id, spec, parent:)
class_for(spec["$type"]).new(id, spec, parent: parent)
end

# @param type [String]
# @return [FormElement]
def self.class_for(type)
case type
when "namespace", "hidden-group", "group"
Container
when "edit-group"
Collection
else
FormInput
end
end
end

# Three different kind of elements:
#
# scalar values, groups and collections
Expand Down Expand Up @@ -201,6 +177,8 @@ def collection_key?

# Container Element
class Container < FormElement
include FormElementHelpers

# @return [Array<FormElement>]
attr_reader :elements

Expand Down Expand Up @@ -242,17 +220,6 @@ def build_elements(spec)
@elements << FormElementFactory.build(id, nested_spec, parent: self)
end
end

# 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

# Defines a collection of {FormElement}s or {Container}s all of them based in
Expand Down
81 changes: 81 additions & 0 deletions src/lib/y2configuration_management/salt/form_element_factory.rb
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 src/lib/y2configuration_management/salt/form_element_helpers.rb
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 test/y2configuration_management/salt/form_element_factory_test.rb
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

0 comments on commit 320c843

Please sign in to comment.