module Paperdragon::Model::Writer

class Album
  extend Paperdragon::Model::Writer
  processable_writer :image

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

Public Instance Methods

processable_writer(name, attachment_class=Attachment) click to toggle source
# 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