Class: Lims::Core::Persistence::ResourceState

Inherits:
Object
  • Object
show all
Includes:
Virtus
Defined in:
lib/lims-core/persistence/resource_state.rb

Overview

Hold different information relateed particular resource within a Session/Persistor. This could be the database id, the current saving state etc …

Instance Method Summary collapse

Constructor Details

#initialize(resource, persistor, id = nil) ⇒ ResourceState

Returns a new instance of ResourceState.



28
29
30
31
32
33
34
35
36
# File 'lib/lims-core/persistence/resource_state.rb', line 28

def initialize(resource, persistor, id=nil)
  @resource=resource
  @persistor=persistor
  self.id=id if id
  @to_delete = false
  update_dirty_key
  reset

end

Instance Method Details

#body_saved!Object



145
146
147
# File 'lib/lims-core/persistence/resource_state.rb', line 145

def body_saved!
  @body_saved = true
end

#body_saved?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/lims-core/persistence/resource_state.rb', line 24

def body_saved?
  @body_saved
end

#childrenObject



138
139
140
141
# File 'lib/lims-core/persistence/resource_state.rb', line 138

def children
  return [] if to_delete
  !@children_saved && @persistor.children_for(resource)
end

#children!Object



142
143
144
# File 'lib/lims-core/persistence/resource_state.rb', line 142

def children!
  children.tap { children_saved! }
end

#children_saved!Object



131
132
133
# File 'lib/lims-core/persistence/resource_state.rb', line 131

def children_saved!
  @children_saved = true
end

#children_saved?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/lims-core/persistence/resource_state.rb', line 20

def children_saved?
  @children_saved
end

#dirty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/lims-core/persistence/resource_state.rb', line 72

def dirty?
 !@body_saved && dirty_key != @last_dirty_key
end

#dirty_keyObject



55
56
57
# File 'lib/lims-core/persistence/resource_state.rb', line 55

def dirty_key
  @persistor.dirty_key_for(resource)
end

#idObject



43
44
45
# File 'lib/lims-core/persistence/resource_state.rb', line 43

def id
  @id
end

#id=(new_id) ⇒ Object

Raises:

  • (RuntimeError)


76
77
78
79
80
81
82
83
# File 'lib/lims-core/persistence/resource_state.rb', line 76

def id=(new_id)
  return  if new_id == @id
  raise RuntimeError, "modifing existing id not allowed. #{self}"   if @id
  @id = new_id
  # link the new id in th id_to_state map
  @persistor.bind_state_to_id(self)
  id
end

#inserted(new_id = nil) ⇒ Object



113
114
115
116
# File 'lib/lims-core/persistence/resource_state.rb', line 113

def inserted(new_id=nil)
  self.id = new_id
  update_dirty_key
end

#mark_for_deletionObject



99
100
101
# File 'lib/lims-core/persistence/resource_state.rb', line 99

def mark_for_deletion
  @to_delete = true
end

#new?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/lims-core/persistence/resource_state.rb', line 63

def new?
  @id == nil
end

#parentsObject



125
126
127
# File 'lib/lims-core/persistence/resource_state.rb', line 125

def parents
  !@parents_saved && @persistor.parents_for(resource)
end

#parents!Object



128
129
130
# File 'lib/lims-core/persistence/resource_state.rb', line 128

def parents!
  parents.tap { parents_saved! }
end

#parents_saved!Object



122
123
124
# File 'lib/lims-core/persistence/resource_state.rb', line 122

def parents_saved!
  @parents_saved = true
end

#parents_saved?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/lims-core/persistence/resource_state.rb', line 135

def parents_saved?
  @parents_saved
end

#persistorObject



51
52
53
# File 'lib/lims-core/persistence/resource_state.rb', line 51

def persistor
  @persistor
end

#resetObject



149
150
151
152
153
# File 'lib/lims-core/persistence/resource_state.rb', line 149

def reset
  @parents_saved = nil
  @children_saved = nil
  @body_saved= nil
end

#resourceObject



47
48
49
# File 'lib/lims-core/persistence/resource_state.rb', line 47

def resource
  @resource
end

#resource=(new_resource) ⇒ Object

Raises:

  • (RuntimeError)


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lims-core/persistence/resource_state.rb', line 85

def resource=(new_resource)
  # Hack
  # for some emtpy resources , resource == nil returns true
  # but nil == resource returns false
  # which is why we test both.
  return if resource == new_resource && new_resource == resource
  raise RuntimeError, "modifing existing resource not allowed. #{self}"   if @resource
  @resource = new_resource
  # link the new resource in th resource_to_state map
  @persistor.bind_state_to_resource(self)
  update_dirty_key
  resource
end

#save_actionObject



104
105
106
107
108
109
110
111
# File 'lib/lims-core/persistence/resource_state.rb', line 104

def save_action
  case
  when @body_saved then nil
  when to_delete && !new? then :delete
  when new? then :insert
  when dirty? then :update
  end
end

#to_deleteObject



38
39
40
# File 'lib/lims-core/persistence/resource_state.rb', line 38

def to_delete
  @to_delete
end

#to_load?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/lims-core/persistence/resource_state.rb', line 67

def to_load?
  @id != nil && @resource == nil
end

#update_dirty_keyObject



59
60
61
# File 'lib/lims-core/persistence/resource_state.rb', line 59

def update_dirty_key
  @last_dirty_key = resource ?  @persistor.dirty_key_for(resource) : nil
end

#updatedObject



118
119
120
# File 'lib/lims-core/persistence/resource_state.rb', line 118

def updated
  update_dirty_key
end