Class: FruitToLime::RootModel

Inherits:
Object
  • Object
show all
Includes:
SerializeHelper
Defined in:
lib/fruit_to_lime/model/rootmodel.rb

Overview

The root model for Go import. This class is the container for everything else.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializeHelper

#get_import_rows, #serialize, #serialize_to_file

Constructor Details

#initializeRootModel

Returns a new instance of RootModel.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 26

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 = []
end

Instance Attribute Details

#coworkersObject

Returns the value of attribute coworkers.



9
10
11
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 9

def coworkers
  @coworkers
end

#dealsObject

Returns the value of attribute deals.



9
10
11
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 9

def deals
  @deals
end

#import_coworkerObject

the import_coworker is a special coworker that is set as responsible for objects that requires a coworker, eg a note.



7
8
9
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 7

def import_coworker
  @import_coworker
end

#notesObject

Returns the value of attribute notes.



9
10
11
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 9

def notes
  @notes
end

#organizationsObject

Returns the value of attribute organizations.



9
10
11
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 9

def organizations
  @organizations
end

#settingsObject

Returns the value of attribute settings.



9
10
11
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 9

def settings
  @settings
end

Instance Method Details

#add_coworker(coworker) ⇒ Object

Adds the specifed coworker object to the model.

Examples:

Add a coworker from a hash

rootmodel.add_coworker({
    :integration_id=>"123",
    :first_name=>"Kalle",
    :last_name=>"Anka",
    :email=>"[email protected]"
})

Add a coworker from a new coworker

coworker = FruitToLime::Coworker.new
coworker.integration_id = "123"
coworker.first_name="Kalle"
coworker.last_name="Anka"
coworker.email = "[email protected]"
rootmodel.add_coworker(coworker)

If you want to keep adding coworkers and dont care about duplicates not being added

begin
   rootmodel.add_coworker(coworker)
rescue FruitToLime::AlreadyAddedError
   puts "Warning: already added coworker"
end

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 62

def add_coworker(coworker)
    @coworkers = [] if @coworkers == nil

    if coworker == nil
        raise "Missing coworker to add!"
    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 FruitToLime::AlreadyAddedError
   puts "Warning: already added deal"
end

Examples:

Add an deal from a hash

rootmodel.add_deal({
    :integration_id => "123",
    :name => "Big deal",
})

Add a deal from a new deal

deal = FruitToLime::Deal.new
deal.integration_id = "123"
deal.name = "Big deal"
rootmodel.add_deal(deal)

If you want to keep adding deals and dont

See Also:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 142

def add_deal(deal)
    @deals = [] if @deals.nil?

    if deal.nil?
        raise "Missing deal to add"
    end

    deal = Deal.new(deal) if !deal.is_a?(Deal)

    if find_deal_by_integration_id(deal.integration_id) != nil
        raise AlreadyAddedError, "Already added a deal with integration_id #{deal.integration_id}"
    end

    @deals.push(deal)

    return deal
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 FruitToLime::AlreadyAddedError
   puts "Warning: already added deal"
end

Examples:

Add an deal from a hash

rootmodel.add_note({
    :integration_id => "123",
    :text => "This is a note",
})

Add a note from a new note

note = FruitToLime::Note.new
note.integration_id = "123"
note.text = "Big deal"
rootmodel.add_note(note)

If you want to keep adding deals and dont

See Also:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 182

def add_note(note)
    @notes = [] if @notes == nil

    if note.nil?
        raise "Missing note to add"
    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 FruitToLime::AlreadyAddedError
   puts "Warning: already added organization"
end

Examples:

Add an organization from a hash

rootmodel.add_organization({
    :integration_id => "123",
    :name => "Beagle Boys",
})

Add an organization from a new organization

organization = FruitToLime::Organization.new
organization.integration_id = "123"
organization.name = "Beagle Boys"
rootmodel.add_organization(organization)

If you want to keep adding organizations and dont

See Also:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 102

def add_organization(organization)
    @organizations = [] if @organizations.nil?

    if organization.nil?
        raise "Missing organization to add"
    end

    organization = Organization.new(organization) if !organization.is_a?(Organization)

    if 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



209
210
211
212
213
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 209

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



238
239
240
241
242
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 238

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



228
229
230
231
232
233
234
235
236
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 228

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



221
222
223
224
225
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 221

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



215
216
217
218
219
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 215

def find_organization_by_integration_id(integration_id)
    return @organizations.find do |organization|
        organization.integration_id == integration_id
    end
end

#sanity_checkObject

Returns a string describing problems with the data. For instance if integration_id for any entity is not unique.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 245

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

    return error.strip
end

#serialize_nameObject



20
21
22
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 20

def serialize_name
    "GoImport"
end

#serialize_variablesObject



10
11
12
13
14
15
16
17
18
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 10

def serialize_variables
    [
     {:id => :settings, :type => :settings},
     {:id => :coworkers, :type => :coworkers},
     {:id => :organizations, :type => :organizations},
     {:id => :deals, :type => :deals},
     {:id => :notes, :type => :notes},
    ]
end

#validateObject



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
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 276

def validate()
    error = String.new

    @organizations.each do |o|
        validation_message = o.validate()

        if !validation_message.empty?
            error = "#{error}\n#{validation_message}"
        end
    end

    @deals.each do |deal|
        validation_message = deal.validate

        if !validation_message.empty?
            error = "#{error}\n#{validation_message}"
        end
    end

    @notes.each do |note|
        validation_message = note.validate

        if !validation_message.empty?
            error = "#{error}\n#{validation_message}"
        end
    end

    return error.strip
end

#with_new_note {|note| ... } ⇒ Object

Yields:

  • (note)


201
202
203
204
205
206
207
# File 'lib/fruit_to_lime/model/rootmodel.rb', line 201

def with_new_note
    @notes = [] if @notes == nil

    note = Note.new
    @notes.push note
    yield note
end