Class: GoImport::RootModel
- Inherits:
-
Object
- Object
- GoImport::RootModel
- Includes:
- SerializeHelper
- Defined in:
- lib/go_import/model/rootmodel.rb
Overview
The root model for Go import. This class is the container for everything else.
Instance Attribute Summary collapse
-
#coworkers ⇒ Object
Returns the value of attribute coworkers.
-
#deals ⇒ Object
Returns the value of attribute deals.
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
-
#import_coworker ⇒ Object
the import_coworker is a special coworker that is set as responsible for objects that requires a coworker, eg a note.
-
#notes ⇒ Object
Returns the value of attribute notes.
-
#organizations ⇒ Object
Returns the value of attribute organizations.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
-
#add_coworker(coworker) ⇒ Object
Adds the specifed coworker object to the model.
-
#add_deal(deal) ⇒ Object
Adds the specifed deal object to the model.
- #add_file(file) ⇒ Object
- #add_link(link) ⇒ Object
-
#add_note(note) ⇒ Object
Adds the specifed note object to the model.
-
#add_organization(organization) ⇒ Object
Adds the specifed organization object to the model.
- #find_coworker_by_integration_id(integration_id) ⇒ Object
- #find_deal_by_integration_id(integration_id) ⇒ Object
-
#find_deals_for_organization(organization) ⇒ Object
find deals for organization using Organization#integration_id.
- #find_note_by_integration_id(integration_id) ⇒ Object
- #find_organization_by_integration_id(integration_id) ⇒ Object
- #find_person_by_integration_id(integration_id) ⇒ Object
-
#initialize ⇒ RootModel
constructor
A new instance of RootModel.
-
#sanity_check ⇒ Object
Returns a string describing problems with the data.
- #serialize_name ⇒ Object
- #serialize_variables ⇒ Object
- #validate(ignore_missing_files = false) ⇒ Object
Methods included from SerializeHelper
#get_import_rows, #serialize, #serialize_to_file
Constructor Details
#initialize ⇒ RootModel
Returns a new instance of RootModel.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/go_import/model/rootmodel.rb', line 34 def initialize() @settings = Settings.new @organizations = [] @coworkers = [] @import_coworker = Coworker.new @import_coworker.integration_id = "import" @import_coworker.first_name = "Import" @coworkers.push @import_coworker @deals = [] @notes = [] @documents = Documents.new end |
Instance Attribute Details
#coworkers ⇒ Object
Returns the value of attribute coworkers.
13 14 15 |
# File 'lib/go_import/model/rootmodel.rb', line 13 def coworkers @coworkers end |
#deals ⇒ Object
Returns the value of attribute deals.
13 14 15 |
# File 'lib/go_import/model/rootmodel.rb', line 13 def deals @deals end |
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
15 16 17 |
# File 'lib/go_import/model/rootmodel.rb', line 15 def documents @documents end |
#import_coworker ⇒ Object
the import_coworker is a special coworker that is set as responsible for objects that requires a coworker, eg a note.
11 12 13 |
# File 'lib/go_import/model/rootmodel.rb', line 11 def import_coworker @import_coworker end |
#notes ⇒ Object
Returns the value of attribute notes.
13 14 15 |
# File 'lib/go_import/model/rootmodel.rb', line 13 def notes @notes end |
#organizations ⇒ Object
Returns the value of attribute organizations.
13 14 15 |
# File 'lib/go_import/model/rootmodel.rb', line 13 def organizations @organizations end |
#settings ⇒ Object
Returns the value of attribute settings.
13 14 15 |
# File 'lib/go_import/model/rootmodel.rb', line 13 def settings @settings end |
Instance Method Details
#add_coworker(coworker) ⇒ Object
Adds the specifed coworker object to the model.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/go_import/model/rootmodel.rb', line 71 def add_coworker(coworker) @coworkers = [] if @coworkers == nil if coworker.nil? return nil end coworker = Coworker.new(coworker) if !coworker.is_a?(Coworker) if find_coworker_by_integration_id(coworker.integration_id) != nil raise AlreadyAddedError, "Already added a coworker with integration_id #{coworker.integration_id}" end @coworkers.push(coworker) return coworker end |
#add_deal(deal) ⇒ Object
Adds the specifed deal object to the model. care about duplicates not being added. Your model might not be saved due to duplicate integration_ids.
begin
rootmodel.add_deal(deal)
rescue GoImport::AlreadyAddedError
puts "Warning: already added deal"
end
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/go_import/model/rootmodel.rb', line 152 def add_deal(deal) @deals = [] if @deals.nil? if deal.nil? return nil end deal = Deal.new(deal) if !deal.is_a?(Deal) if (!deal.integration_id.nil? && deal.integration_id.length > 0) && find_deal_by_integration_id(deal.integration_id) != nil raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}" end if deal.responsible_coworker.nil? deal.responsible_coworker = @import_coworker end @deals.push(deal) return deal end |
#add_file(file) ⇒ Object
222 223 224 225 226 |
# File 'lib/go_import/model/rootmodel.rb', line 222 def add_file(file) @documents = Documents.new if @documents == nil return @documents.add_file(file) end |
#add_link(link) ⇒ Object
216 217 218 219 220 |
# File 'lib/go_import/model/rootmodel.rb', line 216 def add_link(link) @documents = Documents.new if @documents == nil return @documents.add_link(link) end |
#add_note(note) ⇒ Object
Adds the specifed note object to the model. care about duplicates not being added. Your model might not be saved due to duplicate integration_ids.
begin
rootmodel.add_deal(deal)
rescue GoImport::AlreadyAddedError
puts "Warning: already added deal"
end
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/go_import/model/rootmodel.rb', line 197 def add_note(note) @notes = [] if @notes == nil if note.nil? return nil end note = Note.new(note) if !note.is_a?(Note) if (!note.integration_id.nil? && note.integration_id.length > 0) && find_note_by_integration_id(note.integration_id) != nil raise AlreadyAddedError, "Already added a note with integration_id #{note.integration_id}" end @notes.push(note) return note end |
#add_organization(organization) ⇒ Object
Adds the specifed organization object to the model. care about duplicates not being added. Your model might not be saved due to duplicate integration_ids.
begin
rootmodel.add_organization(organization)
rescue GoImport::AlreadyAddedError
puts "Warning: already added organization"
end
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/go_import/model/rootmodel.rb', line 111 def add_organization(organization) @organizations = [] if @organizations.nil? if organization.nil? return nil end organization = Organization.new(organization) if !organization.is_a?(Organization) if (!organization.integration_id.nil? && organization.integration_id.length > 0) && find_organization_by_integration_id(organization.integration_id) != nil raise AlreadyAddedError, "Already added an organization with integration_id #{organization.integration_id}" end @organizations.push(organization) return organization end |
#find_coworker_by_integration_id(integration_id) ⇒ Object
228 229 230 231 232 |
# File 'lib/go_import/model/rootmodel.rb', line 228 def find_coworker_by_integration_id(integration_id) return @coworkers.find do |coworker| coworker.integration_id == integration_id end end |
#find_deal_by_integration_id(integration_id) ⇒ Object
265 266 267 268 269 |
# File 'lib/go_import/model/rootmodel.rb', line 265 def find_deal_by_integration_id(integration_id) return @deals.find do |deal| deal.integration_id == integration_id end end |
#find_deals_for_organization(organization) ⇒ Object
find deals for organization using Organization#integration_id
255 256 257 258 259 260 261 262 263 |
# File 'lib/go_import/model/rootmodel.rb', line 255 def find_deals_for_organization(organization) deals = [] deals = @deals.select do |deal| !deal.customer.nil? && deal.customer.integration_id == organization.integration_id end return deals end |
#find_note_by_integration_id(integration_id) ⇒ Object
248 249 250 251 252 |
# File 'lib/go_import/model/rootmodel.rb', line 248 def find_note_by_integration_id(integration_id) return @notes.find do |note| note.integration_id == integration_id end end |
#find_organization_by_integration_id(integration_id) ⇒ Object
234 235 236 237 238 |
# File 'lib/go_import/model/rootmodel.rb', line 234 def find_organization_by_integration_id(integration_id) return @organizations.find do |organization| organization.integration_id == integration_id end end |
#find_person_by_integration_id(integration_id) ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/go_import/model/rootmodel.rb', line 240 def find_person_by_integration_id(integration_id) return nil if @organizations.nil? @organizations.each do |organization| person = organization.find_employee_by_integration_id(integration_id) return person if person end end |
#sanity_check ⇒ Object
Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/go_import/model/rootmodel.rb', line 272 def sanity_check error = String.new dups = get_integration_id_duplicates(with_non_empty_integration_id(@coworkers)) dups_error_items = (dups.collect{|coworker| coworker.integration_id}).compact if dups.length > 0 error = "#{error}\nDuplicate coworker integration_id: #{dups_error_items.join(", ")}." end dups = get_integration_id_duplicates(with_non_empty_integration_id(@organizations)) dups_error_items = (dups.collect{|org| org.integration_id}).compact if dups.length > 0 error = "#{error}\nDuplicate organization integration_id: #{dups_error_items.join(", ")}." end dups = get_integration_id_duplicates(with_non_empty_integration_id(@deals)) dups_error_items = (dups.collect{|deal| deal.integration_id}).compact if dups_error_items.length > 0 error = "#{error}\nDuplicate deal integration_id: #{dups_error_items.join(", ")}." end persons = @organizations.collect{|o| o.employees}.flatten.compact dups = get_integration_id_duplicates(with_non_empty_integration_id(persons)) dups_error_items = (dups.collect{|person| person.integration_id}).compact if dups_error_items.length > 0 error = "#{error}\nDuplicate person integration_id: #{dups_error_items.join(", ")}." end dups = get_integration_id_duplicates(with_non_empty_integration_id(@documents.links)) dups_error_items = (dups.collect{|l| l.integration_id}).compact if dups_error_items.length > 0 error = "#{error}\nDuplicate link integration_id: #{dups_error_items.join(", ")}." end return error.strip end |
#serialize_name ⇒ Object
28 29 30 |
# File 'lib/go_import/model/rootmodel.rb', line 28 def serialize_name "GoImport" end |
#serialize_variables ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/go_import/model/rootmodel.rb', line 17 def serialize_variables [ {:id => :settings, :type => :settings}, {:id => :coworkers, :type => :coworkers}, {:id => :organizations, :type => :organizations}, {:id => :deals, :type => :deals}, {:id => :notes, :type => :notes}, {:id => :documents, :type => :documents}, ] end |
#validate(ignore_missing_files = false) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/go_import/model/rootmodel.rb', line 309 def validate(ignore_missing_files = false) error = String.new @organizations.each do |o| = o.validate() if !.empty? error = "#{error}\n#{validation_message}" end end @deals.each do |deal| = deal.validate if !.empty? error = "#{error}\n#{validation_message}" end end @notes.each do |note| = note.validate if !.empty? error = "#{error}\n#{validation_message}" end end @documents.links.each do |link| = link.validate if !.empty? error = "#{error}\n#{validation_message}" end end @documents.files.each do |file| = file.validate(ignore_missing_files) if !.empty? error = "#{error}\n#{validation_message}" end end return error.strip end |