true_table 0.1.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de3dc5997b4c8ebd6995d960924a0e40212bd2d276b0bc86dd212cbd3202686c
4
+ data.tar.gz: 8b2c0a8b433450c0b35672efd948309c88f41c9579884abbe85d251cf6a1398f
5
+ SHA512:
6
+ metadata.gz: 836d46c004f4b660928684a5860f34378dda5157dec22b0fd2cb100750b4761fcab619e390adc650aba00f069d6517cc1e61afac94d8dabcde3be977cddc9dfa
7
+ data.tar.gz: afffb78d4d731c67164ce9731f1dc8e5eef80a9d7f4902e528a6d2beb28ee87d9d670e96fe5baa4410609f2edf893c7be7eb6551d9674cff69c3154e13f46e65
@@ -0,0 +1,20 @@
1
+ # True Table
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/true_table.svg)](https://badge.fury.io/rb/true_table)
4
+ [![Build Status](https://github.com/DannyBen/true_table/workflows/Test/badge.svg)](https://github.com/DannyBen/true_table/actions?query=workflow%3ATest)
5
+
6
+ ---
7
+
8
+ Simple and intuitive tabular data type, which is an Array of Hashes.
9
+
10
+ ---
11
+
12
+ ## Installation
13
+
14
+ ```
15
+ $ gem install true_table
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ TODO
@@ -0,0 +1,2 @@
1
+ require 'true_table/table'
2
+ require 'byebug' if ENV['BYEBUG']
@@ -0,0 +1,161 @@
1
+ module TrueTable
2
+ class Table < Array
3
+ # Combines table with other and returns a new one
4
+ def +(other)
5
+ result = self.class.new
6
+ each_row { |row, i| result << row.merge(other[i]) }
7
+ result
8
+ end
9
+
10
+ # Returns a new table without specified columns
11
+ def -(cols)
12
+ keep_keys = headers - cols
13
+ result = self.class.new
14
+ each_row { |row, i| result << row.slice(*keep_keys) }
15
+ result
16
+ end
17
+
18
+ # Returns a row or a column
19
+ def [](key)
20
+ key.is_a?(Symbol) || key.is_a?(String) ? col(key.to_sym) : super
21
+ end
22
+
23
+ # Adds or updates a row or a column
24
+ def []=(key, value)
25
+ key.is_a?(Symbol) || key.is_a?(String) ? add_col(key.to_sym, value) : super
26
+ end
27
+
28
+ # Returns a column as Array
29
+ def col(key)
30
+ map { |row| row[key] }
31
+ end
32
+
33
+ # Returns a hash of columns
34
+ def cols
35
+ result = {}
36
+ each_col { |col, header| result[header] = col }
37
+ result
38
+ end
39
+
40
+ # Returns a copy of self without rows that contain nil in any column
41
+ def compact
42
+ dup.compact!
43
+ end
44
+
45
+ # Removes rows with nil in any column
46
+ def compact!
47
+ delete_if { |row| row.values.include? nil }
48
+ end
49
+
50
+ # Delete a row or a column in place and returns the deleted row/column
51
+ def delete_at(index)
52
+ if index.is_a?(Symbol) || index.is_a?(String)
53
+ result = self[index]
54
+ return nil unless result
55
+ each_row { |row, i| row.delete index }
56
+ result
57
+ else
58
+ super
59
+ end
60
+ end
61
+ alias delete_col delete_at
62
+ alias delete_row delete_at
63
+
64
+ # Returns a table with different rows
65
+ def difference(*others)
66
+ self.class.new super
67
+ end
68
+
69
+ # Extracts nested value. Accepts row, column or column, row
70
+ def dig(*indexes)
71
+ key = indexes.shift
72
+ if key.is_a?(Symbol) || key.is_a?(String)
73
+ col(key.to_sym).dig *indexes
74
+ else
75
+ row(key).dig *indexes
76
+ end
77
+ end
78
+
79
+ # Iterates over columns
80
+ def each_col
81
+ headers.each { |header| yield col(header), header }
82
+ end
83
+
84
+ # Iterates over rows
85
+ alias each_row each_with_index
86
+
87
+ # Returns an array of column headers
88
+ def headers
89
+ first.keys
90
+ end
91
+
92
+ # Returns a new table with intersecting rows
93
+ def intersection(*others)
94
+ self.class.new super
95
+ end
96
+
97
+ # Returns a string with joined rows and columns
98
+ def join(row_separator = $,, col_separator = nil, with_headers: false)
99
+ if col_separator
100
+ result = map { |row| row.values.join col_separator }.join(row_separator)
101
+ with_headers ? headers.join(col_separator) + row_separator + result : result
102
+ else
103
+ super row_separator
104
+ end
105
+ end
106
+
107
+ # Returns the last row or a new table with the last N rows
108
+ def last(*args)
109
+ args.empty? ? super : self.class.new(super)
110
+ end
111
+
112
+ # Returns a new table without rejected rows
113
+ def reject
114
+ self.class.new super
115
+ end
116
+
117
+ # Returns a reversed copy
118
+ def reverse
119
+ self.class.new super
120
+ end
121
+
122
+ # Returns a row
123
+ alias row []
124
+
125
+ # Returns a new table with selected rows
126
+ def select
127
+ self.class.new super
128
+ end
129
+ alias filter select
130
+
131
+ # Returns a new sorted table
132
+ def sort
133
+ self.class.new super
134
+ end
135
+
136
+ # Returns a new sorted table
137
+ def sort_by
138
+ self.class.new super
139
+ end
140
+
141
+ # Returns a CSV string
142
+ def to_csv(row_separator = "\n", col_separator = ",")
143
+ join(row_separator, col_separator, with_headers: true)
144
+ end
145
+
146
+ # Returns only values, without any headers (array of arrays)
147
+ def values
148
+ map { |row| row.values }
149
+ end
150
+
151
+ protected
152
+
153
+ def add_col(key, values)
154
+ values.each_with_index do |value, i|
155
+ self[i] ||= {}
156
+ self[i][key] = value
157
+ end
158
+ end
159
+
160
+ end
161
+ end
@@ -0,0 +1,3 @@
1
+ module TrueTable
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: true_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple and intuitive tabular data type
14
+ email: db@dannyben.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/true_table.rb
21
+ - lib/true_table/table.rb
22
+ - lib/true_table/version.rb
23
+ homepage: https://github.com/dannyben/true_table
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.4.0
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.1.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Simple and intuitive tabular data type
46
+ test_files: []