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



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

def actual_level
  tags_array.compact.length
end

#attr(sym) ⇒ Object



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

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

#blanks_between?Boolean

Returns:

  • (Boolean)


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

def blanks_between?
  actual_level > empty_idx
end

#clear_level(i) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 102

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



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

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 79

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



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

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

#filled_idxsObject



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

def filled_idxs
  tags_array.each_with_index.with_object([]) do |(t, i), o|
    o << i if t
  end
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



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

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

#idx_to_level(x) ⇒ Object



138
139
140
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 138

def idx_to_level(x)
  x + 1
end

#levelObject



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

def level
  actual_level
end

#level_to_idx(x) ⇒ Object



142
143
144
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 142

def level_to_idx(x)
  x - 1
end

#merge!(node) ⇒ Object



94
95
96
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 94

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



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

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



120
121
122
123
124
125
126
127
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 120

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



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

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

#raw_levelObject



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

def raw_level
  tags_array.index(raw_tag) + 1
end

#raw_tagObject



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

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

#remove_double_blanks(str) ⇒ Object



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

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

#replace_not_allowed(str) ⇒ Object



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

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



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

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

#set_attrs(**kargs) ⇒ Object



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

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

#set_high_levels(node) ⇒ Object



98
99
100
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 98

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

#slice(*attrs) ⇒ Object



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

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
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 26

def tag
  raw_tag.yield_self do |str|
    partial = replace_not_allowed(str)
    remove_double_blanks(partial).tap do |result|
      if partial != str
        invalid_chars = identify_invalid_characters(str)
        puts "• (Row: #{self.row_num}) Invalid characters _#{invalid_chars}_ (removed): '#{str}' (converted to '#{result}')"
      end
      if result != partial
        #puts "• There were DOUBLE BLANKS: tag '#{str}' (converted to '#{result}')"
      end
    end
  end
end

#tag_attrs_countObject



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

def tag_attrs_count
  TAGS_ATTRS.length
end

#tag_idxObject



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

def tag_idx
  tags_array.index(raw_tag)
end

#tags_arrayObject



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

def tags_array
  values_at(*TAGS_ATTRS)
end

#to_h(*attrs) ⇒ Object



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

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

#values_at(*attrs) ⇒ Object



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

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