Sha256: 12084e20f6309c73811c382dbdacc55566ae32da7d4d0d5b2ec6a3cd133f2fa4

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module Gemfile
        module Services
          class ReplaceFileContent
            include ConvenientService::Standard::Config

            ##
            # IMPORTANT:
            #   - `CanHaveMethodSteps` is disabled in the Standard config since it causes race conditions in combination with `CanHaveStubbedResult`.
            #   - It will be reenabled after the introduction of thread-safety specs.
            #   - Do not use it in production yet.
            #
            middlewares :step, scope: :class do
              use ConvenientService::Plugins::Service::CanHaveMethodSteps::Middleware
            end

            attr_reader :path, :content

            step :validate_path, in: :path
            step :validate_content, in: :content
            step Services::AssertFileExists, in: :path
            step :result, in: :path

            def initialize(path:, content:)
              @path = path
              @content = content
            end

            def result
              ::File.write(path, content)

              success
            end

            private

            def validate_path
              return failure(path: "Path is `nil`") if path.nil?
              return failure(path: "Path is empty") if path.empty?

              success
            end

            def validate_content
              return failure(content: "Content is `nil`") if content.nil?

              success
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
convenient_service-0.11.0 lib/convenient_service/examples/standard/gemfile/services/replace_file_content.rb