Module: FileModel::Helper::ClassMethods

Defined in:
lib/file_model/helper.rb

Instance Method Summary collapse

Instance Method Details

#mount_file(attr_name, file_model_class) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/file_model/helper.rb', line 3

def mount_file attr_name, file_model_class
  attr_name.must.be_a Symbol
  iv_name = :"@_#{attr_name}"

  define_method attr_name do
    unless file_model = instance_variable_get(iv_name)
      file_name = attribute_get attr_name

      file_model = file_model_class.new
      file_model.model = self
      file_model.read file_name

      instance_variable_set iv_name, file_model
    end
    file_model
  end

  define_method :"#{attr_name}=" do |file|
    file_model = send(attr_name)
    file_model.original = file

    file_name = file_model.build_name file_model.original.name
    attribute_set attr_name, file_name
  end
end