Class: Lims::Core::Persistence::StateGroup

Inherits:
StateList show all
Defined in:
lib/lims-core/persistence/state_group.rb

Overview

A immutable list of ResourceState.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from StateList

#reset_status

Constructor Details

#initialize(persistor, states) ⇒ StateGroup

Returns a new instance of StateGroup.



8
9
10
11
# File 'lib/lims-core/persistence/state_group.rb', line 8

def initialize(persistor, states)
  @persistor = persistor
  super(states)
end

Instance Attribute Details

#persistorObject (readonly)

Returns the value of attribute persistor.



7
8
9
# File 'lib/lims-core/persistence/state_group.rb', line 7

def persistor
  @persistor
end

Instance Method Details

#destroyObject

TODO:

doc

destroy because delete exists already for a Set



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lims-core/persistence/state_group.rb', line 54

def destroy
  return self if size == 0
   # mark each item for deletion
   # so the parents are not saved later.
   # children needs to be deleted NOW to avoid
   # foreign key constraint error.
    each_with_object(StateList.new) do |state, list|
      # don't delete children if they've been deleted already
      next if state.children_saved?
      state.mark_for_deletion
      list.merge(persistor.deletable_children_for(state.resource))
      state.children_saved!
    end.destroy

  
  persistor.bulk_delete(self.select { |s| !s.body_saved? })

    each_with_object(StateList.new) do |state, list|
      next if state.parents_saved?
      list.merge(persistor.deletable_parents_for(state.resource))
      state.parents_saved!
    end.destroy
  each { |state| state.body_saved! }

end

#groupsObject



12
13
14
# File 'lib/lims-core/persistence/state_group.rb', line 12

def groups
  [self]
end

#load(*params) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lims-core/persistence/state_group.rb', line 80

def load(*params)
  to_load = select(&:to_load?)
  all_parents = StateList.new
  attributes_list = []
  persistor.bulk_load(to_load, *params) do |att|
    all_parents.merge(persistor.parents_for_attributes(att))
    attributes_list << att
  end
  all_parents.load(*params)
  attributes_list.map do |att|
    persistor.new_from_attributes(att)
  end
  persistor.load_children(self, *params)
  self
end

#new(&block) ⇒ Object



16
17
18
# File 'lib/lims-core/persistence/state_group.rb', line 16

def new(&block)
  self.class.new(self.persistor, block.call)
end

#saveObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lims-core/persistence/state_group.rb', line 20

def save
  # @todo method for that.
  all_parents = StateList.new
  each do |state|
    next if state.resource == nil or state.to_delete
    state.parents!.andtap do |parents|
      all_parents.merge(parents)
    end
  end

  all_parents.save

  persistor.purge_invalid_object

  # split by status
  group_by(&:save_action).tap do |groups|
    groups[:delete].andtap { |group|  StateGroup.new(persistor, group).destroy }
    groups[:update].andtap { |group| persistor.bulk_update(group) }
    groups[:insert].andtap { |group| persistor.bulk_insert(group) }
  end

  all_children = StateList.new
  each do |state|
    next unless state.resource
    state.body_saved!
    state.children!.andtap do |children|
      all_children.merge(children)
    end
  end
  all_children.save
end