tabelr 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,8 @@ module Tabelr
7
7
  end
8
8
 
9
9
  def go json, output
10
+ raise 'aw' if output.respond_to? :Append
11
+
10
12
  parse json
11
13
  analyse
12
14
  dump output
@@ -22,12 +24,19 @@ module Tabelr
22
24
  end
23
25
 
24
26
  def parse json
25
- # header
26
- json['data'].first.each_key { |key| stash key }
27
- bank
27
+ @header = true
28
+ json.each_value { |h| parse_hash h }
29
+ end
30
+
31
+ def parse_hash hash
32
+ if @header
33
+ hash.first.each_key { |key| stash key }
34
+ bank
35
+ @header = false
36
+ end
28
37
 
29
38
  # content/row
30
- json['data'].each do |line|
39
+ hash.each do |line|
31
40
  line.each_value { |value| stash value }
32
41
  bank
33
42
  end
@@ -43,16 +52,15 @@ module Tabelr
43
52
  end
44
53
 
45
54
  def dump output
46
- output.printf divider
55
+ output << divider
47
56
  @lines.each_with_index do |line, index|
48
- output.printf divider if index == 1
57
+ output << divider if index == 1
49
58
  line.each_with_index do |item, i|
50
- output.printf format item.to_s, @max_len[i]
59
+ output << format(item.to_s, @max_len[i])
51
60
  end
52
- output.printf "|\n"
61
+ output << "|\n"
53
62
  end
54
- output.printf divider
55
- output.flush
63
+ output << divider
56
64
  end
57
65
 
58
66
  def max a, b
data/lib/tabelr.rb CHANGED
@@ -6,12 +6,13 @@ module Tabelr
6
6
 
7
7
  def run
8
8
  args = parse_args
9
+ return unless valid? args
10
+
9
11
  @tf = TableFormator.new
10
12
 
11
13
  json = JSON.parse(File.read(args[0]))
12
- output = STDOUT
13
- output = File.open(args[1], 'w') unless args[1].nil?
14
-
14
+ # output = STDOUT
15
+ output = $stdout
15
16
  @tf.go json, output
16
17
  end
17
18
 
@@ -23,5 +24,17 @@ module Tabelr
23
24
  commands
24
25
  end
25
26
 
27
+ def valid? args
28
+ if args.nil? or args[0].nil?
29
+ puts "Man, you gotta give me a file to read from!"
30
+ return
31
+ end
32
+ unless File.exist?(args[0])
33
+ puts "Man, you gotta give me a valid file to read from!"
34
+ return
35
+ end
36
+ true
37
+ end
38
+
26
39
  end
27
40
  end
data/tabelr.gemspec CHANGED
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
2
2
  s.name = "tabelr"
3
3
  s.summary = "Creates neat aligned ascii tables from a json input"
4
4
  s.description = "Creates neat aligned ascii tables from a json input"
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
  s.author = "Ian Vaughan"
7
7
  s.email = "tabelr@ianvaughan.co.uk"
8
8
  s.homepage = "http://ianvaughan.co.uk"
@@ -0,0 +1,56 @@
1
+ {
2
+ "data":
3
+ [
4
+ {
5
+ "column1": "This",
6
+ "column2": "is",
7
+ "column3": "quite",
8
+ "column4": "cool!"
9
+ },
10
+ {
11
+ "column1": "This",
12
+ "column2": "is the longest field",
13
+ "column3": "by far",
14
+ "column4": 1241
15
+ },
16
+ {
17
+ "column1": "Short",
18
+ "column2": "fields",
19
+ "column3": "fit",
20
+ "column4": "in"
21
+ },
22
+ {
23
+ "column1": "other",
24
+ "column2": "eg",
25
+ "column3": null,
26
+ "column4": 33221199999999991111211111111111
27
+ }
28
+ ],
29
+ "doesnt matter":
30
+ [
31
+ {
32
+ "column1": "This",
33
+ "column2": "is",
34
+ "column3": "quite",
35
+ "column4": "cool!"
36
+ },
37
+ {
38
+ "column1": "This",
39
+ "column2": "is the longest field",
40
+ "column3": "by far",
41
+ "column4": 1241
42
+ },
43
+ {
44
+ "column1": "Short",
45
+ "column2": "fields",
46
+ "column3": "fit",
47
+ "column4": "in"
48
+ },
49
+ {
50
+ "column1": "other---------------------",
51
+ "column2": "eg",
52
+ "column3": null,
53
+ "column4": 3322119999999999
54
+ }
55
+ ]
56
+ }
data/test/example2.txt ADDED
@@ -0,0 +1,12 @@
1
+ +----------------------------+----------------------+---------+----------------------------------+
2
+ | column1 | column2 | column3 | column4 |
3
+ +----------------------------+----------------------+---------+----------------------------------+
4
+ | This | is | quite | cool! |
5
+ | This | is the longest field | by far | 1241 |
6
+ | Short | fields | fit | in |
7
+ | other | eg | | 33221199999999991111211111111111 |
8
+ | This | is | quite | cool! |
9
+ | This | is the longest field | by far | 1241 |
10
+ | Short | fields | fit | in |
11
+ | other--------------------- | eg | | 3322119999999999 |
12
+ +----------------------------+----------------------+---------+----------------------------------+
data/test/test_all.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "test/unit"
2
+ require "./test/test_tabelr"
3
+ require "./test/test_table_formater"
data/test/test_tabelr.rb CHANGED
@@ -1,11 +1,63 @@
1
1
  require "test/unit"
2
+ require 'stringio'
2
3
  require_relative '../lib/tabelr'
3
4
 
4
5
  class TestTabelr < Test::Unit::TestCase
5
- def test_file
6
+
7
+ def test_output
8
+ stdout_orig = $stdout
9
+ $stdout = StringIO.new
10
+ yield if block_given?
11
+ result = $stdout.string.dup
12
+ $stdout = stdout_orig
13
+ result
14
+ end
15
+
16
+ # run
17
+
18
+ def test_run
6
19
  ARGV[0] = 'test/example.json'
7
- ARGV[1] = 'test/test.run'
8
- Tabelr.run
9
- assert_equal File.read('test/output.txt'), File.read(ARGV[1])
20
+ result = test_output do
21
+ Tabelr.run
22
+ end
23
+ assert_equal File.read('test/example.txt'), result
24
+ end
25
+
26
+ # #parse-args
27
+
28
+ def test_parse_args_no_args
29
+ ARGV.clear
30
+ result = Tabelr.parse_args
31
+ assert_equal [], result
32
+ end
33
+
34
+ def test_parse_args_one_arg
35
+ ARGV[0] = ''
36
+ result = Tabelr.parse_args
37
+ assert_equal [''], result
10
38
  end
39
+
40
+ # valid
41
+
42
+ def test_valid_with_no_args
43
+ result = test_output do
44
+ Tabelr.valid? nil
45
+ end
46
+ assert_equal "Man, you gotta give me a file to read from!\n", result
47
+ end
48
+
49
+ def test_valid_one_invalid_arg
50
+ result = test_output do
51
+ Tabelr.valid? ['']
52
+ end
53
+ assert_equal "Man, you gotta give me a valid file to read from!\n", result
54
+ end
55
+
56
+ def test_valid_with_one_valid_arg
57
+ result = test_output do
58
+ Tabelr.valid? [__FILE__]
59
+ end
60
+ assert_equal "", result
61
+ end
62
+
11
63
  end
@@ -0,0 +1,31 @@
1
+ require "test/unit"
2
+ require_relative '../lib/tabelr/table_formater'
3
+ require 'json'
4
+
5
+ class TestTabelr < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @tf = Tabelr::TableFormator.new
9
+ @input = JSON.parse(File.read('test/example.json'))
10
+ end
11
+
12
+ def test_no_file
13
+ # ARGV[1] = 'test/test.run'
14
+ output = ''
15
+ @tf.go @input, output
16
+ assert_equal output, File.read('test/example.txt')
17
+ end
18
+
19
+ def test_larger_hash
20
+ # ARGV[0] = 'test/example2.json'
21
+ # ARGV[1] = 'test/test.run'
22
+ # Tabelr.run
23
+ # assert_equal File.read('test/example2.txt'), File.read(ARGV[1])
24
+ # File.delete ARGV[1]
25
+ end
26
+
27
+ # def test_no_file
28
+ # Tabelr.run
29
+ # assert_equal "No file"
30
+ # end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabelr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-26 00:00:00.000000000 Z
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Creates neat aligned ascii tables from a json input
15
15
  email: tabelr@ianvaughan.co.uk
@@ -25,9 +25,12 @@ files:
25
25
  - stash
26
26
  - tabelr.gemspec
27
27
  - test/example.json
28
- - test/output.txt
29
- - test/test.run
28
+ - test/example.txt
29
+ - test/example2.json
30
+ - test/example2.txt
31
+ - test/test_all.rb
30
32
  - test/test_tabelr.rb
33
+ - test/test_table_formater.rb
31
34
  homepage: http://ianvaughan.co.uk
32
35
  licenses: []
33
36
  post_install_message:
@@ -53,4 +56,6 @@ signing_key:
53
56
  specification_version: 3
54
57
  summary: Creates neat aligned ascii tables from a json input
55
58
  test_files:
59
+ - test/test_all.rb
56
60
  - test/test_tabelr.rb
61
+ - test/test_table_formater.rb
data/test/test.run DELETED
@@ -1,8 +0,0 @@
1
- +---------+----------------------+---------+------------------+
2
- | column1 | column2 | column3 | column4 |
3
- +---------+----------------------+---------+------------------+
4
- | This | is | quite | cool! |
5
- | This | is the longest field | by far | 1241 |
6
- | Short | fields | fit | in |
7
- | other | eg | | 3322119999999999 |
8
- +---------+----------------------+---------+------------------+
File without changes