Class: Agave::Dump::Operation::AddToDataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/agave/dump/operation/add_to_data_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, path, format, value) ⇒ AddToDataFile

Returns a new instance of AddToDataFile.



10
11
12
13
14
15
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 10

def initialize(context, path, format, value)
  @context = context
  @path = path
  @format = format
  @value = value
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 8

def context
  @context
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 8

def format
  @format
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 8

def path
  @path
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 8

def value
  @value
end

Instance Method Details

#performObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/agave/dump/operation/add_to_data_file.rb', line 17

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*agavecms:start.*#\s*agavecms:end|\Z)/m,
    "\n\n# agavecms:start\n#{content_to_add}\n# agavecms:end"
  )

  File.open(complete_path, 'w') do |f|
    f.write new_content
  end
end