Class: GoImport::File

Inherits:
Object
  • Object
show all
Includes:
SerializeHelper
Defined in:
lib/go_import/model/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializeHelper

#get_import_rows, #serialize, #serialize_to_file

Constructor Details

#initialize(opt = nil) ⇒ File

Returns a new instance of File.



23
24
25
26
27
28
29
30
# File 'lib/go_import/model/file.rb', line 23

def initialize(opt = nil)
    if !opt.nil?
        serialize_variables.each do |myattr|
            val = opt[myattr[:id]]
            instance_variable_set("@" + myattr[:id].to_s, val) if val != nil
        end
    end
end

Instance Attribute Details

#created_byObject

Returns the value of attribute created_by.



12
13
14
# File 'lib/go_import/model/file.rb', line 12

def created_by
  @created_by
end

#dealObject

Returns the value of attribute deal.



12
13
14
# File 'lib/go_import/model/file.rb', line 12

def deal
  @deal
end

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/go_import/model/file.rb', line 10

def description
  @description
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/go_import/model/file.rb', line 10

def id
  @id
end

#integration_idObject

Returns the value of attribute integration_id.



10
11
12
# File 'lib/go_import/model/file.rb', line 10

def integration_id
  @integration_id
end

#location_in_zip_fileObject

location_in_zip_file is used internally when the file is stored in the zip file that is sent to LIME Go. You should not modify this property



21
22
23
# File 'lib/go_import/model/file.rb', line 21

def location_in_zip_file
  @location_in_zip_file
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/go_import/model/file.rb', line 16

def name
  @name
end

#organizationObject

Returns the value of attribute organization.



12
13
14
# File 'lib/go_import/model/file.rb', line 12

def organization
  @organization
end

#pathObject

Returns the value of attribute path.



14
15
16
# File 'lib/go_import/model/file.rb', line 14

def path
  @path
end

Instance Method Details

#add_to_zip_file(zip_file) ⇒ Object



152
153
154
155
156
# File 'lib/go_import/model/file.rb', line 152

def add_to_zip_file(zip_file)
    @location_in_zip_file = "files/#{SecureRandom.uuid}#{::File.extname(@path).to_s}"

    zip_file.add(@location_in_zip_file, path_for_project)
end

#has_relative_path?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/go_import/model/file.rb', line 77

def has_relative_path?()
    return !@path.match(/[a-zA-Z]{1}\:[\\\/]/)&&Pathname.new(@path).relative?
end

#path_for_projectObject

This is the path to where the file should be accessed from within the project.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/go_import/model/file.rb', line 107

def path_for_project
    if @path.nil? || @path.empty?
        return ""
    end

    # Get the folder where files should be accessed from
    # during the import. If not defined in converter.rb use
    # the current directory
    if defined?(FILES_FOLDER) && !FILES_FOLDER.empty?()
        root_folder = FILES_FOLDER
    else
        root_folder = Dir.pwd
    end

    if has_relative_path?()
        # since this file is stored with a relative file name
        # we should get it from the root folder
        path_for_project = ::File.expand_path(@path, root_folder)
    else
        # the file is stored with an absolute path, if the
        # file cant be access using that path we must change
        # it to a path that is accessible from this computer.
        # The FILES_FOLDER_AT_CUSTOMER constant states what
        # part of the path that should be replaced with the
        # root folder.

        # We assume that the original system used ONE location
        # for all its files. If not, we should change
        # FILES_FOLDER_AT_CUSTOMER to a list of folders.
        if defined?(FILES_FOLDER_AT_CUSTOMER) && !FILES_FOLDER_AT_CUSTOMER.empty?()
            files_folder_at_customer = FILES_FOLDER_AT_CUSTOMER
        else
            files_folder_at_customer = ""
        end

        if files_folder_at_customer.empty?
            path_for_project = @path
        else
            path_for_project = ::File.expand_path(@path.downcase.sub(files_folder_at_customer.downcase, root_folder))
        end
    end

    return path_for_project
end

#serialize_nameObject



32
33
34
# File 'lib/go_import/model/file.rb', line 32

def serialize_name
    "File"
end

#serialize_variablesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/go_import/model/file.rb', line 36

def serialize_variables
    [ :id, :integration_id, :path, :name, :description, :location_in_zip_file ].map {
        |p| {
            :id => p,
            :type => :string
        }
    } +
        [
         { :id => :created_by_reference, :type => :coworker_reference, :element_name => :created_by },
         { :id => :organization_reference, :type => :organization_reference, :element_name => :organization },
         { :id => :deal_reference, :type => :deal_reference, :element_name => :deal }
        ]
end

#validate(ignore_missing_files = false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/go_import/model/file.rb', line 158

def validate(ignore_missing_files = false)
    error = String.new
    warning = String.new

    if @name.nil? || @name.empty?
        error = "#{error}A file must have a name.\n"
    end

    if @path.nil? || @path.empty?
        error = "Path is required for file.\n"
    else
        if !ignore_missing_files && !::File.exists?(path_for_project())
            error = "#{error}Can't find file with name '#{@name}' and original path '#{@path}' at '#{path_for_project()}'."
        end
    end

    if @created_by_reference.nil?
        error = "#{error}Created_by is required for file (#{@name}).\n"
    end

    if @organization_reference.nil? && @deal_reference.nil?
        error = "#{error}The file (#{@name}) must have either an organization or a deal.\n"
    end

    if !@organization_reference.nil? && !@deal_reference.nil?
        error = "#{error}The file (#{@name}) can't be attached to both an organization and a deal."
    end

    return error
end