tablestakes 0.9.1 → 0.9.2

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -18
  3. data/lib/tablestakes.rb +34 -11
  4. data/spec/table_spec.rb +12 -2
  5. metadata +74 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68f8bcf6867af660a8a70c778e6b99c58d87d34b
4
- data.tar.gz: 1be6354ea8b90fb237e3006d9fa75654a9608222
3
+ metadata.gz: 712201016806acdde2ba71077b3b9d22adbb2901
4
+ data.tar.gz: b3a10d461c4676c63ca3913f54785dd1333cf0fe
5
5
  SHA512:
6
- metadata.gz: 4db9e294efa043aac351ae261d8f54bd6f60d2c52985c5c3aa35582c2737aa59ce9d36033ea5cb2b2a19d93ab9b3f7195b5ecbdc47eeef0c67d7cb6714e5128f
7
- data.tar.gz: a0e06d3114a5d7264a140e92aebad9e6de4ebe362fa2dcd22c855302ba5dd114cf2eab26cd53a17fd07540f5911740a7f8e7ef2e7133a75d9eac5b4bc437d64b
6
+ metadata.gz: bb5e169a7636d4b01236b28944b0f07c6f4576615dc587026d5f4e25316f8934e0f172e6393517dc2777537b966cbaa1789fa4304542105e177255f3fb3ea96b
7
+ data.tar.gz: d79187e12a20e858dc1416f65231d3413f31b26285ac17177d850245747fa929c785e57001e44e5204c337a90bf5e6c3977116b75b6cddcb9c7adc06d864046f
data/README.md CHANGED
@@ -2,31 +2,13 @@ Tablestakes
2
2
  ===========
3
3
 
4
4
  [![Gem Version][GV img]][Gem Version]
5
- [![Build Status][BS img]][Build Status]
6
5
  [![Dependency Status][DS img]][Dependency Status]
7
- [![Code Climate][CC img]][Code Climate]
8
- [![Coverage Status][CS img]][Coverage Status]
9
6
 
10
7
  [Gem Version]: https://rubygems.org/gems/tablestakes
11
- <<<<<<< HEAD
12
8
  [Dependency Status]: https://gemnasium.com/jbfolkerts/tablestakes
13
- [Coverage Status]: https://coveralls.io/r/jbfolkerts/tablestakes
14
9
 
15
10
  [GV img]: https://badge.fury.io/rb/tablestakes.png
16
11
  [DS img]: https://gemnasium.com/jbfolkerts/tablestakes.png
17
- =======
18
- [Build Status]: https://travis-ci.org/jbfolkerts/tablestakes
19
- [travis pull requests]: https://travis-ci.org/jbfolkerts/tablestakes/pull_requests
20
- [Dependency Status]: https://gemnasium.com/jbfolkerts/tablestakes
21
- [Code Climate]: https://codeclimate.com/github/jbfolkerts/tablestakes
22
- [Coverage Status]: https://coveralls.io/r/jbfolkerts/tablestakes
23
-
24
- [GV img]: https://badge.fury.io/rb/tablestakes.png
25
- [BS img]: https://travis-ci.org/jbfolkerts/tablestakes.png
26
- [DS img]: https://gemnasium.com/jbfolkerts/tablestakes.png
27
- [CC img]: https://codeclimate.com/github/jbfolkerts/tablestakes.png
28
- >>>>>>> 12f008f26e005a9519dc7cc6664c218fca72a01c
29
- [CS img]: https://coveralls.io/repos/jbfolkerts/tablestakes/badge.png?branch=master
30
12
 
31
13
  Tablestakes is a gem for processing tabular data. It is for people who would rather not meddle with
32
14
  a spreadsheet, or load their data into a SQL database. You get the instant gratification of being
data/lib/tablestakes.rb CHANGED
@@ -172,6 +172,8 @@ class Table
172
172
  return self
173
173
  end
174
174
 
175
+ alias :<< :add_row
176
+
175
177
  # Delete a column from the Table. Raises ArgumentError if the column name does not exist.
176
178
  #
177
179
  # ==== Attributes
@@ -447,30 +449,53 @@ class Table
447
449
 
448
450
 
449
451
  # Given a field/column, and a regular expression to match against, and a replacement string,
450
- # update the table such that it substitutes the column data with the replacement string.
451
- # Returns +nil+ if the column is not found.
452
+ # create a new table which performs a substitute operation on column data. In the case that the
453
+ # given replacement is a +String+, a direct substitute is performed. In the case that it is a +Hash+
454
+ # and the matched text is one of its keys, the corresponding +Hash+ value will be substituted.
455
+ #
456
+ # Optionally takes a block containing an operation to perform on all matching data elements
457
+ # in the given column. Raises ArgumentError if the column is not found.
452
458
  #
453
459
  # ==== Attributes
454
460
  # +colname+:: +String+ to identify the column to join on
455
461
  # +re+:: +Regexp+ to match the value in the selected column
456
- # +replace+:: +String+ to specify the replacement text for the given +Regexp+
462
+ # +replace+:: OPTIONAL +String+ or +Hash+ to specify the replacement text for the given +Regexp+
463
+ # +&block+:: OPTIONAL block to execute against matching values
457
464
  #
458
465
  # ==== Examples
459
466
  # cities.sub("Population", /(.*?),(.*?)/, '\1\2') # eliminate commas
460
467
  # capitals.sub("State", /NY/, "New York") # replace acronym with full name
468
+ # capitals.sub("State") { |state| state.downcase } # Lowercase for all values
461
469
  #
462
- def sub(colname, re, replace)
470
+ def sub(colname, re=nil, replace=nil, &block)
463
471
  # check arguments
464
472
  raise ArgumentError, "No regular expression to match against" unless re
465
- raise ArgumentError, "No replacement string specified" unless replace
466
473
  raise ArgumentError, "Invalid column name" unless @table.has_key?(colname)
467
-
468
- @table[colname].each do |item|
469
- item.sub!(re, replace)
474
+ replace_str = ""
475
+ if replace.respond_to?(:fetch)
476
+ replace_str = replace.fetch(re)
477
+ elsif replace.respond_to?(:to_str)
478
+ replace_str = replace.to_str
479
+ else
480
+ raise ArgumentError, "Replacement must be String or Hash"
470
481
  end
471
- return self
482
+
483
+ result = Table.new([@headers])
484
+ col_index = @headers.index(colname)
485
+
486
+ self.each do |row|
487
+ if block_given?
488
+ row[col_index] = block.call row[col_index]
489
+ else
490
+ row[col_index] = row[col_index].sub(re, replace_str)
491
+ end
492
+ result.add_row(row)
493
+ end
494
+ return result
472
495
  end
473
496
 
497
+ # alias :sub! :sub
498
+
474
499
  # Return Array with the union of elements columns in the given tables, eliminating duplicates.
475
500
  # Raises an ArgumentError if a column is not found.
476
501
  #
@@ -511,8 +536,6 @@ class Table
511
536
  return self.column(colname) & table2.column(col2name)
512
537
  end
513
538
 
514
- alias :sub! :sub
515
-
516
539
  # Sort the table based on given column. Uses precedence as defined in the
517
540
  # column. By default will sort by the value in the first column.
518
541
  #
data/spec/table_spec.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  #
3
3
  #
4
4
  require 'spec_helper'
5
+ require 'rspec/its'
5
6
  require_relative '../lib/tablestakes'
6
7
 
7
8
 
@@ -205,7 +206,7 @@ describe "Table" do
205
206
  expect((t.select("Name","Address","Records")).headers).to eq(["Name","Address","Records"])
206
207
  end
207
208
  it "does not select columns that are not given as arguments" do
208
- expect((t.select("Name","Address","Records")).headers.include?("Phone")).to be_false
209
+ expect((t.select("Name","Address","Records")).headers.include?("Phone")).to eq(false)
209
210
  end
210
211
  it "raise ArgumentError when the given arguments don't match a column" do
211
212
  expect { t.select("Silly") }.to raise_error(ArgumentError)
@@ -243,11 +244,20 @@ describe "Table" do
243
244
  expect(cities.sub("State", /Jersey/, "York")).to be_a(Table)
244
245
  end
245
246
  it "substitutes the values in a given field" do
246
- expect((cities.sub("State", /Jersey/, "York")).count("State", "New York")).to eq(9)
247
+ expect(cities.sub("State", /Jersey/, "York").column("State")).to include("New York")
247
248
  end
248
249
  it "raises ArgumentError when the given arguments don't match a column" do
249
250
  expect {cities.sub("Silly", /NJ/, "NY") }.to raise_error(ArgumentError)
250
251
  end
252
+ it "raises ArgumentError when not given a Match string" do
253
+ expect {cities.sub("State") }.to raise_error(ArgumentError)
254
+ end
255
+ it "raises ArgumentError when replacement is not a String or Hash" do
256
+ expect {cities.sub("State", /New/, 9)}.to raise_error(ArgumentError)
257
+ end
258
+ it "does not modify the given table" do
259
+ expect(cities.sub("State", /New/, "Old") && cities.column("State")).to include("New York")
260
+ end
251
261
  end
252
262
 
253
263
  describe ".union" do
metadata CHANGED
@@ -1,71 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablestakes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - J.B. Folkerts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-06 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.14.0
19
+ version: '3.2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.14.0
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-its
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-expectations
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
27
69
  - !ruby/object:Gem::Dependency
28
70
  name: factory_girl
29
71
  requirement: !ruby/object:Gem::Requirement
30
72
  requirements:
31
- - - '>='
73
+ - - "~>"
32
74
  - !ruby/object:Gem::Version
33
- version: 4.4.0
75
+ version: '4.4'
34
76
  type: :development
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
- - - '>='
80
+ - - "~>"
39
81
  - !ruby/object:Gem::Version
40
- version: 4.4.0
82
+ version: '4.4'
41
83
  - !ruby/object:Gem::Dependency
42
84
  name: simplecov
43
85
  requirement: !ruby/object:Gem::Requirement
44
86
  requirements:
45
- - - '>='
87
+ - - "~>"
46
88
  - !ruby/object:Gem::Version
47
- version: 0.8.2
89
+ version: '0.9'
48
90
  type: :development
49
91
  prerelease: false
50
92
  version_requirements: !ruby/object:Gem::Requirement
51
93
  requirements:
52
- - - '>='
94
+ - - "~>"
53
95
  - !ruby/object:Gem::Version
54
- version: 0.8.2
96
+ version: '0.9'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: coveralls
57
99
  requirement: !ruby/object:Gem::Requirement
58
100
  requirements:
59
- - - '>='
101
+ - - "~>"
60
102
  - !ruby/object:Gem::Version
61
- version: 0.7.0
103
+ version: '0.7'
62
104
  type: :development
63
105
  prerelease: false
64
106
  version_requirements: !ruby/object:Gem::Requirement
65
107
  requirements:
66
- - - '>='
108
+ - - "~>"
67
109
  - !ruby/object:Gem::Version
68
- version: 0.7.0
110
+ version: '0.7'
69
111
  description: A simple implementation of Tables, for use in summing, joining, slicing
70
112
  and dicing data tables
71
113
  email: jbf@pentambic.com
@@ -73,7 +115,12 @@ executables: []
73
115
  extensions: []
74
116
  extra_rdoc_files: []
75
117
  files:
76
- - lib/tablestakes.rb
118
+ - README.md
119
+ - capitals.sorted
120
+ - capitals.txt
121
+ - cities.txt
122
+ - doc/OrderedRow.html
123
+ - doc/Table.html
77
124
  - doc/created.rid
78
125
  - doc/images/add.png
79
126
  - doc/images/arrow_up.png
@@ -105,20 +152,15 @@ files:
105
152
  - doc/js/jquery.js
106
153
  - doc/js/navigation.js
107
154
  - doc/js/search.js
108
- - doc/js/searcher.js
109
155
  - doc/js/search_index.js
110
- - doc/OrderedRow.html
156
+ - doc/js/searcher.js
111
157
  - doc/rdoc.css
112
- - doc/Table.html
113
158
  - doc/table_of_contents.html
114
- - README.md
115
- - test.tab
116
- - cities.txt
117
- - capitals.txt
118
- - capitals.sorted
159
+ - lib/tablestakes.rb
119
160
  - spec/factories.rb
120
- - spec/table_spec.rb
121
161
  - spec/spec_helper.rb
162
+ - spec/table_spec.rb
163
+ - test.tab
122
164
  homepage: http://rubygems.org/gems/tablestakes
123
165
  licenses:
124
166
  - MIT
@@ -129,17 +171,17 @@ require_paths:
129
171
  - lib
130
172
  required_ruby_version: !ruby/object:Gem::Requirement
131
173
  requirements:
132
- - - '>='
174
+ - - ">="
133
175
  - !ruby/object:Gem::Version
134
176
  version: '0'
135
177
  required_rubygems_version: !ruby/object:Gem::Requirement
136
178
  requirements:
137
- - - '>='
179
+ - - ">="
138
180
  - !ruby/object:Gem::Version
139
181
  version: '0'
140
182
  requirements: []
141
183
  rubyforge_project:
142
- rubygems_version: 2.0.14
184
+ rubygems_version: 2.2.2
143
185
  signing_key:
144
186
  specification_version: 4
145
187
  summary: Implementation of in-memory Tables
@@ -151,3 +193,4 @@ test_files:
151
193
  - spec/factories.rb
152
194
  - spec/table_spec.rb
153
195
  - spec/spec_helper.rb
196
+ has_rdoc: