Refactor actions with Action#setup
This commit is contained in:
@@ -2,27 +2,23 @@ module Producer
|
||||
module Core
|
||||
module Actions
|
||||
class FileAppend < Action
|
||||
def setup
|
||||
@path, @content = arguments
|
||||
end
|
||||
|
||||
def name
|
||||
'file_append'
|
||||
end
|
||||
|
||||
def apply
|
||||
fs.file_write path, combined_content
|
||||
end
|
||||
|
||||
def path
|
||||
arguments[0]
|
||||
end
|
||||
|
||||
def content
|
||||
arguments[1]
|
||||
fs.file_write @path, combined_content
|
||||
end
|
||||
|
||||
def combined_content
|
||||
original_content = fs.file_read(path)
|
||||
original_content = fs.file_read(@path)
|
||||
|
||||
return content unless original_content
|
||||
original_content + content
|
||||
return @content unless original_content
|
||||
original_content + @content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -2,28 +2,20 @@ module Producer
|
||||
module Core
|
||||
module Actions
|
||||
class FileReplaceContent < Action
|
||||
def setup
|
||||
@path, @pattern, @replacement = arguments
|
||||
end
|
||||
|
||||
def name
|
||||
'file_replace_content'
|
||||
end
|
||||
|
||||
def apply
|
||||
fs.file_write path, replaced_content
|
||||
end
|
||||
|
||||
def path
|
||||
arguments[0]
|
||||
end
|
||||
|
||||
def pattern
|
||||
arguments[1]
|
||||
end
|
||||
|
||||
def replacement
|
||||
arguments[2]
|
||||
fs.file_write @path, replaced_content
|
||||
end
|
||||
|
||||
def replaced_content
|
||||
fs.file_read(path).gsub pattern, replacement
|
||||
fs.file_read(@path).gsub @pattern, @replacement
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -2,11 +2,10 @@ module Producer
|
||||
module Core
|
||||
module Actions
|
||||
class FileWriter < Action
|
||||
def initialize(env, *args, **options)
|
||||
super
|
||||
@path, @content = arguments
|
||||
@options[:permissions] = @options.delete :mode if @options.key? :mode
|
||||
@options[:owner] = @options.delete :user if @options.key? :user
|
||||
def setup
|
||||
@path, @content = arguments
|
||||
@options[:permissions] = @options.delete :mode if options.key? :mode
|
||||
@options[:owner] = @options.delete :user if options.key? :user
|
||||
end
|
||||
|
||||
def name
|
||||
|
@@ -2,8 +2,7 @@ module Producer
|
||||
module Core
|
||||
module Actions
|
||||
class Mkdir < Action
|
||||
def initialize(env, *args, **options)
|
||||
super
|
||||
def setup
|
||||
@options[:permissions] = @options.delete :mode if @options.key? :mode
|
||||
@options[:owner] = @options.delete :user if @options.key? :user
|
||||
end
|
||||
|
Reference in New Issue
Block a user