vangogh 0.0.1 → 0.1.0

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
  SHA1:
3
- metadata.gz: 80adbf0ea988235079c45eadfedd8665363c1736
4
- data.tar.gz: 58f370396da56f00f2f1c618831df14b6fbdd71c
3
+ metadata.gz: 975a2f5d4d0a1890a6ce46df6c4151829f88f0d4
4
+ data.tar.gz: f0ef17b4ced2a78632e6b24fd5e7facb3a8eab9f
5
5
  SHA512:
6
- metadata.gz: 9922f54a815b3a48f3a88a6c693e18eac872e3eacaaa37efb70148348fd075ea3843db64fad6339f2691a554ba817f7ea5aa8a541557937e1933614808153595
7
- data.tar.gz: b77ca744bde7b0fba3f889f37441cdd3a31fbcaf05ccdea6fb49c7a5a856f759234616d61d3de0660f18d49297e5dd536ccfeea2e2cd2f63b702bdfa6e535f36
6
+ metadata.gz: 12133477da376c8318233cc5b4e352251314311da60d20ca21489a32430c9cd4fcc1c8f0d55997fb26e89d62f951b5149491d44c8358634e9c11b3c27f2f0d71
7
+ data.tar.gz: 8881fefa3ef6e945d8c00cd44c5b70fba0a098ca5ed66967a09a7b2ad5c1ac2ac8c540f96503c7ea22e8e299a5f69b277e2a32dea5a2fa3a81e4e78a51100f48
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
data/README.md CHANGED
@@ -13,7 +13,7 @@ Vangogh.abstract 12345
13
13
  => 12000
14
14
 
15
15
  # Abstract and abbreviate
16
- Vangogh.abbrevaite 1234
16
+ Vangogh.abbreviate 1234
17
17
  => "1.2k"
18
18
 
19
19
  Vangogh.abbreviate 12345
@@ -40,7 +40,21 @@ $ vangogh abstract 1234
40
40
 
41
41
  $ vangogh abbreviate 12345
42
42
  12.3k
43
- ``
43
+ ```
44
+
45
+ ## Abstracting CSVs
46
+
47
+ Vangogh isn't just a single-medium artist. If you pass Vangogh the path to a CSV, it'll process all numeric-looking cells, and pipe the resulting CSV to STDOUT.
48
+
49
+ ```
50
+ $ vangogh abstract csv.csv >> output.csv
51
+ ```
52
+
53
+ If you want to explicitly include/exclude certain fields, you can do so with the `fields` argument.
54
+
55
+ ```
56
+ $ vangogh abstract csv.csv --fields foo bar >> output.csv
57
+ ```
44
58
 
45
59
  ## Contributing
46
60
 
data/bin/vangogh CHANGED
@@ -1,16 +1,62 @@
1
1
  #!/usr/bin/env ruby
2
2
  require_relative "../lib/vangogh"
3
3
  require 'thor'
4
+ require 'csv'
4
5
 
5
6
  class VangoghCli < Thor
6
- desc "abstract VALUE", "Abstract the given value (e.g., 1234 -> 1200)"
7
+
8
+ class_option :fields, :type => :array, :desc => "Fields to act on (default: all numeric fields)"
9
+
10
+ desc "abstract VALUE|CSV [--fields FIELDS]", "Abstract the given value (e.g., 1234 -> 1200)"
7
11
  def abstract(value)
8
- puts Vangogh.abstract(value.to_f)
12
+ if csv?(value)
13
+ puts process_csv(value, :abstract)
14
+ else
15
+ puts Vangogh.abstract(value)
16
+ end
9
17
  end
10
18
 
11
- desc "abbreviate VALUE", "Abbreviate the given value (e.g., 1234 -> 1.2k)"
19
+ desc "abbreviate VALUE|CSV [--fields FIELDS]", "Abbreviate the given value (e.g., 1234 -> 1.2k)"
12
20
  def abbreviate(value)
13
- puts Vangogh.abbreviate(value.to_f)
21
+ if csv?(value)
22
+ puts process_csv(value, :abbreviate)
23
+ else
24
+ puts Vangogh.abbreviate(value)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ no_commands do
31
+ def read_csv(csv)
32
+ CSV.read(csv, :headers => true)
33
+ end
34
+
35
+ def process_csv(path, process)
36
+ raise ArgumentError unless [:abstract, :abbreviate].include?(process)
37
+ fields = options["fields"] || []
38
+
39
+ csv = CSV.generate do |output|
40
+ input = read_csv(path)
41
+ input.each do |row|
42
+ output_row = []
43
+ output << input.headers
44
+ row.each do |key, value|
45
+ if (fields.empty? || fields.include?(key.strip)) && Vangogh.numeric?(value)
46
+ output_row.push Vangogh.send(process, value)
47
+ else
48
+ output_row.push value
49
+ end
50
+ end
51
+ output << output_row
52
+ end
53
+ end
54
+ csv
55
+ end
56
+
57
+ def csv?(value)
58
+ value =~ /\.csv$/ && File.exists?(value)
59
+ end
14
60
  end
15
61
  end
16
62
 
@@ -4,7 +4,11 @@ module Vangogh
4
4
  attr_accessor :value
5
5
 
6
6
  def initialize(value)
7
- @value = value
7
+ if value.is_a?(String)
8
+ @value = value.strip.to_f
9
+ else
10
+ @value = value
11
+ end
8
12
  end
9
13
 
10
14
  def abstract
@@ -1,3 +1,3 @@
1
1
  module Vangogh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/vangogh.rb CHANGED
@@ -11,5 +11,9 @@ module Vangogh
11
11
  def abbreviate(value)
12
12
  Number.new(value).abbreviate
13
13
  end
14
+
15
+ def numeric?(text)
16
+ !!(text.to_s.strip =~ /\A[\d\,.]+\z/)
17
+ end
14
18
  end
15
19
  end
@@ -0,0 +1,2 @@
1
+ foo,bar
2
+ baz,12345
@@ -8,4 +8,18 @@ describe "Vangogh::Bin" do
8
8
  it "abbreviates values" do
9
9
  expect(`bundle exec vangogh abbreviate 1234`.strip).to eql("1.2k")
10
10
  end
11
+
12
+ it "abstracts CSVs" do
13
+ expect(`bundle exec vangogh abstract spec/fixtures/csv.csv`.strip).to match(/baz\,12000$/)
14
+ end
15
+
16
+ it "abbreviates CSVs" do
17
+ expect(`bundle exec vangogh abbreviate spec/fixtures/csv.csv`.strip).to match(/baz\,12k$/)
18
+ end
19
+
20
+ it "respects the field flag" do
21
+ expect(`bundle exec vangogh abbreviate spec/fixtures/csv.csv --fields foo`.strip).to match(/baz\,12345$/)
22
+
23
+ expect(`bundle exec vangogh abbreviate spec/fixtures/csv.csv --fields bar`.strip).to match(/baz\,12k$/)
24
+ end
11
25
  end
data/spec/vangogh_spec.rb CHANGED
@@ -8,4 +8,15 @@ describe Vangogh do
8
8
  it "abbreviates numbers" do
9
9
  expect(subject.abbreviate(1234)).to eql("1.2k")
10
10
  end
11
+
12
+ it "knows when something is nummeric" do
13
+ expect(subject.numeric?(12345)).to eql(true)
14
+ expect(subject.numeric?("12345")).to eql(true)
15
+ expect(subject.numeric?("12,345")).to eql(true)
16
+ expect(subject.numeric?("123.45")).to eql(true)
17
+
18
+ expect(subject.numeric?("123.45MB")).to eql(false)
19
+ expect(subject.numeric?("123.45 foo")).to eql(false)
20
+ expect(subject.numeric?("foo")).to eql(false)
21
+ end
11
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vangogh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-27 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: si
@@ -116,6 +116,7 @@ files:
116
116
  - script/cibuild
117
117
  - script/console
118
118
  - script/release
119
+ - spec/fixtures/csv.csv
119
120
  - spec/helper.rb
120
121
  - spec/vangogh_bin_spec.rb
121
122
  - spec/vangogh_ext_spec.rb
@@ -147,6 +148,7 @@ signing_key:
147
148
  specification_version: 4
148
149
  summary: A Ruby Gem for abstracting numbers
149
150
  test_files:
151
+ - spec/fixtures/csv.csv
150
152
  - spec/helper.rb
151
153
  - spec/vangogh_bin_spec.rb
152
154
  - spec/vangogh_ext_spec.rb