summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2014-10-20 09:55:39 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2014-10-23 18:06:05 -0700
commitafd0deaf1c0ccb830b9aaf389298b40b3a6f6264 (patch)
tree1d08104fa85322efa2ecee55d79c95e3b23b76af
parent6877c6f6bbb6fd17f6a40f21f8bc068913fd0560 (diff)
downloadchef-afd0deaf1c0ccb830b9aaf389298b40b3a6f6264.tar.gz
Got rid of repeated code
-rw-r--r--lib/chef/provider/env.rb24
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/chef/provider/env.rb b/lib/chef/provider/env.rb
index e2c23dec8b..3e7b0398d7 100644
--- a/lib/chef/provider/env.rb
+++ b/lib/chef/provider/env.rb
@@ -61,8 +61,7 @@ class Chef
def compare_value
if @new_resource.delim
#e.g. check for existing value within PATH
- current_values = @current_resource.value.split(@new_resource.delim)
- not @new_resource.value.split(@new_resource.delim).all? do |val|
+ not new_values.all? do |val|
current_values.include? val
end
else
@@ -92,16 +91,14 @@ class Chef
# after we removed the element.
def delete_element
return false unless @new_resource.delim #no delim: delete the key
- values = @new_resource.value.split(@new_resource.delim)
- current_values = @current_resource.value.split(@new_resource.delim)
- needs_delete = values.any? { |v| current_values.include?(v) }
+ needs_delete = new_values.any? { |v| current_values.include?(v) }
if not needs_delete
Chef::Log.debug("#{@new_resource} element '#{@new_resource.value}' does not exist")
return true #do not delete the key
else
new_value =
- @current_resource.value.split(@new_resource.delim).select { |item|
- values.include?(item)
+ current_values.select { |item|
+ new_values.include?(item)
}.join(@new_resource.delim)
if new_value.empty?
@@ -146,14 +143,23 @@ class Chef
def modify_env
if @new_resource.delim
- current_values = @current_resource.value.split(@new_resource.delim)
- values = @new_resource.value.split(@new_resource.delim).reject do |v|
+ values = new_values.reject do |v|
current_values.include?(v)
end
@new_resource.value((values + [@current_resource.value]).join(@new_resource.delim))
end
create_env
end
+
+ # Returns the current values to split by delimiter
+ def current_values
+ @current_values ||= @current_resource.value.split(@new_resource.delim)
+ end
+
+ # Returns the new values to split by delimiter
+ def new_values
+ @new_values ||= @new_resource.value.split(@new_resource.delim)
+ end
end
end
end