Class: Dato::Dump::Operation::AddToDataFile
- Inherits:
-
Object
- Object
- Dato::Dump::Operation::AddToDataFile
- Defined in:
- lib/dato/dump/operation/add_to_data_file.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(context, path, format, value) ⇒ AddToDataFile
constructor
A new instance of AddToDataFile.
- #perform ⇒ Object
Constructor Details
#initialize(context, path, format, value) ⇒ AddToDataFile
Returns a new instance of AddToDataFile.
11 12 13 14 15 16 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 11 def initialize(context, path, format, value) @context = context @path = path @format = format @value = value end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 9 def context @context end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
9 10 11 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 9 def format @format end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 9 def path @path end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
9 10 11 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 9 def value @value end |
Instance Method Details
#perform ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dato/dump/operation/add_to_data_file.rb', line 18 def perform complete_path = File.join(context.path, path) FileUtils.mkdir_p(File.dirname(complete_path)) content_to_add = Format.dump(format, value) old_content = if File.exist? complete_path ::File.read(complete_path) else "" end new_content = old_content.sub( /\n*(#\s*datocms:start.*#\s*datocms:end|\Z)/m, "\n\n# datocms:start\n#{content_to_add}\n# datocms:end", ) File.open(complete_path, "w") do |f| f.write new_content end end |