Class: Eco::API::UseCases::DefaultCases::CsvToTree::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb

Constant Summary collapse

TAGS_ATTRS =
[:l1, :l2, :l3, :l4, :l5, :l6, :l7, :l8, :l9, :l10, :l11]
ADDITIONAL_ATTRS =
[:row_num]
ALL_ATTRS =
ADDITIONAL_ATTRS + TAGS_ATTRS
ALLOWED_CHARACTERS =
"A-Za-z0-9 &_'\/.-"
VALID_TAG_REGEX =
/^[#{ALLOWED_CHARACTERS}]+$/
INVALID_TAG_REGEX =
/[^#{ALLOWED_CHARACTERS}]+/
VALID_TAG_CHARS =
/[#{ALLOWED_CHARACTERS}]+/
DOUBLE_BLANKS =
/\s\s+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#l1Object

Returns the value of attribute l1

Returns:

  • (Object)

    the current value of l1



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l1
  @l1
end

#l10Object

Returns the value of attribute l10

Returns:

  • (Object)

    the current value of l10



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l10
  @l10
end

#l11Object

Returns the value of attribute l11

Returns:

  • (Object)

    the current value of l11



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l11
  @l11
end

#l2Object

Returns the value of attribute l2

Returns:

  • (Object)

    the current value of l2



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l2
  @l2
end

#l3Object

Returns the value of attribute l3

Returns:

  • (Object)

    the current value of l3



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l3
  @l3
end

#l4Object

Returns the value of attribute l4

Returns:

  • (Object)

    the current value of l4



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l4
  @l4
end

#l5Object

Returns the value of attribute l5

Returns:

  • (Object)

    the current value of l5



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l5
  @l5
end

#l6Object

Returns the value of attribute l6

Returns:

  • (Object)

    the current value of l6



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l6
  @l6
end

#l7Object

Returns the value of attribute l7

Returns:

  • (Object)

    the current value of l7



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l7
  @l7
end

#l8Object

Returns the value of attribute l8

Returns:

  • (Object)

    the current value of l8



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l8
  @l8
end

#l9Object

Returns the value of attribute l9

Returns:

  • (Object)

    the current value of l9



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def l9
  @l9
end

#parentIdObject

Returns the value of attribute parentId.



12
13
14
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 12

def parentId
  @parentId
end

#row_numObject

Returns the value of attribute row_num

Returns:

  • (Object)

    the current value of row_num



2
3
4
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2

def row_num
  @row_num
end

Instance Method Details

#actual_levelObject



59
60
61
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 59

def actual_level
  tags_array.compact.length
end

#attr(sym) ⇒ Object



193
194
195
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 193

def attr(sym)
  self.send(sym.to_sym)
end

#blanks_between?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 162

def blanks_between?
  actual_level > empty_idx
end

#clear_level(i) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 112

def clear_level(i)
  case i
  when Enumerable
    target = i.to_a
  when Integer
    return false unless i >= 1 && i <= tag_attrs_count
    target = Array(i..tag_attrs_count)
  else
    return false
  end
  return false if target.empty?
  target.each do |n|
    #puts "clearing 'l#{n}': #{attr("l#{n}")}"
    set_attr("l#{n}", nil)
  end
  true
end

#copyObject



81
82
83
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 81

def copy
  self.class.new.set_attrs(**self.to_h)
end

#decouple(num = 1) ⇒ Object

We got a missing level that is compacted in one row Here we get the missing intermediate levels This is done from upper to lower level to ensure processing order It skips last one, as that is this object already



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 89

def decouple(num = 1)
  with_info = filled_idxs
  # must be the last among filled_idxs, so let's use it to verify
  unless with_info.last == tag_idx
    raise "Review this (row #{row_num}; '#{raw_tag}'): tag_idx is #{tag_idx}, while last filled idx is #{with_info.last}"
  end
  len = with_info.length
  target_idxs = with_info[len-(num+1)..-2]
  target_idxs.map do |idx|
    self.copy.tap do |dup|
      dup.clear_level(idx_to_level(idx + 1))
    end
  end
end

#empty_idxObject



76
77
78
79
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 76

def empty_idx
  tary = tags_array
  tary.index(nil) || tary.length + 1
end

#filled_idxsObject



156
157
158
159
160
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 156

def filled_idxs
  tags_array.each_with_index.with_object([]) do |(t, i), o|
    o << i if t
  end
end

#has_double_blanks?(str) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 201

def has_double_blanks?(str)
  return false if str.nil?
  str.match(DOUBLE_BLANKS)
end

#idObject



18
19
20
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 18

def id
  tag.upcase
end

#identify_invalid_characters(str) ⇒ Object



217
218
219
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 217

def identify_invalid_characters(str)
  str.gsub(VALID_TAG_CHARS, '')
end

#idx_to_level(x) ⇒ Object



148
149
150
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 148

def idx_to_level(x)
  x + 1
end

#invalid_warned!Object



47
48
49
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 47

def invalid_warned!
  @invalid_warned = true
end

#invalid_warned?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 43

def invalid_warned?
  @invalid_warned ||= false
end

#levelObject



55
56
57
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 55

def level
  actual_level
end

#level_to_idx(x) ⇒ Object



152
153
154
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 152

def level_to_idx(x)
  x - 1
end

#merge!(node) ⇒ Object



104
105
106
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 104

def merge!(node)
  override_upper_levels(node.tags_array)
end

#nameObject



22
23
24
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 22

def name
  tag
end

#nodeIdObject



14
15
16
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 14

def nodeId
  id
end

#override_lower_levels(src_tags_array, to_level: self.raw_level - 1) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 139

def override_lower_levels(src_tags_array, to_level: self.raw_level - 1)
  target_lev = Array(1..to_level)
  target_tags = src_tags_array[level_to_idx(1)..level_to_idx(to_level)]
  target_lev.zip(target_tags).each do |(n, tag)|
    set_attr("l#{n}", tag)
  end
  self
end

#override_upper_levels(src_tags_array, from_level: self.raw_level + 1) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 130

def override_upper_levels(src_tags_array, from_level: self.raw_level + 1)
  target_lev = Array(from_level..tag_attrs_count)
  target_tags = src_tags_array[level_to_idx(from_level)..level_to_idx(tag_attrs_count)]
  target_lev.zip(target_tags).each do |(n, tag)|
    set_attr("l#{n}", tag)
  end
  self
end

#previous_idxObject



71
72
73
74
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 71

def previous_idx
  idx = tag_idx - 1
  idx < 0 ? nil : idx
end

#raw_levelObject



63
64
65
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 63

def raw_level
  tags_array.index(raw_tag) + 1
end

#raw_tagObject



51
52
53
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 51

def raw_tag
  values_at(*TAGS_ATTRS.reverse).compact.first
end

#remove_double_blanks(str) ⇒ Object



206
207
208
209
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 206

def remove_double_blanks(str)
  return nil if str.nil?
  str.gsub(DOUBLE_BLANKS, ' ').strip
end

#replace_not_allowed(str) ⇒ Object



211
212
213
214
215
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 211

def replace_not_allowed(str)
  return nil if str.nil?
  return str if str.match(VALID_TAG_REGEX)
  str.gsub(INVALID_TAG_REGEX, ' ')
end

#set_attr(attr, value) ⇒ Object



189
190
191
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 189

def set_attr(attr, value)
  self.send("#{attr}=", value)
end

#set_attrs(**kargs) ⇒ Object



184
185
186
187
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 184

def set_attrs(**kargs)
  kargs.each {|attr, value| set_attr(attr, value)}
  self
end

#set_high_levels(node) ⇒ Object



108
109
110
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 108

def set_high_levels(node)
  override_lower_levels(node.tags_array)
end

#slice(*attrs) ⇒ Object



179
180
181
182
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 179

def slice(*attrs)
  return {} if attrs.empty?
  to_h(*attrs)
end

#tagObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 26

def tag
  raw_tag.yield_self do |str|
    blanks_x2 = has_double_blanks?(str)
    partial   = replace_not_allowed(str)
    remove_double_blanks(partial).tap do |result|
      next if invalid_warned?
      if partial != str
        invalid_chars = identify_invalid_characters(str)
        puts "• (Row: #{self.row_num}) Invalid characters _#{invalid_chars}_ (removed): '#{str}' (converted to '#{result}')"
      elsif blanks_x2
        puts "• (Row: #{self.row_num}) Double blanks (removed): '#{str}' (converted to '#{result}')"
      end
      invalid_warned!
    end
  end
end

#tag_attrs_countObject



197
198
199
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 197

def tag_attrs_count
  TAGS_ATTRS.length
end

#tag_idxObject



67
68
69
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 67

def tag_idx
  tags_array.index(raw_tag)
end

#tags_arrayObject



166
167
168
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 166

def tags_array
  values_at(*TAGS_ATTRS)
end

#to_h(*attrs) ⇒ Object



174
175
176
177
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 174

def to_h(*attrs)
  attrs = ALL_ATTRS if attrs.empty?
  attrs.zip(values_at(*attrs)).to_h
end

#values_at(*attrs) ⇒ Object



170
171
172
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 170

def values_at(*attrs)
  attrs.map {|a| attr(a)}
end