Class: Refile::Attacher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/attacher.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

Presence =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

->(val) { val if val != "" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, record) ⇒ Attacher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Attacher.



9
10
11
12
13
14
# File 'lib/refile/attacher.rb', line 9

def initialize(definition, record)
  @definition = definition
  @record = record
  @errors = []
  @metadata = {}
end

Instance Attribute Details

#definitionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attacher.rb', line 4

def definition
  @definition
end

#errorsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attacher.rb', line 4

def errors
  @errors
end

#recordObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



4
5
6
# File 'lib/refile/attacher.rb', line 4

def record
  @record
end

#removeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/refile/attacher.rb', line 5

def remove
  @remove
end

Instance Method Details

#basenameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
52
53
54
# File 'lib/refile/attacher.rb', line 48

def basename
  if filename and extension
    ::File.basename(filename, "." << extension)
  else
    filename
  end
end

#cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/refile/attacher.rb', line 20

def cache
  @definition.cache
end

#cache!(uploadable) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/refile/attacher.rb', line 93

def cache!(uploadable)
  @metadata = {
    size: uploadable.size,
    content_type: Refile.extract_content_type(uploadable),
    filename: Refile.extract_filename(uploadable)
  }
  if valid?
    @metadata[:id] = cache.upload(uploadable).id
    
  elsif @definition.raise_errors?
    raise Refile::Invalid, @errors.join(", ")
  end
end

#cache_idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
# File 'lib/refile/attacher.rb', line 44

def cache_id
  Presence[@metadata[:id]]
end

#content_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/refile/attacher.rb', line 40

def content_type
  Presence[@metadata[:content_type] || read(:content_type)]
end

#dataObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
# File 'lib/refile/attacher.rb', line 155

def data
  @metadata if valid?
end

#delete!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



141
142
143
144
145
# File 'lib/refile/attacher.rb', line 141

def delete!
  cache.delete(cache_id) if cache_id
  store.delete(id) if id
  @metadata = {}
end

#download(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/refile/attacher.rb', line 107

def download(url)
  unless url.to_s.empty?
    download = Refile::Download.new(url)
    @metadata = {
      size: download.size,
      filename: download.original_filename,
      content_type: download.content_type
    }
    if valid?
      @metadata[:id] = cache.upload(download.io).id
      
    elsif @definition.raise_errors?
      raise Refile::Invalid, @errors.join(", ")
    end
  end
rescue Refile::Error
  @errors = [:download_failed]
  raise if @definition.raise_errors?
end

#extensionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
# File 'lib/refile/attacher.rb', line 56

def extension
  if filename
    Presence[::File.extname(filename).sub(/^\./, "")]
  elsif content_type
    type = MIME::Types[content_type][0]
    type.extensions[0] if type
  end
end

#filenameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
# File 'lib/refile/attacher.rb', line 36

def filename
  Presence[@metadata[:filename] || read(:filename)]
end

#getObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
72
73
# File 'lib/refile/attacher.rb', line 65

def get
  if remove?
    nil
  elsif cache_id
    cache.get(cache_id)
  elsif id
    store.get(id)
  end
end

#idObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/refile/attacher.rb', line 28

def id
  Presence[read(:id, true)]
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/refile/attacher.rb', line 16

def name
  @definition.name
end

#present?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


151
152
153
# File 'lib/refile/attacher.rb', line 151

def present?
  not @metadata.empty?
end

#remove?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


147
148
149
# File 'lib/refile/attacher.rb', line 147

def remove?
  remove and remove != "" and remove !~ /\A0|false$\z/
end

#retrieve!(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
90
91
# File 'lib/refile/attacher.rb', line 84

def retrieve!(value)
  if value.is_a?(String)
    @metadata = Refile.parse_json(value, symbolize_names: true) || {}
  elsif value.is_a?(Hash)
    @metadata = value
  end
   if cache_id
end

#set(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



75
76
77
78
79
80
81
82
# File 'lib/refile/attacher.rb', line 75

def set(value)
  self.remove = false
  case value
    when nil then self.remove = true
    when String, Hash then retrieve!(value)
    else cache!(value)
  end
end

#sizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/refile/attacher.rb', line 32

def size
  Presence[@metadata[:size] || read(:size)]
end

#storeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'lib/refile/attacher.rb', line 24

def store
  @definition.store
end

#store!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/refile/attacher.rb', line 127

def store!
  if remove?
    delete!
    write(:id, nil, true)
    
  elsif cache_id
    file = store.upload(get)
    delete!
    write(:id, file.id, true)
    
  end
  @metadata = {}
end

#valid?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/refile/attacher.rb', line 159

def valid?
  @errors = @definition.validate(self)
  @errors.empty?
end