Class: SledImage

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData, Shared::Notes, Shared::Tags
Defined in:
app/models/sled_image.rb

Overview

A SledImage contains the metadata to parse one image into many depictions.

  • if ‘metadata` is provided without a `collection_object` stub then Specimen is assumed

A section is verbatim from sled

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#_column_totalObject (private)

Internal processing



36
37
38
# File 'app/models/sled_image.rb', line 36

def _column_total
  @_column_total
end

#_first_identifierObject (private)

Internal processing



36
37
38
# File 'app/models/sled_image.rb', line 36

def _first_identifier
  @_first_identifier
end

#_identifier_matrixArray of Arrays (private)

Returns:

  • (Array of Arrays)


39
40
41
# File 'app/models/sled_image.rb', line 39

def _identifier_matrix
  @_identifier_matrix
end

#_row_totalObject (private)

Internal processing



36
37
38
# File 'app/models/sled_image.rb', line 36

def _row_total
  @_row_total
end

#collection_object_paramsObject

Stubs coming from UI



22
23
24
# File 'app/models/sled_image.rb', line 22

def collection_object_params
  @collection_object_params
end

#depiction_paramsObject

Stubs coming from UI



25
26
27
# File 'app/models/sled_image.rb', line 25

def depiction_params
  @depiction_params
end

#metadataJSON

Returns position coordinates.

Returns:

  • (JSON)

    position coordinates



15
16
17
18
19
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
51
52
53
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
155
156
157
158
159
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/models/sled_image.rb', line 15

class SledImage < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::IsData

  # Stubs coming from UI
  attr_accessor :collection_object_params # note, tag, deterimination

  # Stubs coming from UI
  attr_accessor :depiction_params # for is_metadata_depiction

  # @param step_identifier [String]
  #  defaults to 'column', used only to compute object identifiers
  attr_accessor :step_identifier_on #row, column

  # If assigned to 'nuke', and .destroy, then will
  # additionall destroy all related collection objects
  attr_accessor :nuke

  # Internal processing
  attr_accessor :_first_identifier, :_row_total, :_column_total

  # A nil value occurs when `!section.metadata.nil?`
  attr_accessor :_identifier_matrix

  belongs_to :image
  has_many :depictions, through: :image
  has_many :collection_objects, through: :depictions, source: :depiction_object, source_type: 'CollectionObject'

  before_destroy :destroy_related

  validates_presence_of :image
  validates_uniqueness_of :image_id

  after_save :set_cached, unless: Proc.new {|n| n.&.empty? || errors.any? } # must be before process
  after_save :process, unless: Proc.new { |n| n.&.empty? }

  # zero beers
  def total(direction = 'row')
    i = nil
    .each do |o|
      i = o[direction] if i.nil? || (!i.nil? && o[direction] > i)
    end
    i
  end

  def metadata=(value)
    if value.kind_of? Array
      write_attribute(:metadata, value)
    elsif value.kind_of? String
      write_attribute(:metadata, JSON.parse(value))
    elsif value.nil?
      write_attribute(:metadata, [])
    else
      raise
    end
  end

  def summary
    m = []
    .each do |s|
      c = s['column'].to_i
      r = s['row'].to_i
      m[r] ||= []
      d = depiction_for(s)
      m[r][c] = {
        depiction_id: d&.id,
        collection_object_id: d&.depiction_object_id,
        identifier: d&.depiction_object&.identifiers&.first&.cached
      }
    end
    m
  end

  def depiction_for(section)
    x,y = section['column'], section['row']
    depictions.where(sled_image_x_position: x, sled_image_y_position: y).first
  end

  def step_identifier_on
    @step_identifier_on ||= 'column'
  end

  private

  def destroy_related
    if nuke == 'nuke'
      collection_objects.reload.each do |d|
        d.destroy
      end
    end

    depictions.each do |d|
      d.destroy
    end
    true
  end

  def new_collection_object
    CollectionObject.new(collection_object_params)
  end

  def _first_identifier
    @_first_identifier ||= new_collection_object.identifiers&.first&.identifier&.to_i
  end

  def _row_total
    @_row_total ||= total('row')
  end

  def _column_total
    @_column_total ||= total('column')
  end

  # @return [Array of Arrays]
  def _identifier_matrix
    @_identifier_matrix ||= get_identifier_matrix
  end

  def identity_matrix
    i = []
    .each do |s|
      r = s['row'].to_i
      c = s['column'].to_i
      i[r] ||= []
      i[r][c] = s['metadata'].blank? ? 0 : 1
    end
    i
  end

  def increment_matrix
    i = []
    j = identity_matrix
    k = 0
    case step_identifier_on
    when 'row'
      (0.._row_total).each do |r|
        (0.._column_total).each do |c|
          k += j[r][c]
          i[r] ||= []
          i[r][c] = (j[r][c] == 1 ? 0 : k)
        end
      end

    when 'column'
      (0.._column_total).each do |c|
        (0.._row_total).each do |r|
          k += j[r][c]
          i[r] ||= []
          i[r][c] = (j[r][c] == 1 ? 0 : k)
        end
      end
    end
    i
  end

  def get_identifier_matrix
    i = increment_matrix

    m = []

    .each do |s|
      r = s['row'].to_i
      c = s['column'].to_i
      m[r] ||= []
      m[r][c] = nil if !s['metadata'].blank?
      next if !s['metadata'].blank? || _first_identifier.nil?

      inc = r + c - i[r][c] + _first_identifier
      v = nil
      case step_identifier_on
      when 'row'
        v = (r * _column_total) + inc
      when 'column'
        v = (c * _row_total) + inc
      end

      m[r][c] = v
    end
    @_identifier_matrix = m
  end

  def identifier_for(section)
    _identifier_matrix[section['row'].to_i][section['column'].to_i]
  end

  def process
    depictions.any? ?  syncronize : create_objects
    true
  end

  def create_objects
    return true unless !collection_object_params.nil?
    begin
      .each do |i|
        next unless i['metadata'].blank?
        p = collection_object_params.merge(
          depictions_attributes: [
            {
              sled_image_id: id,
              image_id: image_id,
              svg_clip: svg_clip(i),
              svg_view_box: svg_view_box(i),
              sled_image_x_position: i['column'],
              sled_image_y_position: i['row'],
              is_metadata_depiction: 
            }
          ]
        )

        j = identifier_for(i)

        # Check to see if object exists
        if j && k = Identifier::Local::CatalogNumber.find_by(p[:identifiers_attributes].first.merge(identifier: j, identifier_object_type: 'CollectionObject'))
          # Remove the identifier attributes, identifier exists
          p.delete :identifiers_attributes
          p.delete :tags_attributes
          p.delete :notes_attributes
          p.delete :taxon_determinations_attributes
          p.delete :data_attributes_attributes
          p.delete :total

          k.identifier_object.update!(p)
        else
          c = CollectionObject.new(p)

          if c.identifiers.first
            c.identifiers.first.identifier = identifier_for(i)
          end
          c.save!
        end

      end
    rescue ActiveRecord::RecordInvalid => e
      errors.add(:base, e.message)
      raise
    end
    true
  end

  def syncronize
    if  == []
      process if !&.empty?
    else
      # At this point we only allow coordinate updates
      .each do |i|
        next if !i['metadata'].nil?
        if d = depiction_for(i)
          d.update_columns(
            svg_clip: svg_clip(i),
            svg_view_box: svg_view_box(i),
          )
        end
      end
    end
    true
  end

  def set_cached
    update_columns(
      cached_total_collection_objects: ,
      cached_total_rows: _row_total,
      cached_total_columns: _column_total
    )
  end

  def 
    .count
  end

  # x1, y1, x2, y2
  def coordinates(section)
    [
      section['upperCorner']['x'].to_f, section['upperCorner']['y'].to_f,
      section['lowerCorner']['x'].to_f, section['lowerCorner']['y'].to_f]
  end


  def svg_view_box(section)
    view_box_values(section).join(' ')
  end

  # @return top left x, top left y, height, width
  def view_box_values(section)
    x1, y1, x2, y2 = coordinates(section)
    [x1, y1, x2 - x1, y2 - y1]
  end

  def svg_clip(section)
    x, y, h, w = view_box_values(section)
    "<rect x=\"#{x}\" y=\"#{y}\" width=\"#{h}\" height=\"#{w}\" />"
  end

  private

  def 
    if depiction_params && !depiction_params.empty?
      a = depiction_params[:is_metadata_depiction]
      ((a == 'true') || (a == true)) ? true : false
    else
      false
    end
  end

end

#nukeObject

If assigned to ‘nuke’, and .destroy, then will additionall destroy all related collection objects



33
34
35
# File 'app/models/sled_image.rb', line 33

def nuke
  @nuke
end

#object_layoutJSON

Returns ROI metadata per metadata type, expandable to all sled sections.

Returns:

  • (JSON)

    ROI metadata per metadata type, expandable to all sled sections



15
16
17
18
19
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
51
52
53
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
155
156
157
158
159
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/models/sled_image.rb', line 15

class SledImage < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::IsData

  # Stubs coming from UI
  attr_accessor :collection_object_params # note, tag, deterimination

  # Stubs coming from UI
  attr_accessor :depiction_params # for is_metadata_depiction

  # @param step_identifier [String]
  #  defaults to 'column', used only to compute object identifiers
  attr_accessor :step_identifier_on #row, column

  # If assigned to 'nuke', and .destroy, then will
  # additionall destroy all related collection objects
  attr_accessor :nuke

  # Internal processing
  attr_accessor :_first_identifier, :_row_total, :_column_total

  # A nil value occurs when `!section.metadata.nil?`
  attr_accessor :_identifier_matrix

  belongs_to :image
  has_many :depictions, through: :image
  has_many :collection_objects, through: :depictions, source: :depiction_object, source_type: 'CollectionObject'

  before_destroy :destroy_related

  validates_presence_of :image
  validates_uniqueness_of :image_id

  after_save :set_cached, unless: Proc.new {|n| n.&.empty? || errors.any? } # must be before process
  after_save :process, unless: Proc.new { |n| n.&.empty? }

  # zero beers
  def total(direction = 'row')
    i = nil
    .each do |o|
      i = o[direction] if i.nil? || (!i.nil? && o[direction] > i)
    end
    i
  end

  def metadata=(value)
    if value.kind_of? Array
      write_attribute(:metadata, value)
    elsif value.kind_of? String
      write_attribute(:metadata, JSON.parse(value))
    elsif value.nil?
      write_attribute(:metadata, [])
    else
      raise
    end
  end

  def summary
    m = []
    .each do |s|
      c = s['column'].to_i
      r = s['row'].to_i
      m[r] ||= []
      d = depiction_for(s)
      m[r][c] = {
        depiction_id: d&.id,
        collection_object_id: d&.depiction_object_id,
        identifier: d&.depiction_object&.identifiers&.first&.cached
      }
    end
    m
  end

  def depiction_for(section)
    x,y = section['column'], section['row']
    depictions.where(sled_image_x_position: x, sled_image_y_position: y).first
  end

  def step_identifier_on
    @step_identifier_on ||= 'column'
  end

  private

  def destroy_related
    if nuke == 'nuke'
      collection_objects.reload.each do |d|
        d.destroy
      end
    end

    depictions.each do |d|
      d.destroy
    end
    true
  end

  def new_collection_object
    CollectionObject.new(collection_object_params)
  end

  def _first_identifier
    @_first_identifier ||= new_collection_object.identifiers&.first&.identifier&.to_i
  end

  def _row_total
    @_row_total ||= total('row')
  end

  def _column_total
    @_column_total ||= total('column')
  end

  # @return [Array of Arrays]
  def _identifier_matrix
    @_identifier_matrix ||= get_identifier_matrix
  end

  def identity_matrix
    i = []
    .each do |s|
      r = s['row'].to_i
      c = s['column'].to_i
      i[r] ||= []
      i[r][c] = s['metadata'].blank? ? 0 : 1
    end
    i
  end

  def increment_matrix
    i = []
    j = identity_matrix
    k = 0
    case step_identifier_on
    when 'row'
      (0.._row_total).each do |r|
        (0.._column_total).each do |c|
          k += j[r][c]
          i[r] ||= []
          i[r][c] = (j[r][c] == 1 ? 0 : k)
        end
      end

    when 'column'
      (0.._column_total).each do |c|
        (0.._row_total).each do |r|
          k += j[r][c]
          i[r] ||= []
          i[r][c] = (j[r][c] == 1 ? 0 : k)
        end
      end
    end
    i
  end

  def get_identifier_matrix
    i = increment_matrix

    m = []

    .each do |s|
      r = s['row'].to_i
      c = s['column'].to_i
      m[r] ||= []
      m[r][c] = nil if !s['metadata'].blank?
      next if !s['metadata'].blank? || _first_identifier.nil?

      inc = r + c - i[r][c] + _first_identifier
      v = nil
      case step_identifier_on
      when 'row'
        v = (r * _column_total) + inc
      when 'column'
        v = (c * _row_total) + inc
      end

      m[r][c] = v
    end
    @_identifier_matrix = m
  end

  def identifier_for(section)
    _identifier_matrix[section['row'].to_i][section['column'].to_i]
  end

  def process
    depictions.any? ?  syncronize : create_objects
    true
  end

  def create_objects
    return true unless !collection_object_params.nil?
    begin
      .each do |i|
        next unless i['metadata'].blank?
        p = collection_object_params.merge(
          depictions_attributes: [
            {
              sled_image_id: id,
              image_id: image_id,
              svg_clip: svg_clip(i),
              svg_view_box: svg_view_box(i),
              sled_image_x_position: i['column'],
              sled_image_y_position: i['row'],
              is_metadata_depiction: 
            }
          ]
        )

        j = identifier_for(i)

        # Check to see if object exists
        if j && k = Identifier::Local::CatalogNumber.find_by(p[:identifiers_attributes].first.merge(identifier: j, identifier_object_type: 'CollectionObject'))
          # Remove the identifier attributes, identifier exists
          p.delete :identifiers_attributes
          p.delete :tags_attributes
          p.delete :notes_attributes
          p.delete :taxon_determinations_attributes
          p.delete :data_attributes_attributes
          p.delete :total

          k.identifier_object.update!(p)
        else
          c = CollectionObject.new(p)

          if c.identifiers.first
            c.identifiers.first.identifier = identifier_for(i)
          end
          c.save!
        end

      end
    rescue ActiveRecord::RecordInvalid => e
      errors.add(:base, e.message)
      raise
    end
    true
  end

  def syncronize
    if  == []
      process if !&.empty?
    else
      # At this point we only allow coordinate updates
      .each do |i|
        next if !i['metadata'].nil?
        if d = depiction_for(i)
          d.update_columns(
            svg_clip: svg_clip(i),
            svg_view_box: svg_view_box(i),
          )
        end
      end
    end
    true
  end

  def set_cached
    update_columns(
      cached_total_collection_objects: ,
      cached_total_rows: _row_total,
      cached_total_columns: _column_total
    )
  end

  def 
    .count
  end

  # x1, y1, x2, y2
  def coordinates(section)
    [
      section['upperCorner']['x'].to_f, section['upperCorner']['y'].to_f,
      section['lowerCorner']['x'].to_f, section['lowerCorner']['y'].to_f]
  end


  def svg_view_box(section)
    view_box_values(section).join(' ')
  end

  # @return top left x, top left y, height, width
  def view_box_values(section)
    x1, y1, x2, y2 = coordinates(section)
    [x1, y1, x2 - x1, y2 - y1]
  end

  def svg_clip(section)
    x, y, h, w = view_box_values(section)
    "<rect x=\"#{x}\" y=\"#{y}\" width=\"#{h}\" height=\"#{w}\" />"
  end

  private

  def 
    if depiction_params && !depiction_params.empty?
      a = depiction_params[:is_metadata_depiction]
      ((a == 'true') || (a == true)) ? true : false
    else
      false
    end
  end

end

#step_identifier_onObject

Parameters:

  • step_identifier (String)

    defaults to ‘column’, used only to compute object identifiers



29
30
31
# File 'app/models/sled_image.rb', line 29

def step_identifier_on
  @step_identifier_on
end

Instance Method Details

#coordinates(section) ⇒ Object (private)

x1, y1, x2, y2



287
288
289
290
291
# File 'app/models/sled_image.rb', line 287

def coordinates(section)
  [
    section['upperCorner']['x'].to_f, section['upperCorner']['y'].to_f,
    section['lowerCorner']['x'].to_f, section['lowerCorner']['y'].to_f]
end

#create_objectsObject (private)



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/models/sled_image.rb', line 207

def create_objects
  return true unless !collection_object_params.nil?
  begin
    .each do |i|
      next unless i['metadata'].blank?
      p = collection_object_params.merge(
        depictions_attributes: [
          {
            sled_image_id: id,
            image_id: image_id,
            svg_clip: svg_clip(i),
            svg_view_box: svg_view_box(i),
            sled_image_x_position: i['column'],
            sled_image_y_position: i['row'],
            is_metadata_depiction: 
          }
        ]
      )

      j = identifier_for(i)

      # Check to see if object exists
      if j && k = Identifier::Local::CatalogNumber.find_by(p[:identifiers_attributes].first.merge(identifier: j, identifier_object_type: 'CollectionObject'))
        # Remove the identifier attributes, identifier exists
        p.delete :identifiers_attributes
        p.delete :tags_attributes
        p.delete :notes_attributes
        p.delete :taxon_determinations_attributes
        p.delete :data_attributes_attributes
        p.delete :total

        k.identifier_object.update!(p)
      else
        c = CollectionObject.new(p)

        if c.identifiers.first
          c.identifiers.first.identifier = identifier_for(i)
        end
        c.save!
      end

    end
  rescue ActiveRecord::RecordInvalid => e
    errors.add(:base, e.message)
    raise
  end
  true
end

#depiction_for(section) ⇒ Object



90
91
92
93
# File 'app/models/sled_image.rb', line 90

def depiction_for(section)
  x,y = section['column'], section['row']
  depictions.where(sled_image_x_position: x, sled_image_y_position: y).first
end


101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/sled_image.rb', line 101

def destroy_related
  if nuke == 'nuke'
    collection_objects.reload.each do |d|
      d.destroy
    end
  end

  depictions.each do |d|
    d.destroy
  end
  true
end

#get_identifier_matrixObject (private)



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/sled_image.rb', line 172

def get_identifier_matrix
  i = increment_matrix

  m = []

  .each do |s|
    r = s['row'].to_i
    c = s['column'].to_i
    m[r] ||= []
    m[r][c] = nil if !s['metadata'].blank?
    next if !s['metadata'].blank? || _first_identifier.nil?

    inc = r + c - i[r][c] + _first_identifier
    v = nil
    case step_identifier_on
    when 'row'
      v = (r * _column_total) + inc
    when 'column'
      v = (c * _row_total) + inc
    end

    m[r][c] = v
  end
  @_identifier_matrix = m
end

#identifier_for(section) ⇒ Object (private)



198
199
200
# File 'app/models/sled_image.rb', line 198

def identifier_for(section)
  _identifier_matrix[section['row'].to_i][section['column'].to_i]
end

#identity_matrixObject (private)



135
136
137
138
139
140
141
142
143
144
# File 'app/models/sled_image.rb', line 135

def identity_matrix
  i = []
  .each do |s|
    r = s['row'].to_i
    c = s['column'].to_i
    i[r] ||= []
    i[r][c] = s['metadata'].blank? ? 0 : 1
  end
  i
end

#increment_matrixObject (private)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/sled_image.rb', line 146

def increment_matrix
  i = []
  j = identity_matrix
  k = 0
  case step_identifier_on
  when 'row'
    (0.._row_total).each do |r|
      (0.._column_total).each do |c|
        k += j[r][c]
        i[r] ||= []
        i[r][c] = (j[r][c] == 1 ? 0 : k)
      end
    end

  when 'column'
    (0.._column_total).each do |c|
      (0.._row_total).each do |r|
        k += j[r][c]
        i[r] ||= []
        i[r][c] = (j[r][c] == 1 ? 0 : k)
      end
    end
  end
  i
end

#is_metadata_depiction?Boolean (private)

Returns:

  • (Boolean)


311
312
313
314
315
316
317
318
# File 'app/models/sled_image.rb', line 311

def 
  if depiction_params && !depiction_params.empty?
    a = depiction_params[:is_metadata_depiction]
    ((a == 'true') || (a == true)) ? true : false
  else
    false
  end
end

#new_collection_objectObject (private)



114
115
116
# File 'app/models/sled_image.rb', line 114

def new_collection_object
  CollectionObject.new(collection_object_params)
end

#processObject (private)



202
203
204
205
# File 'app/models/sled_image.rb', line 202

def process
  depictions.any? ?  syncronize : create_objects
  true
end

#set_cachedObject (private)



274
275
276
277
278
279
280
# File 'app/models/sled_image.rb', line 274

def set_cached
  update_columns(
    cached_total_collection_objects: ,
    cached_total_rows: _row_total,
    cached_total_columns: _column_total
  )
end

#summaryObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/sled_image.rb', line 74

def summary
  m = []
  .each do |s|
    c = s['column'].to_i
    r = s['row'].to_i
    m[r] ||= []
    d = depiction_for(s)
    m[r][c] = {
      depiction_id: d&.id,
      collection_object_id: d&.depiction_object_id,
      identifier: d&.depiction_object&.identifiers&.first&.cached
    }
  end
  m
end

#svg_clip(section) ⇒ Object (private)



304
305
306
307
# File 'app/models/sled_image.rb', line 304

def svg_clip(section)
  x, y, h, w = view_box_values(section)
  "<rect x=\"#{x}\" y=\"#{y}\" width=\"#{h}\" height=\"#{w}\" />"
end

#svg_view_box(section) ⇒ Object (private)



294
295
296
# File 'app/models/sled_image.rb', line 294

def svg_view_box(section)
  view_box_values(section).join(' ')
end

#syncronizeObject (private)



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'app/models/sled_image.rb', line 256

def syncronize
  if  == []
    process if !&.empty?
  else
    # At this point we only allow coordinate updates
    .each do |i|
      next if !i['metadata'].nil?
      if d = depiction_for(i)
        d.update_columns(
          svg_clip: svg_clip(i),
          svg_view_box: svg_view_box(i),
        )
      end
    end
  end
  true
end

#total(direction = 'row') ⇒ Object

zero beers



54
55
56
57
58
59
60
# File 'app/models/sled_image.rb', line 54

def total(direction = 'row')
  i = nil
  .each do |o|
    i = o[direction] if i.nil? || (!i.nil? && o[direction] > i)
  end
  i
end

#total_metadata_objectsObject (private)



282
283
284
# File 'app/models/sled_image.rb', line 282

def 
  .count
end

#view_box_values(section) ⇒ Object (private)

Returns top left x, top left y, height, width.

Returns:

  • top left x, top left y, height, width



299
300
301
302
# File 'app/models/sled_image.rb', line 299

def view_box_values(section)
  x1, y1, x2, y2 = coordinates(section)
  [x1, y1, x2 - x1, y2 - y1]
end