Module: Eco::API::UseCases::OozeSamples::HelpersMigration::Copying

Includes:
Language::AuxiliarLogger
Included in:
Eco::API::UseCases::OozeSamples::HelpersMigration
Defined in:
lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

#copy_field_content(src, dst) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 132

def copy_field_content(src, dst) # rubocop:disable Metrics/AbcSize
  type = src.type
  case type
  when "select"
    src.values.each {|val| dst.select(val)} # rubocop:disable Style/HashEachMethods
    dst.other_desc = src.other_desc if src.other && src.other_desc && dst.other
  when "plain_text", "date", "number", "gauge"
    dst.value = src.value
  when "rich_text"
    dst.content = src.content
  when "cross_reference"
    src.reference_ids.each {|id| dst.add(id)}
  when "people"
    dst.people_ids << src.people_ids.to_a
  when "geo"
    src_coo = src.coordinates
    dst_coo = dst.coordinates
    dst_coo.lat, dst_coo.lon = src_coo.lat, src_coo.lon # rubocop:disable Style/ParallelAssignment
  when "file"
    src.items.each do |src_item|
      dst.add_file(src_item.file_container_id) do |dst_item|
        dst_item.label = src_item.label
      end
    end
  when "image_gallery"
    src.images.each do |src_image|
      dst.add_image(src_image.upload_id) do |dst_image|
        dst_image.caption = src_image.caption
      end
    end
  when "checklist"
    src.items.each do |src_item|
      dst.add_item(label: src_item.label) do |dst_item|
        dst_item.checked = src_item.checked
      end
    end
  when "law"
    "won't copy"
  when "page_action"
    "won't copy"
  when "actions_list"
    "won't copy"
  when "signature"
    "can't copy"
  when "tag_field"
    "can't copy"
  when "chart"
    "won't copy"
  when "frequency_rate_chart"
    "won't copy"
  end
end

#copy_generic_paired_fields(src, dst, src_excluded: [], dst_excluded: [], exclude_ximport: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 11

def copy_generic_paired_fields(src, dst, src_excluded: [], dst_excluded: [], exclude_ximport: false)
  HelpersMigration::TypedFieldsPairing.new(
    src,
    dst,
    src_excluded:    src_excluded,
    dst_excluded:    dst_excluded,
    exclude_ximport: exclude_ximport
  ).tap do |pairing|
    pairing.each_pair do |src_fld, dst_fld|
      copy_field_content(src_fld, dst_fld)
      yield(src_fld, dst_fld, pairing) if block_given?
    end
  end
end

#copy_hooked_fields(src, dst, hook:, type:, fields_tracker: nil, src_excluded: [], dst_excluded: []) ⇒ Object



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
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 26

def copy_hooked_fields(src, dst, hook:, type:, fields_tracker: nil, src_excluded: [], dst_excluded: [])
  case hook
  when String
    with_src_dst_field_pair(
      src,
      dst,
      label:        to_regex(hook),
      type:         type,
      src_excluded: src_excluded,
      dst_excluded: dst_excluded
    ) do |src_fld, dst_fld|
      copy_field_content(src_fld, dst_fld).tap do
        fields_tracker&.resolve(src_fld, dst_fld)
        yield(src_fld, dst_fld, fields_tracker) if block_given?
      end
    end
  when Hash
    hook.each do |hook_src, hook_dst|
      src_lab = to_regex(hook_src)
      dst_lab = to_regex(hook_dst)

      with_src_dst_field_pair(
        src,
        dst,
        label:        src_lab,
        label_dst:    dst_lab,
        type:         type,
        src_excluded: src_excluded,
        dst_excluded: dst_excluded
      ) do |src_fld, dst_fld|
        copy_field_content(src_fld, dst_fld).tap do
          fields_tracker&.resolve(src_fld, dst_fld)
          yield(src_fld, dst_fld, fields_tracker) if block_given?
        end
      end
    end
  end
end

#sessionObject



7
8
9
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 7

def session
  defined?(super)? super : @session
end

#to_regex(str) ⇒ Object



65
66
67
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 65

def to_regex(str)
  /^#{str}$/i
end

#with_src_dst_field_pair(src, dst, label: nil, label_dst: label, type: nil, src_excluded: [], dst_excluded: []) ⇒ Object

Note:
  • When used with type: nil it may pair fields of different type. There isn't refinement for this yet.


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
# File 'lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb', line 71

def with_src_dst_field_pair(src, dst, label: nil, label_dst: label, type: nil, src_excluded: [], dst_excluded: []) # rubocop:disable Metrics/AbcSize
  msg = "These helpers are to be included in class cases inheriting from Eco::API::UseCases::OozeSamples::RegisterUpdateCase"
  raise msg unless respond_to?(:with_fields, true)

  src_flds = with_fields(src, label: label, type: type)
  dst_flds = with_fields(dst, label: label_dst, type: type)
  src_fld  = src_flds.first
  dst_fld  = dst_flds.first

  # Not found
  if dst_flds.empty? || src_flds.empty?
    msg     = "There isn't a '#{type}'"
    dst_msg = dst_flds.empty?? "destination field named '#{label_dst}'" : nil
    src_msg = src_flds.empty?? "source field named '#{label}'" : nil
    msg    << " #{[dst_msg, src_msg].join(" or ")} (source: '#{src.id}')"

    log(:warn) { msg }
    return nil, nil
  end

  # Fields exclusion
  dst_flds -= dst_excluded unless dst_excluded.empty?
  src_flds -= src_excluded unless src_excluded.empty?

  src_fld = src_flds.first
  dst_fld = dst_flds.first

  return src_fld, dst_fld if dst_flds.empty? || src_flds.empty?

  if dst_flds.count > 1
    log(:warn) {
      msg  = "There are #{dst_flds.count} destination '#{type}' fields "
      msg << "named '#{label_dst}' (source: '#{dst.id}'). Using first..."
      msg
    }
  end

  if src_flds.count > 1
    msg         = "There are #{src_flds.count} source '#{type}' fields named '#{label}' (source: '#{src.id}'). "
    src_content = src_flds.reject(&:empty?)

    if src_content.empty?
      msg << "None with content. Using first..."
    else
      src_fld = src_content.first

      if src_content.count == 1
        msg << "Using the only one with content (idx: #{src_flds.index(src_fld)})..."
      else
        msg << "#{src_content.count} have content. Using first..."
      end
    end

    log(:warn) { msg }
  end

  [src_fld, dst_fld].tap do
    yield(src_fld, dst_fld) if block_given?
  end
end