Sha256: 151ded9f991fa0a3f790bc7b583f99ac710b20388b8b61ff60283c8b08b450cd

Contents?: true

Size: 934 Bytes

Versions: 1

Compression:

Stored size: 934 Bytes

Contents

module Epubber::Services
  class Persistance
    attr_reader :workspace
    def initialize(workspace)
      @workspace = workspace
    end

    # Persist a file into the working directory with the spcified contents.
    # The file argument is the relative path to the new file, eg: test/chapters/file.xhtml
    def persist(file:, content:)
      create_path_for file
      write file, content
    end

    # Remove the working directory, thus cleaning the file system
    def clean
      FileUtils.remove_dir workspace, true
    end

  protected

    def write(file, content)
      if content.is_a?(File)
        FileUtils.cp content.path, File.dirname(path(file))
      else
        File.write path(file), content
      end
    end

    def create_path_for(file)
      dir = File.dirname path(file)
      FileUtils.mkdir_p dir unless File.directory?(dir)
    end

    def path(file)
      File.join workspace, file
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
epubber-0.2.0 lib/epubber/services/persistance.rb