Class: Sprig::Reap::FileAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/sprig/reap/file_attribute.rb

Defined Under Namespace

Classes: LocalFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ FileAttribute

Returns a new instance of FileAttribute.



5
6
7
# File 'lib/sprig/reap/file_attribute.rb', line 5

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/sprig/reap/file_attribute.rb', line 3

def input
  @input
end

Instance Method Details

#existing_locationObject



17
18
19
20
21
22
23
# File 'lib/sprig/reap/file_attribute.rb', line 17

def existing_location
  begin
    input.url if input.url.present? && open(input.url).is_a?(Tempfile)
  rescue
    input.path
  end
end

#fileObject



9
10
11
# File 'lib/sprig/reap/file_attribute.rb', line 9

def file
  local_file if file?
end

#file?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sprig/reap/file_attribute.rb', line 13

def file?
  input.is_a? CarrierWave::Uploader::Base
end

#filenameObject



25
26
27
# File 'lib/sprig/reap/file_attribute.rb', line 25

def filename
  File.basename(existing_location)
end

#local_fileObject



41
42
43
# File 'lib/sprig/reap/file_attribute.rb', line 41

def local_file
  @local_file ||= LocalFile.new(existing_location, target_location)
end

#target_folderObject



29
30
31
32
33
34
35
# File 'lib/sprig/reap/file_attribute.rb', line 29

def target_folder
  @target_folder ||= begin
    path = Rails.root.join('db', 'seeds', Sprig::Reap.target_env, 'files')
    FileUtils.mkdir_p(path)
    path
  end
end

#target_locationObject



37
38
39
# File 'lib/sprig/reap/file_attribute.rb', line 37

def target_location
  target_folder.join(filename)
end