tablestructured 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c77dbdbe0cccc56193727474e29adcbeae82f32
4
- data.tar.gz: adf4183d1aaf55fbce1c65f40b920c01ea787e6e
3
+ metadata.gz: afe95e0ea3585ddcf3f5eb0a76439ff7465b73c3
4
+ data.tar.gz: 9dc5a0d873b750f94a6b38485d88d6169aa5e46e
5
5
  SHA512:
6
- metadata.gz: 2f96dfc2fda4cee69e3cc9cfa3c0132a570e805a2cdff7fb6deec1f394531e839134a87a1fd16d83ef7c73db023b3e1c12fd8825d4de9f01ec3721935b9c6b60
7
- data.tar.gz: 6ddb896c4c6320c9a4f0002f9389009726e477fc86c13c340832c9fb53a878acc21c0f6ef6f92c1a09771c240b51cf074b9b9167d56622429a5d21a022e68ca6
6
+ metadata.gz: e5a62687c47e0162da70c59c42f1ff1f47dc183094e8a423cef9eae5a6a9e7b20de45bc538a172efdcf1a51f1eb7076e5bf6cb4d01d8ece862d054315d735395
7
+ data.tar.gz: f9b29c7573f1ad098a576df0c008b468bae2870ed360c4c46b245111cf68dafc754dcc19783757450de657c63dfc4cf6d90923cc626f261599f84856a8c787f6
@@ -1,12 +1,38 @@
1
1
  module TableStructured
2
- def self.new object, headers: :top # TODO: headers_left should probably mean the ids of entries
3
- if object.respond_to? :css
2
+ Error = Class.new RuntimeError
3
+ def self.new object, headers: :top, drop_first: 0, drop_last: 0 # TODO: headers_left should probably mean the ids of entries
4
+ if object.nil?
5
+ raise ArgumentError, "passed object can be an Array or Ferrum Node, but not Nil"
6
+ elsif object.respond_to? :css
4
7
  # the xml object ignores the :headers arg for now
5
- struct = Struct.new *object.at_css("thead").css("th").map(&:text).map(&:to_sym).tap{ |_| fail "headers should be unique" if _.uniq! }
6
- object.css("tr").drop(1).map{ |_| struct.new *_.css("td") }
8
+ ss = if :top == headers
9
+ object
10
+ elsif headers.respond_to? :css
11
+ headers
12
+ else
13
+ fail "invalid type of headers"
14
+ end.css("th")[drop_first..-1-drop_last].map{ |_| _.text.sub(/\A[[[:space:]]]*/,"").sub(/[[[:space:]]]*\z/,"").to_sym }
15
+ struct = begin
16
+ Struct.new *ss
17
+ rescue NameError
18
+ raise $!.exception "#{$!}: #{ss.inspect}"
19
+ end
20
+ object.css("tbody>tr").map do |_|
21
+ tds = []
22
+ Timeout.timeout 2 do
23
+ tds = _.css("td")[drop_first..-1-drop_last]
24
+ if tds.empty?
25
+ STDERR.puts "empty row"
26
+ sleep 0.1
27
+ redo
28
+ end
29
+ end
30
+ raise Error, "size mismatch (#{ss.size} headers, #{tds.size} row items)" if tds.size != ss.size
31
+ struct.new *tds
32
+ end
7
33
  else
8
- struct = Struct.new *object.first.map(&:to_s).map(&:to_sym).tap{ |_| fail "headers should be unique" if _.uniq! }
9
- object.drop(1).map{ |_| struct.new *_ }
34
+ struct = Struct.new *(headers == :top ? object.first : headers).map(&:to_s).map(&:to_sym).tap{ |_| fail "headers should be unique" if _.uniq! }
35
+ object.drop(headers == :top ? 1 : 0).map{ |_| struct.new *_ }
10
36
  end
11
37
  end
12
38
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "tablestructured"
3
- spec.version = "0.1.1"
3
+ spec.version = "0.2.0"
4
4
  spec.summary = "convert two-dimensional Array into an Array of Structs consuming the first row and/or column as attributes"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablestructured
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-06 00:00:00.000000000 Z
11
+ date: 2022-09-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: nakilon@gmail.com