Class: GoImport::File

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

Constant Summary collapse

DEFAULT_MAX_FILE_SIZE =

100 Mb

100000000

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.



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

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.



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

def created_by
  @created_by
end

#dealObject

Returns the value of attribute deal.



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

def deal
  @deal
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#integration_idObject

Returns the value of attribute integration_id.



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

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



23
24
25
# File 'lib/go_import/model/file.rb', line 23

def location_in_zip_file
  @location_in_zip_file
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/go_import/model/file.rb', line 18

def name
  @name
end

#organizationObject

Returns the value of attribute organization.



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

def organization
  @organization
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#add_to_zip_file(zip_file) ⇒ Object



156
157
158
# File 'lib/go_import/model/file.rb', line 156

def add_to_zip_file(zip_file)
    zip_file.add(@location_in_zip_file, path_for_project)
end

#has_relative_path?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/go_import/model/file.rb', line 81

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.



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
151
152
153
154
# File 'lib/go_import/model/file.rb', line 111

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



34
35
36
# File 'lib/go_import/model/file.rb', line 34

def serialize_name
    "File"
end

#serialize_variablesObject



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

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_invalid_files = false, max_file_size = DEFAULT_MAX_FILE_SIZE) ⇒ Object



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
188
189
190
191
# File 'lib/go_import/model/file.rb', line 160

def validate(ignore_invalid_files = false, max_file_size = DEFAULT_MAX_FILE_SIZE)
    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"
    elsif !ignore_invalid_files
        if !::File.exists?(path_for_project())
            error = "#{error}Can't find file with name '#{@name}' and original path '#{@path}' at '#{path_for_project()}'."
        elsif ::File.exists?(path_for_project) && ::File.size(path_for_project()) > max_file_size
            error = "#{error}File '#{@name}' is bigger than #{max_file_size} bytes."
        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