module Paperdragon::Attachment::InstanceMethods

Attributes

metadata[R]
options[R]

Public Class Methods

new(metadata, options={}) click to toggle source
# File lib/paperdragon/attachment.rb, line 17
def initialize(metadata, options={})
  @metadata = Metadata[metadata]
  @options  = options # to be used in #(re)build_uid for your convenience. # DISCUSS: we pass in the model here - is that what we want?
end

Public Instance Methods

[](style, file=nil) click to toggle source
# File lib/paperdragon/attachment.rb, line 23
def [](style, file=nil) # not sure if i like passing file here, consider this method signature semi-public.
  file_metadata = @metadata[style]

  uid = file_metadata[:uid] || uid_from(style, file)
  self.class.file_class.new(uid, file_metadata)
end
exists?() click to toggle source
# File lib/paperdragon/attachment.rb, line 57
def exists? # should be #uploaded? or #stored?
  # not sure if i like that kind of state here, so consider method semi-public.
  @metadata.populated?
end
rebuild_uid(file, fingerprint=nil) click to toggle source

Per default, paperdragon tries to increment the fingerprint in the file name, identified by the pattern /-\d{10}/ just before the filename extension (.png).

# File lib/paperdragon/attachment.rb, line 46
def rebuild_uid(file, fingerprint=nil) # the signature of this method is to be considered semi-private.
  ext  = ::File.extname(file.uid)
  name = ::File.basename(file.uid, ext)

  if fingerprint and matches = name.match(/-(\d{10})$/)
    return file.uid.sub(matches[1], fingerprint.to_s)
  end

  file.uid.sub(name, "#{name}-#{fingerprint}")
end
task(upload=nil, &block) click to toggle source

DSL method providing the task instance. When called with block, it yields the task and returns the generated metadata.

# File lib/paperdragon/attachment.rb, line 32
def task(upload=nil, &block)
  task = Task.new(self, upload, &block)

  return task unless block_given?
  task.metadata_hash
end
uid_from(*args) click to toggle source

Computes UID when File doesn't have one, yet. Called in initialize.

# File lib/paperdragon/attachment.rb, line 40
def uid_from(*args)
  build_uid(*args)
end

Private Instance Methods

build_uid(style, file) click to toggle source
# File lib/paperdragon/attachment.rb, line 65
def build_uid(style, file)
  # can we use Dragonfly's API here?
  "#{style}-#{Dragonfly::TempObject.new(file).name}"
end