Sha256: c07d1d6039599cb42be48326666f92ebc93e4005c7a4a194dd54882a2e4884e3

Contents?: true

Size: 767 Bytes

Versions: 5

Compression:

Stored size: 767 Bytes

Contents

module FileModel::Helper
  module ClassMethods
    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
file_model-0.0.6 lib/file_model/helper.rb
file_model-0.0.5 lib/file_model/helper.rb
file_model-0.0.4 lib/file_model/helper.rb
file_model-0.0.3 lib/file_model/helper.rb
file_model-0.0.2 lib/file_model/helper.rb