Sha256: 95392ca978e9307a6f2dd7bc1564e92562f52120052d8bb8c4826799281fda5b

Contents?: true

Size: 1.03 KB

Versions: 49

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true
require 'dato/dump/format'

module Dato
  module Dump
    module Operation
      class AddToDataFile
        attr_reader :context, :path, :format, :value

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

        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
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
dato-0.6.9 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.8 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.7 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.6 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.5 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.3 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.3.beta lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.2 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.1 lib/dato/dump/operation/add_to_data_file.rb
dato-0.6.0 lib/dato/dump/operation/add_to_data_file.rb
dato-0.5.1 lib/dato/dump/operation/add_to_data_file.rb
dato-0.5.0 lib/dato/dump/operation/add_to_data_file.rb
dato-0.4.3 lib/dato/dump/operation/add_to_data_file.rb
dato-0.4.2 lib/dato/dump/operation/add_to_data_file.rb
dato-0.4.1 lib/dato/dump/operation/add_to_data_file.rb
dato-0.4.0 lib/dato/dump/operation/add_to_data_file.rb
dato-0.3.31 lib/dato/dump/operation/add_to_data_file.rb
dato-0.4.0.pre lib/dato/dump/operation/add_to_data_file.rb
dato-0.3.30 lib/dato/dump/operation/add_to_data_file.rb
dato-0.3.29 lib/dato/dump/operation/add_to_data_file.rb