Sha256: f74ea9e45de30c49141910dc0693e27bc3bd97560f94dd8fb42f6676979259e7

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'singleton'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/aliasing'

module Proselytism
  module Converters
    class Base
      include ::Singleton
      include Proselytism::Shared
      class_attribute :from, :to, :subclasses

      class Error < Exception; end

      def config
        Proselytism.config
      end

      def destination_file_path(origin, options={})
        if options[:dest]
          options[:dest]
        else
          File.join options[:dir], File.basename(origin).gsub(/\..*$/, options[:folder] ? '' : ".#{options[:to]}")
        end
      end

      #call perform logging duration and potential errors
      def convert(file_path, options={})
        log :debug, "convert #{file_path} to :#{options[:to]}" do
          begin
            perform(file_path, options)
          rescue Error => e
            log :error, e.message
            raise e
          end
        end
      end

      #execute a command and raise error with the command output if it fails
      def execute(command)
        output = `#{command}`
        if $?.exitstatus != 0
          raise self.class::Error, ["#{self.class.name} unable to exec command: #{command}",'--', output,'--'].join("\n")
        end
        $?.exitstatus == 0
      end

      singleton_class.class_eval do

        def inherited_with_registering(subclass)
          self.subclasses ||= []
          self.subclasses << subclass
          inherited_without_registering(subclass)
          subclass
        end

        alias_method_chain :inherited, :registering

        [:from, :to].each do |attr|
          define_method "#{attr}_with_default" do |*formats|
            if formats.length
              self.send "#{attr}=", formats.map(&:to_sym)
            else
              self.send "#{attr}_without_default" || []
            end
          end
          alias_method_chain attr, :default
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proselytism-0.0.1 lib/proselytism/converter.rb