Class: Eco::API::UseCases::DefaultCases::CsvToTree::Node
- Inherits:
-
Struct
- Object
- Struct
- Eco::API::UseCases::DefaultCases::CsvToTree::Node
- 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
-
#l1 ⇒ Object
Returns the value of attribute l1.
-
#l10 ⇒ Object
Returns the value of attribute l10.
-
#l11 ⇒ Object
Returns the value of attribute l11.
-
#l2 ⇒ Object
Returns the value of attribute l2.
-
#l3 ⇒ Object
Returns the value of attribute l3.
-
#l4 ⇒ Object
Returns the value of attribute l4.
-
#l5 ⇒ Object
Returns the value of attribute l5.
-
#l6 ⇒ Object
Returns the value of attribute l6.
-
#l7 ⇒ Object
Returns the value of attribute l7.
-
#l8 ⇒ Object
Returns the value of attribute l8.
-
#l9 ⇒ Object
Returns the value of attribute l9.
-
#parentId ⇒ Object
Returns the value of attribute parentId.
-
#row_num ⇒ Object
Returns the value of attribute row_num.
Instance Method Summary collapse
- #actual_level ⇒ Object
- #attr(sym) ⇒ Object
- #blanks_between? ⇒ Boolean
- #clear_level(i) ⇒ Object
- #copy ⇒ Object
-
#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.
- #empty_idx ⇒ Object
- #filled_idxs ⇒ Object
- #id ⇒ Object
- #identify_invalid_characters(str) ⇒ Object
- #idx_to_level(x) ⇒ Object
- #level ⇒ Object
- #level_to_idx(x) ⇒ Object
- #merge!(node) ⇒ Object
- #name ⇒ Object
- #nodeId ⇒ Object
- #override_lower_levels(src_tags_array, to_level: self.raw_level - 1) ⇒ Object
- #override_upper_levels(src_tags_array, from_level: self.raw_level + 1) ⇒ Object
- #previous_idx ⇒ Object
- #raw_level ⇒ Object
- #raw_tag ⇒ Object
- #remove_double_blanks(str) ⇒ Object
- #replace_not_allowed(str) ⇒ Object
- #set_attr(attr, value) ⇒ Object
- #set_attrs(**kargs) ⇒ Object
- #set_high_levels(node) ⇒ Object
- #slice(*attrs) ⇒ Object
- #tag ⇒ Object
- #tag_attrs_count ⇒ Object
- #tag_idx ⇒ Object
- #tags_array ⇒ Object
- #to_h(*attrs) ⇒ Object
- #values_at(*attrs) ⇒ Object
Instance Attribute Details
#l1 ⇒ Object
Returns the value of attribute l1
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l1 @l1 end |
#l10 ⇒ Object
Returns the value of attribute l10
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l10 @l10 end |
#l11 ⇒ Object
Returns the value of attribute l11
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l11 @l11 end |
#l2 ⇒ Object
Returns the value of attribute l2
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l2 @l2 end |
#l3 ⇒ Object
Returns the value of attribute l3
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l3 @l3 end |
#l4 ⇒ Object
Returns the value of attribute l4
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l4 @l4 end |
#l5 ⇒ Object
Returns the value of attribute l5
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l5 @l5 end |
#l6 ⇒ Object
Returns the value of attribute l6
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l6 @l6 end |
#l7 ⇒ Object
Returns the value of attribute l7
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l7 @l7 end |
#l8 ⇒ Object
Returns the value of attribute l8
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l8 @l8 end |
#l9 ⇒ Object
Returns the value of attribute l9
2 3 4 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 2 def l9 @l9 end |
#parentId ⇒ Object
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_num ⇒ Object
Returns the value of attribute 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_level ⇒ Object
49 50 51 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 49 def actual_level .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
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 |
#copy ⇒ Object
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_idx ⇒ Object
66 67 68 69 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 66 def empty_idx tary = tary.index(nil) || tary.length + 1 end |
#filled_idxs ⇒ Object
146 147 148 149 150 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 146 def filled_idxs .each_with_index.with_object([]) do |(t, i), o| o << i if t end end |
#id ⇒ Object
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 |
#level ⇒ Object
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.) end |
#name ⇒ Object
22 23 24 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 22 def name tag end |
#nodeId ⇒ Object
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(, to_level: self.raw_level - 1) target_lev = Array(1..to_level) = [level_to_idx(1)..level_to_idx(to_level)] target_lev.zip().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(, from_level: self.raw_level + 1) target_lev = Array(from_level..tag_attrs_count) = [level_to_idx(from_level)..level_to_idx(tag_attrs_count)] target_lev.zip().each do |(n, tag)| set_attr("l#{n}", tag) end self end |
#previous_idx ⇒ Object
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_level ⇒ Object
53 54 55 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 53 def raw_level .index(raw_tag) + 1 end |
#raw_tag ⇒ Object
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.) 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 |
#tag ⇒ Object
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_count ⇒ Object
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_idx ⇒ Object
57 58 59 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 57 def tag_idx .index(raw_tag) end |
#tags_array ⇒ Object
156 157 158 |
# File 'lib/eco/api/usecases/default_cases/csv_to_tree_case/node.rb', line 156 def 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 |