duffy 1.0.5 → 1.0.6

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: 73762eee91c5b809fa97cbecf522816e751d80d87ab085d8ba29a7aa0971dd37
4
- data.tar.gz: 41d570f9aa5e93fcf875567f0a689db05770ed58d293aab8997c86860635ec5e
3
+ metadata.gz: 7b26949061663fcfd666bc4479d89671494dfc81d2e14a3bb3189970de202255
4
+ data.tar.gz: e8dc1912140fc07fb39c4f9d15070c26397d91b9cd63fdaa7fc164e889e4cf5c
5
5
  SHA512:
6
- metadata.gz: 68ba4df32be9196d43b56b30c4ec97c39ae6794cac761f3fc2f5cbd622967a65bec9b1b138664569a153c66e4113ed9988f4547910a8e785f82f5a35e62167b8
7
- data.tar.gz: 22616030c9bedd21996236912e0eab43d53ad617c5361b4bff7d8ff20b7d6ec95275e951e16883a601d7d92860f9d50448c8e4b3c968b6455f769db1889edc48
6
+ metadata.gz: 2816bf8e70c972cc7ce9e3066f8d54d9c93cb8e00958e2bd7ada8ab060163958faaeff52ca973af982039994acdcef6a0148b907d2193976ed4cbcf0787bfa11
7
+ data.tar.gz: '065851c6e0a9e97533715eef0800390cd0811c2ecc3453871479be7fb9d85056fbdc4327c54cbf4555819e5aa81b5f82c1007cd94525c316c17bb905f980dae0'
@@ -0,0 +1,17 @@
1
+ name: RSpec
2
+ on: push
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ os: [ubuntu-latest, macos-latest]
9
+ ruby: [2.6, 2.7, '3.0', 3.1]
10
+ runs-on: ${{ matrix.os }}
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
17
+ - run: bundle exec rake
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # Duffy
2
2
 
3
- [![Build Status](https://travis-ci.org/duffyjp/duffy.svg?branch=master)](https://travis-ci.org/duffyjp/duffy)
4
- [![Code Climate](https://codeclimate.com/github/duffyjp/duffy/badges/gpa.svg)](https://codeclimate.com/github/duffyjp/duffy)
5
- [![Test Coverage](https://codeclimate.com/github/duffyjp/duffy/badges/coverage.svg)](https://codeclimate.com/github/duffyjp/duffy/coverage)
6
-
7
3
  This is a collection of reusable things I don't want to keep duplicating in tons of projects.
8
4
 
9
5
  ## Installation
@@ -39,10 +35,10 @@ to_ssn | "123456789" | "123-45-6789"
39
35
 
40
36
 
41
37
  ## Array Patches:
42
- Method | Example | Output
43
- ------------------|------------------------|-------
44
- to_box | ['abc', 123].to_box | ![Example](doc/abc123.png)
45
-
38
+ Method | Example | Output
39
+ ------------------|---------------------------|-------
40
+ to_box | ['abc', 123].to_box | ![Example](doc/abc123.png)
41
+ to_markdown | [[1,2],[3,4]].to_markdown | ![Example](doc/markdown.png)
46
42
 
47
43
  ## Date Patches:
48
44
  * See config to set your organization's fiscal year start.
data/doc/markdown.png ADDED
Binary file
data/lib/duffy/array.rb CHANGED
@@ -14,4 +14,36 @@ class Array
14
14
  out << (b[5,2] + self.join(b[4,3]) + b[4,2])[0,80] + "\n"
15
15
  out << (b[7] + self.map{ |x| b[1] * (x.to_s.gsub(/\e\[([;\d]+)?m/, '').length + 2) }.join(b[8]) + b[9])[0,80]
16
16
  end
17
+
18
+
19
+
20
+ # Create a markdown table from a 2D Array
21
+ #
22
+ # puts [["Car", "Boat", Date.today, 5],["Horse", "Car", nil, 0]].to_markdown
23
+ # String | String | Date | Integer
24
+ # -------|--------|------------|---------
25
+ # Car | Boat | 2022-01-12 | 5
26
+ # Horse | Car | | 0
27
+ #
28
+ def to_markdown
29
+ raise "expected 2D array" unless self.first.is_a?(Array)
30
+
31
+ # Find max widths of data/headers: eg => [10, 25, 18, 21, 6]
32
+ widths = self.transpose.map{ |col| ([col.first.class.to_s.length] + col.map{ |i| i.to_s.length }).max }
33
+
34
+ # Use the class of each cell in the first row for the table headers.
35
+ out = self.first.map.with_index{|c,i| c.class.to_s.ljust(widths[i])}.join(" | ") + "\n"
36
+ out << out.gsub(/[^|]/, "-") + "\n"
37
+
38
+ # Left justify each cell
39
+ self.each do |row|
40
+ out << row.map(&:to_s).map.with_index{|c,i| c.ljust(widths[i])}.join(" | ") + "\n"
41
+ end
42
+ out
43
+
44
+ rescue => e
45
+ warn "Unable to generate table: #{e}"
46
+ nil
47
+ end
48
+
17
49
  end
data/lib/duffy/version.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  module Duffy
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.6'
3
3
  end
4
4
 
5
5
  # History
6
+ # 1.0.6 - Add to_markdown Array method.
6
7
  # 1.0.5 - Update smart_titlecase to better accommodate names.
7
8
  # 1.0.4 - Added battery_percent method.
8
9
  # 1.0.3 - to_box methods now accept ANSI colored strings.
data/spec/array_spec.rb CHANGED
@@ -21,4 +21,33 @@ describe Array do
21
21
  end
22
22
 
23
23
  end
24
+
25
+ describe "to_markdown" do
26
+
27
+ it "creates a markdown compatible table" do
28
+ array = [[1,2,3],[4,5,6],[7,8,9]]
29
+ expect(array.to_markdown).to eq "Integer | Integer | Integer\n--------|---------|---------\n1 | 2 | 3 \n4 | 5 | 6 \n7 | 8 | 9 \n"
30
+ end
31
+
32
+ it "uses the first row for column header names" do
33
+ array = [[1, "a", Date.today]]
34
+ expect(array.to_markdown.lines.first).to eq "Integer | String | Date \n"
35
+ end
36
+
37
+ it "arrays with very short data format correctly" do
38
+ array = [[1,2,3],[4,5,6],[7,8,9]]
39
+ expect(array.to_markdown).to eq "Integer | Integer | Integer\n--------|---------|---------\n1 | 2 | 3 \n4 | 5 | 6 \n7 | 8 | 9 \n"
40
+ end
41
+
42
+ it "rejects non-2D arrays" do
43
+ array = ["Wisconsin", "cheese"]
44
+ expect{array.to_markdown}.to output("Unable to generate table: expected 2D array\n").to_stderr
45
+ end
46
+
47
+ it "rejects arrays with inconsistent dimensions" do
48
+ array = [[1],[2,3],[4,5,6]]
49
+ expect{array.to_markdown}.to output("Unable to generate table: element size differs (2 should be 1)\n").to_stderr
50
+ end
51
+ end
52
+
24
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duffy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Duffy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-20 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,10 +102,10 @@ executables:
102
102
  extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
+ - ".github/workflows/rspec.yml"
105
106
  - ".gitignore"
106
107
  - ".rspec"
107
108
  - ".rubocop.yml"
108
- - ".travis.yml"
109
109
  - Gemfile
110
110
  - LICENSE.txt
111
111
  - README.md
@@ -113,6 +113,7 @@ files:
113
113
  - bin/console
114
114
  - doc/abc.png
115
115
  - doc/abc123.png
116
+ - doc/markdown.png
116
117
  - duffy.gemspec
117
118
  - lib/duffy.rb
118
119
  - lib/duffy/active_record.rb
@@ -139,7 +140,7 @@ homepage: https://github.com/duffyjp/duffy
139
140
  licenses:
140
141
  - MIT
141
142
  metadata: {}
142
- post_install_message:
143
+ post_install_message:
143
144
  rdoc_options: []
144
145
  require_paths:
145
146
  - lib
@@ -154,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  - !ruby/object:Gem::Version
155
156
  version: '0'
156
157
  requirements: []
157
- rubygems_version: 3.0.6
158
- signing_key:
158
+ rubygems_version: 3.2.32
159
+ signing_key:
159
160
  specification_version: 4
160
161
  summary: Library of things
161
162
  test_files:
data/.travis.yml DELETED
@@ -1,18 +0,0 @@
1
- env:
2
- global:
3
- - CC_TEST_REPORTER_ID=d77de389b3b98dc3b4f404875592cde5efbf22d5268880fc8e86c4c323871bb9
4
- language: ruby
5
- rvm:
6
- - 2.6.1
7
-
8
- before_install:
9
- - gem update bundler
10
-
11
- before_script:
12
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
- - chmod +x ./cc-test-reporter
14
- - ./cc-test-reporter before-build
15
- script:
16
- - bundle exec rspec
17
- after_script:
18
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT