Skip to content

Commit

Permalink
Add SystemService#changed_value?
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs authored and joseivanlopez committed Jul 17, 2018
1 parent 3a28527 commit 2cb0c5e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
58 changes: 45 additions & 13 deletions library/system/src/lib/yast2/system_service.rb
Expand Up @@ -49,6 +49,8 @@ module Yast2
class SystemService
extend Forwardable

Change = Struct.new(:old_value, :new_value)

# @return [Yast::SystemdService]
attr_reader :service

Expand Down Expand Up @@ -101,7 +103,7 @@ def initialize(service)
#
# @return [Symbol] Start mode (:on_boot, :on_demand, :manual)
def start_mode
changes[:start_mode] || current_start_mode
new_value_for(:start_mode) || current_start_mode
end

# Sets the service start mode
Expand All @@ -117,9 +119,9 @@ def start_mode=(mode)
end

if mode == current_start_mode
changes.delete(:start_mode)
unregister_change(:start_mode)
else
changes[:start_mode] = mode
register_change(:start_mode, current_start_mode, mode)
end
end

Expand All @@ -128,19 +130,20 @@ def start_mode=(mode)
# The given value will be applied after calling #save.
#
# @param value [Boolean] true to set this service as active
def active=(active)
if active == service.active?
changes.delete(:active)
def active=(value)
if value == service.active?
unregister_change(:start_mode)
else
changes[:active] = active
register_change(:active, service.active?, value)
end
end

# Determine whether the service will be active after calling #save
#
# @return [Boolean] true if the service must be active; false otherwise
def active
return changes[:active] unless changes[:active].nil?
new_value = new_value_for(:active)
return new_value unless new_value.nil?
service.active?
end

Expand All @@ -157,7 +160,7 @@ def save(ignore_status: false)

# Reverts stored changes
def reload
changes.clear
clear_changes
@current_start_mode = nil
end

Expand Down Expand Up @@ -194,6 +197,13 @@ def search_terms
terms
end

# Determines whether a value has been changed
#
# @return [Boolean] true if the value has been changed; false otherwise
def changed_value?(key)
changes.key?(key)
end

private

# @return [Hash<String,Object>]
Expand All @@ -213,8 +223,8 @@ def current_start_mode

# Sets start mode to the underlying system
def save_start_mode
return unless changes[:start_mode]
case changes[:start_mode]
return unless new_value_for(:start_mode)
case new_value_for(:start_mode)
when :on_boot
service.enable
socket.disable
Expand All @@ -229,9 +239,9 @@ def save_start_mode

# Sets service status
def set_current_status
if changes[:active] && !service.active?
if new_value_for(:active) && !service.active?
service.start
elsif changes[:active] == false && service.active?
elsif new_value_for(:active) == false && service.active?
service.stop
end
end
Expand All @@ -242,5 +252,27 @@ def set_current_status
def socket
service && service.socket
end

def unregister_change(key)
changes.delete(key)
end

def register_change(key, old_value, new_value)
changes[key] = Change.new(old_value, new_value)
end

def clear_changes
changes.clear
end

def new_value_for(key)
return nil unless changes[key]
changes[key].new_value
end

def old_value_for(key)
return nil unless changes[key]
changes[key].old_value
end
end
end
18 changes: 18 additions & 0 deletions library/system/test/yast2/system_service_test.rb
Expand Up @@ -358,4 +358,22 @@
end
end
end

describe "#changed_value?" do
context "when no value has been changed" do
it "returns true" do
expect(system_service.changed_value?(:active)).to eq(false)
end
end

context "when some value has been changed" do
before do
system_service.active = !system_service.active
end

it "returns true" do
expect(system_service.changed_value?(:active)).to eq(true)
end
end
end
end

0 comments on commit 2cb0c5e

Please sign in to comment.