loady 0.8.0 → 0.8.2

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: cf18fef23de1f5e6fdaf2105cd8fb869f1c34242
4
- data.tar.gz: dc657ab3d90455b7e4aa7d590b65ca50e6587cc5
3
+ metadata.gz: ecc3df9a14b6e88e79402ef6f27f4f5fa9a451bf
4
+ data.tar.gz: 2d3fed1bdaed9630f5f518eeeab491fbf4aa041d
5
5
  SHA512:
6
- metadata.gz: c4d2a56c7ce7798837c3eb97994b0d8d0d2ae876e266ba9cc5d98dabe8242a428d93bec1db1809589fb882e6535efbacb1f11374138c9581e8bc9d5a239f6201
7
- data.tar.gz: 5bd7240dbfbcb811af20be78f60078b35bfe2e7930b3fe1fbb5656f044bf748832bf671b4c91fa050a9b203d7981d13a839ed08f7377638173aaeab78bb587b2
6
+ metadata.gz: f0f459bcfacda6e99aeef4a7850e07db3722506c9142ac56f19a16db97e2f71486ebdc80487d47fc1e5639f16c6cb085526672d672bac99121bed08f5073e75f
7
+ data.tar.gz: 21b249d660c826e3ccfeb4503681bb40105c7ff97e7bccd6f37091a0ba539d3f95656a45b524727a10727f55537f4eeea9f39ccfb92e00e37e8ec63e732ef73a
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tee Parham
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 CHANGED
@@ -1,11 +1,13 @@
1
- [![Gem Version](https://badge.fury.io/rb/loady.png)](http://badge.fury.io/rb/loady)
2
- [![Build Status](https://api.travis-ci.org/teeparham/loady.png)](https://travis-ci.org/teeparham/loady)
3
- [![Code Climate](https://codeclimate.com/github/teeparham/loady.png)](https://codeclimate.com/github/teeparham/loady)
4
- [![Coverage Status](https://coveralls.io/repos/teeparham/loady/badge.png)](https://coveralls.io/r/teeparham/loady)
1
+ # Loady
5
2
 
6
- # Loady - A file loader with simple logging
3
+ [![Gem Version](http://img.shields.io/gem/v/loady.svg)](http://rubygems.org/gem/loady)
4
+ [![Build Status](http://img.shields.io/travis/teeparham/loady.svg)](https://travis-ci.org/teeparham/loady)
5
+ [![Code Climate](http://img.shields.io/codeclimate/github/teeparham/loady.svg)](https://codeclimate.com/github/teeparham/loady)
6
+ [![Coverage Status](http://img.shields.io/coveralls/teeparham/loady.svg)](https://coveralls.io/r/teeparham/loady)
7
7
 
8
- Loady is a simple file reader and logger. Use it to quickly load any delimited file, continue on error rows, and do basic logging.
8
+ Loady is a simple file reader and logger. Use it to read any delimited file. Loady makes it easy to conveniently convert input fields to an attribute hash, continue on error rows, and do basic logging.
9
+
10
+ Loady was initially created to load hundreds of millions of rows from CSV files that had various data errors.
9
11
 
10
12
  It works with MRI ruby 1.9.3+. It uses ruby's CSV library to parse rows.
11
13
 
@@ -64,3 +66,21 @@ Loady.read "/your/file.tab", col_sep: "\t" do |row|
64
66
  end
65
67
  ```
66
68
 
69
+ ### Collect log messages in memory
70
+
71
+ `Loady::MemoryLoader` is a class that collects log messages in memory.
72
+
73
+ ``` ruby
74
+ memory_logger = Loady::MemoryLogger.new
75
+
76
+ Loady.read "/your/file.csv", logger: memory_logger do |row|
77
+ # do things
78
+ end
79
+
80
+ memory_logger.messages
81
+ => [
82
+ 'Line 123: Something bad happened.',
83
+ 'Line 456: Exception of some sort.',
84
+ 'Finished. Loaded 9998 rows. 2 unprocessed rows.'
85
+ ]
86
+ ```
@@ -9,15 +9,15 @@ module Loady
9
9
  # options:
10
10
  # strip: false -- default = true
11
11
  # -- array values must be strings if :strip is true
12
- def to_attributes(names, options={})
12
+ def to_attributes(names, options = {})
13
13
  options = { strip: true }.merge(options)
14
14
 
15
15
  attr_hash = {}
16
16
 
17
17
  names.each_with_index do |name, i|
18
18
  attr_hash[name] =
19
- if i < self.size && self[i]
20
- options[:strip] ? self[i].strip : self[i]
19
+ if i < size && self[i]
20
+ options[:strip] ? self[i].to_s.strip : self[i]
21
21
  end
22
22
  end
23
23
 
@@ -14,18 +14,18 @@ module Loady
14
14
  # logger: Logger.new('/somewhere/file.log') -- default = Logger.new(STDOUT)
15
15
  # plus any valid options you can pass to CSV.new:
16
16
  # see http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html#M000190
17
- def read(filename, options={}, &block)
17
+ def read(filename, options = {}, &block)
18
18
  @logger = options.delete(:logger) || default_logger
19
19
 
20
- f = File.new(filename)
21
- f.autoclose = true
22
- f.gets if options.delete(:skip_first_row)
20
+ file = File.new(filename)
21
+ file.autoclose = true
22
+ file.gets if options.delete(:skip_first_row)
23
23
 
24
- f.each do |line|
24
+ file.each do |line|
25
25
  readline line, options, &block
26
26
  end
27
27
 
28
- @logger.info "Finished. Loaded #@success rows. #@warning unprocessed rows."
28
+ @logger.info "Finished. Loaded #{@success} rows. #{@warning} unprocessed rows."
29
29
  end
30
30
 
31
31
  class << self
@@ -46,7 +46,7 @@ module Loady
46
46
  end
47
47
  rescue Exception => ex
48
48
  @warning += 1
49
- @logger.warn "#{ex.to_s.gsub("line 1", "line #@line_number")}\n#{line}"
49
+ @logger.warn "#{ex.to_s.gsub("line 1", "line #{@line_number}")}\n#{line}"
50
50
  end
51
51
 
52
52
  def default_logger
@@ -2,7 +2,7 @@ module Loady
2
2
  class MemoryLogger
3
3
  attr_reader :messages
4
4
 
5
- def initialize(options={})
5
+ def initialize(options = {})
6
6
  @messages = []
7
7
  end
8
8
 
@@ -10,7 +10,7 @@ module Loady
10
10
  @messages << message
11
11
  end
12
12
 
13
- alias_method :warn, :info
14
- alias_method :error, :info
13
+ alias warn info
14
+ alias error info
15
15
  end
16
16
  end
data/lib/loady/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Loady
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.2"
3
3
  end
metadata CHANGED
@@ -1,83 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loady
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tee Parham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-14 00:00:00.000000000 Z
11
+ date: 2014-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mocha
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.0'
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
- version: '0'
26
+ version: '1.0'
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
- version: '0'
33
+ version: '10.1'
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
- version: '0'
40
+ version: '10.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '5.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description: CSV file loader with simple logging
54
+ version: '5.2'
55
+ description: File loader with simple logging
56
56
  email:
57
57
  - tee@neighborland.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".ruby-version"
64
- - ".travis.yml"
65
- - Gemfile
62
+ - LICENSE.txt
66
63
  - README.md
67
- - Rakefile
68
64
  - lib/loady.rb
69
65
  - lib/loady/attribute_array.rb
70
66
  - lib/loady/csv_loader.rb
71
67
  - lib/loady/memory_logger.rb
72
68
  - lib/loady/version.rb
73
- - loady.gemspec
74
- - test/csv/file1.csv
75
- - test/csv/file2.csv
76
- - test/csv/file3.dat
77
- - test/test_attribute_array.rb
78
- - test/test_csv_loader.rb
79
- - test/test_helper.rb
80
- - test/test_memory_logger.rb
81
69
  homepage: http://github.com/teeparham/loady
82
70
  licenses:
83
71
  - MIT
@@ -98,15 +86,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
86
  version: '0'
99
87
  requirements: []
100
88
  rubyforge_project:
101
- rubygems_version: 2.2.1
89
+ rubygems_version: 2.4.4
102
90
  signing_key:
103
91
  specification_version: 4
104
- summary: CSV file loader with simple logging
105
- test_files:
106
- - test/csv/file1.csv
107
- - test/csv/file2.csv
108
- - test/csv/file3.dat
109
- - test/test_attribute_array.rb
110
- - test/test_csv_loader.rb
111
- - test/test_helper.rb
112
- - test/test_memory_logger.rb
92
+ summary: File loader with simple logging
93
+ test_files: []
94
+ has_rdoc:
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- pkg/*
2
- *.gem
3
- .bundle
4
- .idea
5
- Gemfile.lock
6
- *#
7
- .#*
8
- *~
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.1.0
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- group :test do
4
- gem 'coveralls', require: false
5
- end
6
-
7
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- task default: [:test]
5
-
6
- Rake::TestTask.new(:test) do |t|
7
- t.libs << 'test'
8
- t.test_files = %w(test/**/*.rb)
9
- t.verbose = false
10
- end
data/loady.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require "./lib/loady/version"
3
-
4
- Gem::Specification.new do |s|
5
- s.name = "loady"
6
- s.version = Loady::VERSION
7
- s.platform = Gem::Platform::RUBY
8
- s.authors = ["Tee Parham"]
9
- s.email = %w(tee@neighborland.com)
10
- s.homepage = "http://github.com/teeparham/loady"
11
- s.summary = %q{CSV file loader with simple logging}
12
- s.description = %q{CSV file loader with simple logging}
13
- s.license = "MIT"
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = []
18
- s.require_paths = %w(lib)
19
-
20
- s.required_ruby_version = '>= 1.9.3'
21
-
22
- s.add_development_dependency "mocha"
23
- s.add_development_dependency "rake"
24
- s.add_development_dependency "minitest"
25
- end
data/test/csv/file1.csv DELETED
@@ -1,11 +0,0 @@
1
- monkey name,best year
2
- Bubbles,2000
3
- Grape Ape,1975
4
- Albert,1948
5
- Koko,1971
6
- Curious George,1941
7
- Clyde,1975
8
- Bear,1980
9
- Mighty Joe Young,1998
10
- Donkey Kong,1981
11
- King Kong,1933
data/test/csv/file2.csv DELETED
@@ -1,15 +0,0 @@
1
- "Bubbles","2000"
2
- "Grape Ape",1975
3
- Albert,1948,note: everything is read as a string whether it is quoted or not
4
- Koko,1971
5
- Curious George,1941
6
- blah.......,
7
- ",,,,,,,,,,,,,,,,,,,,this is a bad row because of the quote
8
- ",,,,,,,,,,,",,,/"\",,,,,,this is a bad row too
9
-
10
- " blank rows are skipped, this is bad though
11
-
12
- Bear,1980,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"this is ok"
13
- Mighty Joe Young
14
- Donkey Kong,1981
15
- King Kong,1933
data/test/csv/file3.dat DELETED
@@ -1,11 +0,0 @@
1
- monkey name best year
2
- Bubbles 2000
3
- Grape Ape 1975
4
- Albert 1948
5
- Koko 1971
6
- Curious George 1941
7
- Clyde 1975
8
- Bear 1980
9
- Mighty Joe Young 1998
10
- Donkey Kong 1981
11
- King Kong 1933
@@ -1,33 +0,0 @@
1
- require 'test_helper'
2
-
3
- class AttributeArrayTest < MiniTest::Spec
4
- it "respond to to_attributes" do
5
- aa = Loady::AttributeArray.new
6
- assert aa.respond_to? :to_attributes
7
- end
8
-
9
- it "initialize with Array" do
10
- aa = Loady::AttributeArray.new([1,2])
11
- assert_equal [1,2], aa
12
- end
13
-
14
- describe "#to_attributes" do
15
- it "return named attributes" do
16
- row = Loady::AttributeArray.new(['Bubbles ', '2000', ' King Kong '])
17
- attrs = row.to_attributes [:name, :year, :mom]
18
- assert_equal attrs.size, 3
19
- assert_equal attrs[:name], 'Bubbles'
20
- assert_equal attrs[:year], '2000'
21
- assert_equal attrs[:mom], 'King Kong'
22
- end
23
-
24
- it "return named attributes when missing values" do
25
- row = Loady::AttributeArray.new(['Bubbles ', '2000'])
26
- attrs = row.to_attributes [:name, :year, :mom]
27
- assert_equal attrs.size, 3
28
- assert_equal attrs[:name], 'Bubbles'
29
- assert_equal attrs[:year], '2000'
30
- assert_nil attrs[:mom]
31
- end
32
- end
33
- end
@@ -1,62 +0,0 @@
1
- require 'test_helper'
2
-
3
- class CsvLoaderTest < MiniTest::Spec
4
- it "delegate Loady.csv to instance #read" do
5
- Loady::CsvLoader.any_instance.expects :read
6
- Loady.csv("file")
7
- end
8
-
9
- it "delegate Loady.read to instance #read" do
10
- Loady::CsvLoader.any_instance.expects :read
11
- Loady.read("file")
12
- end
13
-
14
- it "delegate #read to instance #read" do
15
- Loady::CsvLoader.any_instance.expects :read
16
- Loady::CsvLoader.read("file")
17
- end
18
-
19
- it "read file1" do
20
- monkeys = []
21
-
22
- Loady.read "test/csv/file1.csv", skip_first_row: true do |row|
23
- monkeys << { name: row[0], year: row[1] }
24
- end
25
-
26
- assert_equal monkeys.count, 10, "total rows read"
27
- assert_equal monkeys[0][:name], "Bubbles", "first row name"
28
- assert_equal monkeys[0][:year], "2000", "first row year"
29
- assert_equal monkeys[9][:name], "King Kong", "last row name"
30
- assert_equal monkeys[9][:year], "1933", "last row year"
31
- end
32
-
33
- it "read file2 with logger using named attributes" do
34
- logger = Logger.new("/dev/null")
35
- monkeys = []
36
-
37
- Loady.read "test/csv/file2.csv", logger: logger do |row|
38
- monkeys << row.to_attributes([:name, :year])
39
- end
40
-
41
- assert_equal monkeys.count, 10, "total rows read"
42
- assert_equal monkeys[0][:name], "Bubbles", "first row name"
43
- assert_equal monkeys[0][:year], "2000", "first row year"
44
- assert_equal monkeys[9][:name], "King Kong", "last row name"
45
- assert_equal monkeys[9][:year], "1933", "last row year"
46
- end
47
-
48
- it "read file3, a tab-delimited file" do
49
- monkeys = []
50
-
51
- Loady.read "test/csv/file3.dat", skip_first_row: true, col_sep: "\t" do |row|
52
- monkeys << { name: row[0], year: row[1] }
53
- end
54
-
55
- assert_equal monkeys.count, 10, "total rows read"
56
- assert_equal monkeys[0][:name], "Bubbles", "first row name"
57
- assert_equal monkeys[0][:year], "2000", "first row year"
58
- assert_equal monkeys[9][:name], "King Kong", "last row name"
59
- assert_equal monkeys[9][:year], "1933", "last row year"
60
- end
61
-
62
- end
data/test/test_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- require 'coveralls'
2
- Coveralls.wear!
3
-
4
- require 'minitest/autorun'
5
- require 'mocha/setup'
6
- require 'loady'
@@ -1,28 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MemoryLoggerTest < MiniTest::Spec
4
- describe "memory logger" do
5
- before do
6
- @logger = Loady::MemoryLogger.new
7
- end
8
-
9
- it "have no messages" do
10
- assert_empty @logger.messages
11
- end
12
-
13
- it "log warning" do
14
- @logger.warn "message"
15
- assert_equal "message", @logger.messages.first
16
- end
17
-
18
- it "log info" do
19
- @logger.info "message"
20
- assert_equal "message", @logger.messages.first
21
- end
22
-
23
- it "log error" do
24
- @logger.error "message"
25
- assert_equal "message", @logger.messages.first
26
- end
27
- end
28
- end