Module: Paperdragon::Attachment::InstanceMethods

Included in:
Paperdragon::Attachment
Defined in:
lib/paperdragon/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#metadataObject (readonly)

TODO: test me.



21
22
23
# File 'lib/paperdragon/attachment.rb', line 21

def 
  @metadata
end

Instance Method Details

#[](style, file = nil) ⇒ Object

not sure if i like passing file here, consider this method signature semi-public.



23
24
25
26
27
28
# 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.
   = @metadata[style]

  uid = [:uid] || uid_from(style, file)
  self.class.file_class.new(uid, )
end

#exists?Boolean

should be #uploaded? or #stored?

Returns:

  • (Boolean)


57
58
59
60
# 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

#initialize(metadata, options = {}) ⇒ Object



17
18
19
20
# File 'lib/paperdragon/attachment.rb', line 17

def initialize(, options={})
  @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

#rebuild_uid(file, fingerprint = nil) ⇒ Object

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



46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

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



32
33
34
35
36
37
# File 'lib/paperdragon/attachment.rb', line 32

def task(upload=nil, &block)
  task = Task.new(self, upload, &block)

  return task unless block_given?
  task.
end

#uid_from(*args) ⇒ Object

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



40
41
42
# File 'lib/paperdragon/attachment.rb', line 40

def uid_from(*args)
  build_uid(*args)
end