Module: Paperdragon::Model::Writer

Defined in:
lib/paperdragon/model.rb

Overview

class Album

extend Paperdragon::Model::Writer
processable_writer :image

Provides Album#image!(file) { |v| v.thumb!(“64x64”) }

Instance Method Summary collapse

Instance Method Details

#processable_writer(name, attachment_class = Attachment) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/paperdragon/model.rb', line 37

def processable_writer(name, attachment_class=Attachment)
  mod = Module.new do # TODO: abstract that into Uber, we use it everywhere.
    define_method "#{name}!" do |file=nil, options={}, &block|
      attachment = attachment_class.new(public_send("#{name}_meta_data"),
                                        options.merge(model: self))

      # run the task block and save the returned new metadata in the model.
      self.public_send("#{name}_meta_data=", attachment.task(*[file], &block))
    end
  end
  include mod
end