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_in_use?, #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



46
47
48
# File 'app/models/sled_image.rb', line 46

def _column_total
  @_column_total
end

#_first_identifierObject (private)

Internal processing



46
47
48
# File 'app/models/sled_image.rb', line 46

def _first_identifier
  @_first_identifier
end

#_identifier_matrixArray of Arrays (private)

Returns:

  • (Array of Arrays)


49
50
51
# File 'app/models/sled_image.rb', line 49

def _identifier_matrix
  @_identifier_matrix
end

#_row_totalObject (private)

Internal processing



46
47
48
# File 'app/models/sled_image.rb', line 46

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

#horizontal_step_directionObject

Parameters:

  • horizontal_step_direction (String)

    'left_to_right' (default), or 'right_to_left'. The order in which columns are visited when computing object identifiers.



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

def horizontal_step_direction
  @horizontal_step_direction
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# 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

  # @param horizontal_step_direction [String]
  #   'left_to_right' (default), or 'right_to_left'.  The order in which columns
  #   are visited when computing object identifiers.
  attr_accessor :horizontal_step_direction

  # @param vertical_step_direction [String]
  #   'top_to_bottom' (default), or 'bottom_to_top'.  The order in which rows
  #   are visited when computing object identifiers.
  attr_accessor :vertical_step_direction

  # 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, inverse_of: :sled_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

  def horizontal_step_direction
    @horizontal_step_direction ||= 'left_to_right'
  end

  def vertical_step_direction
    @vertical_step_direction ||= 'top_to_bottom'
  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_s
  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_identifier(code, increment = 1)
    if code =~ /(.*?)(\d+)([^0-9]*)$/
      prefix = Regexp.last_match(1)
      number = Regexp.last_match(2)
      suffix = Regexp.last_match(3)

      new_number = number.to_i + increment
      padded_number = new_number.to_s.rjust(number.length, '0')

      "#{prefix}#{padded_number}#{suffix}"
    else
      nil
    end
  end

  # @return [Array of Arrays]
  #   [row, column] pairs, in the order in which identifiers are assigned to them.
  #   The pattern is the combination of the axis stepped on first
  #   (`step_identifier_on`) and the direction each axis is travelled in
  #   (`horizontal_step_direction`, `vertical_step_direction`).
  def ordered_sections
    return [] if .blank?

    rows = (0.._row_total).to_a
    columns = (0.._column_total).to_a

    rows.reverse! if vertical_step_direction == 'bottom_to_top'
    columns.reverse! if horizontal_step_direction == 'right_to_left'

    if step_identifier_on == 'row'
      rows.product(columns)
    else
      columns.product(rows).collect { |column, row| [row, column] }
    end
  end

  def get_identifier_matrix
    sections = identity_matrix
    m = []
    increment = 0

    ordered_sections.each do |row, column|
      m[row] ||= []

      # Sections that are metadata, or that are not present at all, are skipped
      # without consuming an identifier.
      if sections.dig(row, column) == 0
        m[row][column] = _first_identifier.nil? ? nil : increment_identifier(_first_identifier, increment)
        increment += 1
      else
        m[row][column] = nil
      end
    end

    @_identifier_matrix = m
  end

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

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

  def create_objects
    return true unless !collection_object_params.nil?
    begin
      .each do |i|
        next if i['metadata'].present?
        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 synchronize
    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.present?
      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



43
44
45
# File 'app/models/sled_image.rb', line 43

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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# 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

  # @param horizontal_step_direction [String]
  #   'left_to_right' (default), or 'right_to_left'.  The order in which columns
  #   are visited when computing object identifiers.
  attr_accessor :horizontal_step_direction

  # @param vertical_step_direction [String]
  #   'top_to_bottom' (default), or 'bottom_to_top'.  The order in which rows
  #   are visited when computing object identifiers.
  attr_accessor :vertical_step_direction

  # 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, inverse_of: :sled_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

  def horizontal_step_direction
    @horizontal_step_direction ||= 'left_to_right'
  end

  def vertical_step_direction
    @vertical_step_direction ||= 'top_to_bottom'
  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_s
  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_identifier(code, increment = 1)
    if code =~ /(.*?)(\d+)([^0-9]*)$/
      prefix = Regexp.last_match(1)
      number = Regexp.last_match(2)
      suffix = Regexp.last_match(3)

      new_number = number.to_i + increment
      padded_number = new_number.to_s.rjust(number.length, '0')

      "#{prefix}#{padded_number}#{suffix}"
    else
      nil
    end
  end

  # @return [Array of Arrays]
  #   [row, column] pairs, in the order in which identifiers are assigned to them.
  #   The pattern is the combination of the axis stepped on first
  #   (`step_identifier_on`) and the direction each axis is travelled in
  #   (`horizontal_step_direction`, `vertical_step_direction`).
  def ordered_sections
    return [] if .blank?

    rows = (0.._row_total).to_a
    columns = (0.._column_total).to_a

    rows.reverse! if vertical_step_direction == 'bottom_to_top'
    columns.reverse! if horizontal_step_direction == 'right_to_left'

    if step_identifier_on == 'row'
      rows.product(columns)
    else
      columns.product(rows).collect { |column, row| [row, column] }
    end
  end

  def get_identifier_matrix
    sections = identity_matrix
    m = []
    increment = 0

    ordered_sections.each do |row, column|
      m[row] ||= []

      # Sections that are metadata, or that are not present at all, are skipped
      # without consuming an identifier.
      if sections.dig(row, column) == 0
        m[row][column] = _first_identifier.nil? ? nil : increment_identifier(_first_identifier, increment)
        increment += 1
      else
        m[row][column] = nil
      end
    end

    @_identifier_matrix = m
  end

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

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

  def create_objects
    return true unless !collection_object_params.nil?
    begin
      .each do |i|
        next if i['metadata'].present?
        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 synchronize
    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.present?
      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

#vertical_step_directionObject

Parameters:

  • vertical_step_direction (String)

    'top_to_bottom' (default), or 'bottom_to_top'. The order in which rows are visited when computing object identifiers.



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

def vertical_step_direction
  @vertical_step_direction
end

Instance Method Details

#coordinates(section) ⇒ Object (private)

x1, y1, x2, y2



310
311
312
313
314
# File 'app/models/sled_image.rb', line 310

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)



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
# File 'app/models/sled_image.rb', line 230

def create_objects
  return true unless !collection_object_params.nil?
  begin
    .each do |i|
      next if i['metadata'].present?
      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



100
101
102
103
# File 'app/models/sled_image.rb', line 100

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


119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/sled_image.rb', line 119

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)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/sled_image.rb', line 200

def get_identifier_matrix
  sections = identity_matrix
  m = []
  increment = 0

  ordered_sections.each do |row, column|
    m[row] ||= []

    # Sections that are metadata, or that are not present at all, are skipped
    # without consuming an identifier.
    if sections.dig(row, column) == 0
      m[row][column] = _first_identifier.nil? ? nil : increment_identifier(_first_identifier, increment)
      increment += 1
    else
      m[row][column] = nil
    end
  end

  @_identifier_matrix = m
end

#identifier_for(section) ⇒ Object (private)



221
222
223
# File 'app/models/sled_image.rb', line 221

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

#identity_matrixObject (private)



153
154
155
156
157
158
159
160
161
162
# File 'app/models/sled_image.rb', line 153

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_identifier(code, increment = 1) ⇒ Object (private)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/sled_image.rb', line 164

def increment_identifier(code, increment = 1)
  if code =~ /(.*?)(\d+)([^0-9]*)$/
    prefix = Regexp.last_match(1)
    number = Regexp.last_match(2)
    suffix = Regexp.last_match(3)

    new_number = number.to_i + increment
    padded_number = new_number.to_s.rjust(number.length, '0')

    "#{prefix}#{padded_number}#{suffix}"
  else
    nil
  end
end

#is_metadata_depiction?Boolean (private)

Returns:

  • (Boolean)


334
335
336
337
338
339
340
341
# File 'app/models/sled_image.rb', line 334

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

#new_collection_objectObject (private)



132
133
134
# File 'app/models/sled_image.rb', line 132

def new_collection_object
  CollectionObject.new(collection_object_params)
end

#ordered_sectionsArray of Arrays (private)

Returns [row, column] pairs, in the order in which identifiers are assigned to them. The pattern is the combination of the axis stepped on first (step_identifier_on) and the direction each axis is travelled in (horizontal_step_direction, vertical_step_direction).

Returns:

  • (Array of Arrays)

    [row, column] pairs, in the order in which identifiers are assigned to them. The pattern is the combination of the axis stepped on first (step_identifier_on) and the direction each axis is travelled in (horizontal_step_direction, vertical_step_direction).



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/sled_image.rb', line 184

def ordered_sections
  return [] if .blank?

  rows = (0.._row_total).to_a
  columns = (0.._column_total).to_a

  rows.reverse! if vertical_step_direction == 'bottom_to_top'
  columns.reverse! if horizontal_step_direction == 'right_to_left'

  if step_identifier_on == 'row'
    rows.product(columns)
  else
    columns.product(rows).collect { |column, row| [row, column] }
  end
end

#processObject (private)



225
226
227
228
# File 'app/models/sled_image.rb', line 225

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

#set_cachedObject (private)



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

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

#summaryObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/sled_image.rb', line 84

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)



327
328
329
330
# File 'app/models/sled_image.rb', line 327

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)



317
318
319
# File 'app/models/sled_image.rb', line 317

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

#synchronizeObject (private)



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/models/sled_image.rb', line 279

def synchronize
  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



64
65
66
67
68
69
70
# File 'app/models/sled_image.rb', line 64

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)



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

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



322
323
324
325
# File 'app/models/sled_image.rb', line 322

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