Class: DataMapper::Property::ParseFile

Inherits:
Object
  • Object
show all
Defined in:
lib/property/parse_file.rb

Instance Method Summary collapse

Instance Method Details

#dump(value) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/property/parse_file.rb', line 6

def dump(value)
  case value
  when ::URI
    { "__type" => "File", "name" => File.basename(value.path), "url" => value.to_s }
  when ::Hash
    value
  end
end

#load(value) ⇒ Object



15
16
17
# File 'lib/property/parse_file.rb', line 15

def load(value)
  value && URI(value["url"])
end

#typecast(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/property/parse_file.rb', line 19

def typecast(value)
  if value.respond_to?(:original_filename) && value.respond_to?(:read) && value.respond_to?(:content_type)
    adapter       = model.repository.adapter
    filename      = Digest::SHA256.new.hexdigest value.original_filename
    content       = value.read
    content_type  = value.content_type
    response      = adapter.upload_file(filename, content, content_type)
    URI(response["url"])
  elsif value.is_a?(::Hash)
    URI(value["url"])
  elsif value.is_a?(::String)
    URI(value)
  end
end