Skip to content

Commit

Permalink
Simplify SystemService changes implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs authored and joseivanlopez committed Jul 17, 2018
1 parent 2cb0c5e commit abe81f8
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions library/system/src/lib/yast2/system_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ module Yast2
class SystemService
extend Forwardable

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

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

Expand Down Expand Up @@ -121,7 +119,7 @@ def start_mode=(mode)
if mode == current_start_mode
unregister_change(:start_mode)
else
register_change(:start_mode, current_start_mode, mode)
register_change(:start_mode, mode)
end
end

Expand All @@ -134,16 +132,15 @@ def active=(value)
if value == service.active?
unregister_change(:start_mode)
else
register_change(:active, service.active?, value)
register_change(: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
new_value = new_value_for(:active)
return new_value unless new_value.nil?
return new_value_for(:active) if changed_value?(:active)
service.active?
end

Expand Down Expand Up @@ -223,7 +220,7 @@ def current_start_mode

# Sets start mode to the underlying system
def save_start_mode
return unless new_value_for(:start_mode)
return unless changed_value?(:start_mode)
case new_value_for(:start_mode)
when :on_boot
service.enable
Expand Down Expand Up @@ -253,26 +250,33 @@ def socket
service && service.socket
end

# Unregisters change for a given key
#
# @param [Symbol] Change key
def unregister_change(key)
changes.delete(key)
end

def register_change(key, old_value, new_value)
changes[key] = Change.new(old_value, new_value)
# Registers change for a given key
#
# @param [Symbol] Change key
# @param [Object] New value
def register_change(key, new_value)
changes[key] = new_value
end

# Clears changes
def clear_changes
changes.clear
end

# Returns the new value for a given key
#
# @param [Symbol] Change key
# @return [Object] New value
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
return nil unless changed_value?(key)
changes[key]
end
end
end

0 comments on commit abe81f8

Please sign in to comment.