true_table 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de3dc5997b4c8ebd6995d960924a0e40212bd2d276b0bc86dd212cbd3202686c
4
- data.tar.gz: 8b2c0a8b433450c0b35672efd948309c88f41c9579884abbe85d251cf6a1398f
3
+ metadata.gz: 9c778d54af435cbd53a4dfc45df1026d954dd79fda06c12ca7de9c5203054bd5
4
+ data.tar.gz: ecb8c574d6f53b7936cf7c668e55050f4effee32063922b92605200f48569d94
5
5
  SHA512:
6
- metadata.gz: 836d46c004f4b660928684a5860f34378dda5157dec22b0fd2cb100750b4761fcab619e390adc650aba00f069d6517cc1e61afac94d8dabcde3be977cddc9dfa
7
- data.tar.gz: afffb78d4d731c67164ce9731f1dc8e5eef80a9d7f4902e528a6d2beb28ee87d9d670e96fe5baa4410609f2edf893c7be7eb6551d9674cff69c3154e13f46e65
6
+ metadata.gz: 9cc7983ee8202a8ce1c1e4414dc5e5a2c34f5ed4e70c2789e2c940c325431cb1034d99ddaaae6c11e7771b993b8f0f747c3e3a7a792445b22f6376573e02b638
7
+ data.tar.gz: 3f2e42e7b50716d0e93e0676fd7c161a8e6bab1962c50358b734fb301d7b967785acaa7db79382a1b70fffce28c54d6ed9e1e27a88e45c237db717c2ddca6ed5
data/README.md CHANGED
@@ -5,7 +5,8 @@
5
5
 
6
6
  ---
7
7
 
8
- Simple and intuitive tabular data type, which is an Array of Hashes.
8
+ Simple and intuitive tabular data type, which is a subclass of `Array` where
9
+ each row is a `Hash`.
9
10
 
10
11
  ---
11
12
 
@@ -7,7 +7,7 @@ module TrueTable
7
7
  result
8
8
  end
9
9
 
10
- # Returns a new table without specified columns
10
+ # Returns a new table without the specified columns
11
11
  def -(cols)
12
12
  keep_keys = headers - cols
13
13
  result = self.class.new
@@ -17,12 +17,17 @@ module TrueTable
17
17
 
18
18
  # Returns a row or a column
19
19
  def [](key)
20
- key.is_a?(Symbol) || key.is_a?(String) ? col(key.to_sym) : super
20
+ key.is_a?(Symbol) ? col(key) : super
21
21
  end
22
22
 
23
23
  # Adds or updates a row or a column
24
24
  def []=(key, value)
25
- key.is_a?(Symbol) || key.is_a?(String) ? add_col(key.to_sym, value) : super
25
+ key.is_a?(Symbol) ? add_col(key.to_sym, value) : super
26
+ end
27
+
28
+ # Returns a deep copy of self
29
+ def clone
30
+ self.class.new map(&:clone)
26
31
  end
27
32
 
28
33
  # Returns a column as Array
@@ -69,7 +74,7 @@ module TrueTable
69
74
  # Extracts nested value. Accepts row, column or column, row
70
75
  def dig(*indexes)
71
76
  key = indexes.shift
72
- if key.is_a?(Symbol) || key.is_a?(String)
77
+ if key.is_a?(Symbol)
73
78
  col(key.to_sym).dig *indexes
74
79
  else
75
80
  row(key).dig *indexes
@@ -84,6 +89,22 @@ module TrueTable
84
89
  # Iterates over rows
85
90
  alias each_row each_with_index
86
91
 
92
+ def fetch(key, *default)
93
+ if key.is_a?(Symbol)
94
+ if headers.include? key
95
+ col(key.to_sym)
96
+ elsif default.any?
97
+ default.first
98
+ elsif block_given?
99
+ yield key
100
+ else
101
+ raise IndexError, "row :#{key} does not exist"
102
+ end
103
+ else
104
+ super
105
+ end
106
+ end
107
+
87
108
  # Returns an array of column headers
88
109
  def headers
89
110
  first.keys
@@ -119,6 +140,11 @@ module TrueTable
119
140
  self.class.new super
120
141
  end
121
142
 
143
+ # Returns a new table with the element at `count` as the first element
144
+ def rotate(count = 1)
145
+ self.class.new super
146
+ end
147
+
122
148
  # Returns a row
123
149
  alias row []
124
150
 
@@ -128,6 +154,11 @@ module TrueTable
128
154
  end
129
155
  alias filter select
130
156
 
157
+ # Returns a new shuffled tables
158
+ def shuffle(*args)
159
+ self.class.new super
160
+ end
161
+
131
162
  # Returns a new sorted table
132
163
  def sort
133
164
  self.class.new super
@@ -143,6 +174,12 @@ module TrueTable
143
174
  join(row_separator, col_separator, with_headers: true)
144
175
  end
145
176
 
177
+ # Returns a hash representation of the table using the values of the
178
+ # first column as hash keys
179
+ def to_h
180
+ map { |row| [row.values.first, row] }.to_h
181
+ end
182
+
146
183
  # Returns only values, without any headers (array of arrays)
147
184
  def values
148
185
  map { |row| row.values }
@@ -1,3 +1,3 @@
1
1
  module TrueTable
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: true_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-03 00:00:00.000000000 Z
11
+ date: 2020-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple and intuitive tabular data type
14
14
  email: db@dannyben.com
@@ -32,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: 2.4.0
35
+ version: 2.7.0
36
36
  required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="