tblr 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +32 -3
- data/Rakefile +7 -0
- data/lib/tblr/row.rb +54 -0
- data/lib/tblr/table.rb +41 -0
- data/lib/tblr/version.rb +1 -1
- data/lib/tblr.rb +2 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/tblr/table_row_spec.rb +45 -0
- data/spec/tblr/table_spec.rb +45 -0
- data/tblr.gemspec +1 -0
- metadata +33 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f60f49c7fa3131f9e0d18b76d5b35f599df0c4a
|
4
|
+
data.tar.gz: 3b3e869406b4196d4476cdbaca861ea78099edac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84f78e0953421b3c66b37cb64477fc24a45086e3d006c5768fe519001d8851bd4b3e1abd8704018124017f56ffa7b6a238bef161d2e048465be67a13420bab53
|
7
|
+
data.tar.gz: 91691e957e8b820bd38f62602b750efe92712d3e6ef9699c6962b2404fc4f1c23321b5849afa810dcefcfdb57b95912a0d848311334183f8ec64eded267fc91f
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Tblr
|
2
2
|
|
3
|
-
|
3
|
+
Table is a really little gem that gives you a table data type
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,12 +20,41 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
create a table
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
|
+
headers = ['firstName', 'last_name']
|
27
|
+
rows = [
|
28
|
+
%w(Barny Rubble),
|
29
|
+
%w(Fred Flinstone),
|
30
|
+
%w(Wilma Flinstone),
|
31
|
+
]
|
32
|
+
@table = Table.new(headers, rows)
|
33
|
+
```
|
34
|
+
|
35
|
+
Do things with your table
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
@table.collect(&:firstName) #=> ['Rubble', 'Fred', 'Wilma']
|
39
|
+
@table.size #=> 3
|
40
|
+
@table[0] #=> #<Tblr::Row: @headers=["firstName", "last_name"], @row=["First", "Last"]>
|
41
|
+
|
42
|
+
@table.group_by(&:last_name) #=>
|
43
|
+
# {
|
44
|
+
# "Rubble" => #<Tblr::Table: @headers=["firstName", "last_name"],
|
45
|
+
# @rows=[["Barny", "Rubble"]]>,
|
46
|
+
# "Flinstone" => #<Tblr::Table: @headers=["firstName", "last_name"],
|
47
|
+
# @rows=[["Fred", "Flinstone"], ["Wilma", "Flinstone"]]>
|
48
|
+
# }
|
49
|
+
```
|
50
|
+
|
51
|
+
*Note*: Table includes Enumerable, so you can do all those things too.
|
52
|
+
|
53
|
+
##Contributing
|
26
54
|
|
27
55
|
1. Fork it ( https://github.com/[my-github-username]/tblr/fork )
|
28
56
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
2. Writes Specs, pull requrests will not be accepted without tests.
|
29
58
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
59
|
4. Push to the branch (`git push origin my-new-feature`)
|
31
60
|
5. Create a new Pull Request
|
data/Rakefile
CHANGED
data/lib/tblr/row.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Tblr
|
2
|
+
|
3
|
+
module StringRefinements
|
4
|
+
refine String do
|
5
|
+
def camelize(first_letter = :upper)
|
6
|
+
result = self.split("_").map{|s| s.capitalize! }.join("")
|
7
|
+
case first_letter
|
8
|
+
when :upper; result
|
9
|
+
when :lower; result[0].downcase + result[1..-1]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Row
|
16
|
+
using StringRefinements
|
17
|
+
|
18
|
+
def initialize(*args)
|
19
|
+
@headers = args[0]
|
20
|
+
@row = args[1]
|
21
|
+
end
|
22
|
+
|
23
|
+
def ==(another_row)
|
24
|
+
headers == another_row.headers &&
|
25
|
+
row == another_row.row
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_a
|
29
|
+
row
|
30
|
+
end
|
31
|
+
|
32
|
+
def data_for_header(name)
|
33
|
+
idx = :nope
|
34
|
+
camel = name.camelize(:lower)
|
35
|
+
if headers.include?(name)
|
36
|
+
idx = headers.index(name)
|
37
|
+
elsif headers.include?(camel)
|
38
|
+
idx = headers.index(camel)
|
39
|
+
end
|
40
|
+
|
41
|
+
idx == :nope ? :nope : row[idx]
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing(sym, *args, &block)
|
45
|
+
data = data_for_header(sym.to_s)
|
46
|
+
data == :nope ? super(sym, *args, &block) : data
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
attr_accessor :headers, :row
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/lib/tblr/table.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Tblr
|
2
|
+
class Table
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :headers
|
6
|
+
|
7
|
+
def initialize(*args)
|
8
|
+
@headers = args[0]
|
9
|
+
@rows = args[1]
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(a_table)
|
13
|
+
headers == a_table.headers &&
|
14
|
+
rows == a_table.rows
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](idx)
|
18
|
+
Tblr::Row.new(headers, rows[idx])
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
@rows.each_with_index do |row, idx|
|
23
|
+
yield(Tblr::Row.new(headers, rows[idx]))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def group_by(*args, &block)
|
28
|
+
hash = self.map{|row| row}.group_by(*args, &block)
|
29
|
+
hash.each{|key, data| hash[key] = self.class.new(headers, data.map(&:to_a))}
|
30
|
+
end
|
31
|
+
|
32
|
+
def size
|
33
|
+
rows.size
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
attr_reader :rows
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
data/lib/tblr/version.rb
CHANGED
data/lib/tblr.rb
CHANGED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
|
18
|
+
require 'tblr'
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
=begin
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
55
|
+
# For more details, see:
|
56
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
57
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
58
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
59
|
+
config.disable_monkey_patching!
|
60
|
+
|
61
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
62
|
+
# be too noisy due to issues in dependencies.
|
63
|
+
config.warnings = true
|
64
|
+
|
65
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
66
|
+
# file, and it's useful to allow more verbose output when running an
|
67
|
+
# individual spec file.
|
68
|
+
if config.files_to_run.one?
|
69
|
+
# Use the documentation formatter for detailed output,
|
70
|
+
# unless a formatter has already been configured
|
71
|
+
# (e.g. via a command-line flag).
|
72
|
+
config.default_formatter = 'doc'
|
73
|
+
end
|
74
|
+
|
75
|
+
# Print the 10 slowest examples and example groups at the
|
76
|
+
# end of the spec run, to help surface which specs are running
|
77
|
+
# particularly slow.
|
78
|
+
config.profile_examples = 10
|
79
|
+
|
80
|
+
# Run specs in random order to surface order dependencies. If you find an
|
81
|
+
# order dependency and want to debug it, you can fix the order by providing
|
82
|
+
# the seed, which is printed after each run.
|
83
|
+
# --seed 1234
|
84
|
+
config.order = :random
|
85
|
+
|
86
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
87
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
88
|
+
# test failures related to randomization by passing the same `--seed` value
|
89
|
+
# as the one that triggered the failure.
|
90
|
+
Kernel.srand config.seed
|
91
|
+
=end
|
92
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
include Tblr
|
2
|
+
describe Tblr::Row do
|
3
|
+
|
4
|
+
before do
|
5
|
+
headers = ['firstName', 'last_name']
|
6
|
+
row = ['First', 'Last']
|
7
|
+
@row = Row.new(headers, row)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#==' do
|
11
|
+
it 'should be equal to another' do
|
12
|
+
expect(@row).to eq Row.new(%w(firstName last_name), %w(First Last))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#to_a' do
|
17
|
+
it 'should translate row to array' do
|
18
|
+
expect(@row.to_a).to eq %w(First Last)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#missing_methods' do
|
23
|
+
it 'should be able to get row data by argument' do
|
24
|
+
expect(@row.first_name).to eq 'First'
|
25
|
+
expect(@row.last_name).to eq 'Last'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should raise error if row doesnt contain the header' do
|
29
|
+
expect{@row.missing_name}.to raise_error NoMethodError
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should work if value is nil' do
|
33
|
+
row = Row.new(%w(firstName last_name), ['First', nil])
|
34
|
+
expect( row.last_name).to eq nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#data_for_header' do
|
39
|
+
it 'should return data from row for specific header' do
|
40
|
+
expect( @row.data_for_header('first_name')).to eq 'First'
|
41
|
+
expect( @row.data_for_header('last_name')).to eq 'Last'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
include Tblr
|
2
|
+
describe Tblr::Table do
|
3
|
+
|
4
|
+
before do
|
5
|
+
headers = ['firstName', 'last_name']
|
6
|
+
rows = [
|
7
|
+
%w(First Last),
|
8
|
+
%w(Fred Flinstone),
|
9
|
+
%w(Wilma Flinstone),
|
10
|
+
]
|
11
|
+
@table = Table.new(headers, rows)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#each' do
|
15
|
+
it 'should support enumerable module' do
|
16
|
+
expect(@table.map(&:firstName)).to eq %w(First Fred Wilma)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#size' do
|
21
|
+
it 'should return number of rows in table' do
|
22
|
+
expect(@table.size).to eq 3
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#[idx]' do
|
27
|
+
it 'should return idx row' do
|
28
|
+
expect(@table[0]).to eq Row.new(%w(firstName last_name), %w(First Last) )
|
29
|
+
expect(@table[1]).to eq Row.new(%w(firstName last_name), %w(Fred Flinstone) )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#group_by' do
|
34
|
+
it 'should return an array of tables' do
|
35
|
+
expected = {
|
36
|
+
'Last' => Table.new(%w(firstName last_name), [%w(First Last)]),
|
37
|
+
'Flinstone' => Table.new(%w(firstName last_name), [
|
38
|
+
%w(Fred Flinstone), %w(Wilma Flinstone)
|
39
|
+
])
|
40
|
+
}
|
41
|
+
expect(@table.group_by(&:last_name)).to eq expected
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/tblr.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tblr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
@@ -14,30 +14,44 @@ dependencies:
|
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.7'
|
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
26
|
version: '1.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
41
55
|
description: Initialize a Tblr::Table with headers and rows, do things with the table
|
42
56
|
email:
|
43
57
|
- benguest@gmail.com
|
@@ -45,13 +59,19 @@ executables: []
|
|
45
59
|
extensions: []
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
|
-
- .gitignore
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
49
64
|
- Gemfile
|
50
65
|
- LICENSE.txt
|
51
66
|
- README.md
|
52
67
|
- Rakefile
|
53
68
|
- lib/tblr.rb
|
69
|
+
- lib/tblr/row.rb
|
70
|
+
- lib/tblr/table.rb
|
54
71
|
- lib/tblr/version.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/tblr/table_row_spec.rb
|
74
|
+
- spec/tblr/table_spec.rb
|
55
75
|
- tblr.gemspec
|
56
76
|
homepage: ''
|
57
77
|
licenses:
|
@@ -63,18 +83,21 @@ require_paths:
|
|
63
83
|
- lib
|
64
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
85
|
requirements:
|
66
|
-
- -
|
86
|
+
- - ">="
|
67
87
|
- !ruby/object:Gem::Version
|
68
88
|
version: '0'
|
69
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
90
|
requirements:
|
71
|
-
- -
|
91
|
+
- - ">="
|
72
92
|
- !ruby/object:Gem::Version
|
73
93
|
version: '0'
|
74
94
|
requirements: []
|
75
95
|
rubyforge_project:
|
76
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.4.5
|
77
97
|
signing_key:
|
78
98
|
specification_version: 4
|
79
99
|
summary: Little gem to support talbes
|
80
|
-
test_files:
|
100
|
+
test_files:
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/tblr/table_row_spec.rb
|
103
|
+
- spec/tblr/table_spec.rb
|