Sha256: a94661376f7c1531373250af8f5fb14836c9cd5bc2878524c468166a76cf863d

Contents?: true

Size: 1.78 KB

Versions: 39

Compression:

Stored size: 1.78 KB

Contents

module Ddr::Derivatives
  class Derivative

    attr_reader :generator
    attr_accessor :workdir

    def initialize(generator)
      @generator = generator
    end

    def generate!(object)
      self.workdir = FileUtils.mkdir(File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname('',nil))).first
      source_path = create_source_file(object)
      results = generator.generate(source_path, output_path(object))
      if results.status.success?
        store(object, results.output_path)
      else
        Rails.logger.error results.stderr
        raise Ddr::Models::DerivativeGenerationFailure,
                "Failure generating #{self.class.name} for #{object.id}: #{results.stderr}"
      end
      FileUtils.remove_dir(workdir)
    end

    # Whether a derivative can be generated for the object.
    # Implemented in each subclass.
    def self.generatable?(object)
      raise NotImplementedError
    end

    # Whether the object has this derivative.
    # Implemented in each subclass.
    def self.has_derivative?(object)
      raise NotImplementedError
    end

    # Delete the existing derivative
    # Implemented in each subclass.
    def delete!(object)
      raise NotImplementedError
    end

    protected

    # The path to which the generated output is written.
    # Implemented in each subclass.
    def output_path(object)
      raise NotImplementedError
    end

    # The mechanism by which the generated derivative is registered with the ActiveFedora object.
    # Implemented in each subclass.
    def store(object, output_path)
      raise NotImplementedError
    end

    private

    def create_source_file(object)
      file_path = File.join(workdir, "source")
      file = File.new(file_path, "wb")
      file.write(object.content.content)
      file.close
      file_path
    end

  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
ddr-models-3.0.8 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.7 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.6 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.5 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.4 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.3 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.2 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.1 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc6 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc5 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc4 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc3 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc2 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.rc1 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.beta.22 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.beta.21 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.beta.20 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.beta.18 lib/ddr/derivatives/derivative.rb
ddr-models-3.0.0.beta.17 lib/ddr/derivatives/derivative.rb