Sha256: d6430b03af67448f64e4b5b9255b9b7e054c099f845dd8c01d54d88606cd4653

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

module Progstr
  module Filer
    class Attachment
      attr_accessor :id, :attribute, :file, :pre_validated

      @@id_generator = ::UUID.new

      class EmptyAttachment < Attachment
        def blank?
          true
        end

        def size
          0
        end

        def path
          ""
        end

        def extension
          ""
        end
      end

      def self.empty
        EmptyAttachment.new
      end

      def self.from_file(attribute, file)
        result = Attachment.new
        result.id = generate_id
        result.attribute = attribute

        result.file = file
        result.pre_validated = false
        result
      end

      def self.generate_id
        uuid = @@id_generator.generate
        uuid.gsub("-", "")
      end

      def size
        file.size
      end

      def path
        (file.original_filename if file.respond_to?(:original_filename)) ||
          (file_path = file.path if file.respond_to?(:path))
      end

      def extension
        from_file = File.extname(path) || ""
        from_file.sub(".", "")
      end

      def self.from_id(attribute, id)
        result = Attachment.new
        result.id = id
        result.attribute = attribute

        result.pre_validated = true
        result
      end

      def blank?
        false
      end

      def url
        if !blank?
          "#{Progstr::Filer.url_prefix}files/data/#{Progstr::Filer.access_key}/#{id}"
        else
          ""
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
progstr-filer-0.8.3 lib/filer/attachment.rb
progstr-filer-0.8.2 lib/filer/attachment.rb