atv 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTQxMzBkNjk2OGE2NzFlOWNjMjhiOGM5MTNiZjI4MzcwNWJkY2QwOQ==
5
+ data.tar.gz: !binary |-
6
+ ZDljZWRkNGM5Mjc5MzY3ZWJjNDE4NGM0NDNiNzkzODIzMTA5YTVhZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OWQ0NjZiYmY5MzE2MWQwNTU3MTJlMzM1MzU1MGUzZDBhNTNiOTY3OWFiNWEw
10
+ NTQ5ZjgzODk2ZmViMGVlYmM3MmJiN2YzNjUyNDg2YTIwNWI0ODczN2ZlMWM0
11
+ NmJlNmNiM2YwZTBkZGE2NzdmZTg1NjJkODZlOWY4OWFhYzgwNGE=
12
+ data.tar.gz: !binary |-
13
+ YmJkZWZkZTc1ZTUzY2JhNmNiMDkwYWJjYjIzMmZiNzNiNWQ1NjdhYWM2MWRl
14
+ MzE0ZWE5MTMwZTkyZmI5MDdlM2M4MzVjNzBkYzU4ZGU0ODFlYTQ5NDg2NDYy
15
+ ZmNhMjVhY2FlZjNkMmFmNjUyYWRmMDJlNDQxMGUzNTRlYmY0YjA=
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /.idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in atv.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kelly Felkins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # ATV: Ascii Table Values
2
+
3
+ ATV is a reader for data in ascii table format.
4
+ It allows you to read data formatted like this:
5
+
6
+ <pre>
7
+ |------------------+--------------------|
8
+ | name | dob |
9
+ |------------------+--------------------|
10
+ | Malcolm Reynolds | September 20, 2468 |
11
+ |------------------+--------------------|
12
+ | Zoe Washburne | February 15, 2484 |
13
+ |------------------+--------------------|
14
+ </pre>
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'atv'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install atv
31
+
32
+ ## Usage
33
+
34
+ Rows are returned or yielded as [CSV::Row][1] objects.
35
+
36
+ <pre>
37
+ |-----------------------+------------------------------+-----------------------------------|
38
+ | **Ascii Table Value** | **Returned Value** | **Notes** |
39
+ |-----------------------+------------------------------+-----------------------------------|
40
+ | Malcolm Reynolds | "Malcolm Reynolds" | Most values returned as string |
41
+ |-----------------------+------------------------------+-----------------------------------|
42
+ | 123 | "123" | including numbers |
43
+ |-----------------------+------------------------------+-----------------------------------|
44
+ | wrapped strings are | "wrapped strings are folded" | Similar to yaml, wrapped |
45
+ | folded | | strings are folded with a single |
46
+ | | | space replacing the new line |
47
+ |-----------------------+------------------------------+-----------------------------------|
48
+ | | not available | The CSV::Row object will not |
49
+ | | | include columns with blank values |
50
+ |-----------------------+------------------------------+-----------------------------------|
51
+ | null | nil | null, true, and false are |
52
+ | | | special values |
53
+ |-----------------------+------------------------------+-----------------------------------|
54
+ | true | true | |
55
+ |-----------------------+------------------------------+-----------------------------------|
56
+ | false | false | |
57
+ |-----------------------+------------------------------+-----------------------------------|
58
+ </pre>
59
+
60
+ **Reading a String**
61
+
62
+ ```ruby
63
+ atv = ATV.from_string(string)
64
+ atv.each do |row|
65
+ # use row here...
66
+ end
67
+ ```
68
+
69
+ **Reading from am IO object**
70
+
71
+ ```ruby
72
+ atv = ATV.new(io)
73
+ atv.each do |row|
74
+ # use row here...
75
+ end
76
+ ```
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/IndieKelly/atv/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
85
+
86
+ [1]: http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/CSV/Row.html
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/atv.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'atv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "atv"
8
+ spec.version = Atv::VERSION
9
+ spec.authors = ["Kelly Felkins"]
10
+ spec.email = ["kelly@indiegogo.com"]
11
+ spec.summary = "Read Ascii Table Values"
12
+ spec.description = "Read data from an ascii table. Inspired by Ruby's CSV library."
13
+ spec.homepage = "https://github.com/IndieKelly/atv"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Atv
2
+ VERSION = "0.0.6"
3
+ end
data/lib/atv.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "atv/version"
2
+ require 'csv'
3
+
4
+ class ATV
5
+ include Enumerable
6
+
7
+ SUBSTITUTIONS = {
8
+ 'true' => true,
9
+ 'false' => false,
10
+ 'null' => nil
11
+ }
12
+
13
+ attr_reader(:headers)
14
+
15
+ def initialize(io)
16
+ @io = io
17
+ @io.readline
18
+ @headers = split_table_line(@io.readline.chomp)
19
+ @io.readline
20
+ end
21
+
22
+ def each
23
+ line_data = []
24
+ @io.each_line do |line|
25
+ line.chomp!
26
+ if line =~ /^\|\-/
27
+ csv_row = CSV::Row.new(@headers, line_data.
28
+ transpose.
29
+ map { |tokens| tokens.reject(&:empty?).join(' ') }.
30
+ map { |token| SUBSTITUTIONS.has_key?(token) ? SUBSTITUTIONS[token] : token }
31
+ ).delete_if {|k, v| v == ''}
32
+ yield csv_row if csv_row.size > 0
33
+ line_data = []
34
+ next
35
+ end
36
+ line_data << split_table_line(line)
37
+ end
38
+ end
39
+
40
+ def self.from_string(string)
41
+ self.new(StringIO.new(string))
42
+ end
43
+
44
+ protected
45
+
46
+ def split_table_line(line)
47
+ return [] if line.empty?
48
+ line[1..-1].split('|').map(&:strip)
49
+ end
50
+ end
data/test/atv_test.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/atv'
3
+
4
+ describe ATV do
5
+ DATA_AS_TABLE = <<-TEXT
6
+ |-----------+--------------------+--------------|
7
+ | name | dob | predictable? |
8
+ |-----------+--------------------+--------------|
9
+ | Malcolm | September 20, 2468 | false |
10
+ | Reynolds | | |
11
+ |-----------+--------------------+--------------|
12
+ | Zoe | February 15, 2484 | |
13
+ | Washburne | | |
14
+ |-----------+--------------------+--------------|
15
+ | Derrial | null | true |
16
+ | Book | | |
17
+ |-----------+--------------------+--------------|
18
+
19
+ TEXT
20
+
21
+ EXPECTED = [
22
+ ['Malcolm Reynolds', 'September 20, 2468', false],
23
+ ['Zoe Washburne', 'February 15, 2484'],
24
+ ['Derrial Book', nil, true]
25
+ ]
26
+
27
+ describe '#each' do
28
+ before do
29
+ @atv = ATV.new(StringIO.new(DATA_AS_TABLE))
30
+ end
31
+
32
+ it 'with a block it yields rows of data as CSV rows' do
33
+ i = 0
34
+
35
+ @atv.each do |row|
36
+ row.fields.must_equal(EXPECTED[i])
37
+ row[0].must_equal(EXPECTED[i][0])
38
+ row['name'].must_equal(EXPECTED[i][0])
39
+ row[1].must_equal(EXPECTED[i][1])
40
+ row['dob'].must_equal(EXPECTED[i][1])
41
+ row[2].must_equal(EXPECTED[i][2])
42
+ row['predictable?'].must_equal(EXPECTED[i][2])
43
+ i += 1
44
+ end
45
+ i.must_equal(3)
46
+ end
47
+
48
+ it 'without a block it returns an enumerator of data as CSV rows' do
49
+ enum = @atv.enum_for
50
+ enum.map(&:fields).must_equal(EXPECTED)
51
+ end
52
+ end
53
+
54
+ describe "#headers" do
55
+ it 'returns all the headers' do
56
+ ATV.new(StringIO.new(DATA_AS_TABLE)).headers.must_equal(%w|name dob predictable?|)
57
+ end
58
+ end
59
+
60
+ describe ".from_string(string)" do
61
+ it 'initializes ATV with an IO object' do
62
+ fussy = -> (io) { io.must_be_kind_of StringIO }
63
+
64
+ ATV.stub :new, fussy do
65
+ ATV.from_string(DATA_AS_TABLE)
66
+ end
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Felkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Read data from an ascii table. Inspired by Ruby's CSV library.
42
+ email:
43
+ - kelly@indiegogo.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - atv.gemspec
54
+ - lib/atv.rb
55
+ - lib/atv/version.rb
56
+ - test/atv_test.rb
57
+ homepage: https://github.com/IndieKelly/atv
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Read Ascii Table Values
81
+ test_files:
82
+ - test/atv_test.rb