Module: Adrift::Integration::Base

Included in:
ActiveRecord, DataMapper
Defined in:
lib/adrift/integration/base.rb

Overview

Common integration code.

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#attachment(name, options = {}) ⇒ Object

Defines accessor methods for the Attachment, includes InstanceMethods in the model class, and stores the attachment name and options for future reference (see #attachment_definitions).

name and options are the arguments that Attachment::new expects, and receives, with the exception that it accepts an :class option with a custom class to use instead of Attachment. In that case, options[:class] will receive new with name, the model, and options without :class.

The accessor methods are named after the Attachment. For instance, the following code will define avatar and avatar= on the model class that receives #attachment:

attachment :avatar

The writter method (in the example: avatar=) will assign to the Attachment the results of calling FileToAttach::new with its argument. See Attachment#assign for more details.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/adrift/integration/base.rb', line 59

def attachment(name, options={})
  include InstanceMethods

  attachment_definitions[name] = options.dup
  attachment_class = options.delete(:class) || Attachment

  define_method(name) do
    instance_variable = "@#{name}_attachment"
    unless instance_variable_defined?(instance_variable)
      attachment = attachment_class.new(name, self, options)
      instance_variable_set(instance_variable, attachment)
    end
    instance_variable_get(instance_variable)
  end

  define_method("#{name}=") do |file_representation|
    send(name).assign(FileToAttach.new(file_representation))
  end
end

#attachment_definitionsObject

Attachment definitions for the model. Is a Hash where the keys are the names and the values are the options passed to #attachment.



82
83
84
# File 'lib/adrift/integration/base.rb', line 82

def attachment_definitions
  @attachment_definitions ||= {}
end