Skip to content

Commit

Permalink
Merge b9dee06 into 03a9a04
Browse files Browse the repository at this point in the history
  • Loading branch information
teeparham committed May 8, 2019
2 parents 03a9a04 + b9dee06 commit 22e898d
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 92 deletions.
16 changes: 14 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
inherit_from: .rubocop_todo.yml

Style/AlignParameters:
AllCops:
TargetRubyVersion: 2.4

Layout/AlignParameters:
Enabled: false

Style/MultilineOperationIndentation:
Layout/MultilineOperationIndentation:
Enabled: false

Metrics/LineLength:
Max: 100

Style/SignalException:
EnforcedStyle: only_raise

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/FrozenStringLiteralComment:
Exclude:
- 'Gemfile'
- 'Rakefile'
- '*.gemspec'
46 changes: 3 additions & 43 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-01-29 13:14:26 -0700 using RuboCop version 0.35.1.
# on 2019-05-08 16:58:55 -0600 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/RescueException:
Exclude:
- 'lib/loady/csv_loader.rb'

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
Lint/UnusedMethodArgument:
Exclude:
- 'lib/loady/csv_loader.rb'
- 'lib/loady/memory_logger.rb'

# Offense count: 9
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 97

# Offense count: 1
# Configuration parameters: CountComments.
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 13

# Offense count: 3
# Cop supports --auto-correct.
Style/Alias:
Exclude:
- 'lib/loady.rb'
- 'lib/loady/memory_logger.rb'

# Offense count: 5
# Configuration parameters: Exclude.
# Offense count: 4
Style/Documentation:
Exclude:
- 'spec/**/*'
Expand All @@ -46,30 +27,9 @@ Style/Documentation:
- 'lib/loady/attribute_array.rb'
- 'lib/loady/csv_loader.rb'
- 'lib/loady/memory_logger.rb'
- 'lib/loady/version.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
Exclude:
- 'Gemfile'

# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/SpaceInsideStringInterpolation:
Exclude:
- 'lib/loady/csv_loader.rb'

# Offense count: 26
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/StringLiterals:
Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/StringLiteralsInInterpolation:
Enabled: false
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: ruby
sudo: false
cache: bundler
rvm:
- 2.4.0
- 2.3.3
- 2.2.6
- 2.1
- 2.6
- 2.5
- 2.4
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"

if ENV['TRAVIS']
gem 'coveralls', require: false
if ENV["TRAVIS"]
gem "coveralls", require: false
end

gemspec
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "rake/testtask"
task default: [:test]

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.test_files = %w(test/**/*.rb)
t.libs << "test"
t.test_files = %w[test/**/*.rb]
t.verbose = false
end
8 changes: 5 additions & 3 deletions lib/loady.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'loady/attribute_array'
require 'loady/csv_loader'
require 'loady/memory_logger'
# frozen_string_literal: true

require "loady/attribute_array"
require "loady/csv_loader"
require "loady/memory_logger"

module Loady
def read(*args, &block)
Expand Down
2 changes: 2 additions & 0 deletions lib/loady/attribute_array.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Loady
class AttributeArray < Array
# usage:
Expand Down
22 changes: 12 additions & 10 deletions lib/loady/csv_loader.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'csv'
require 'logger'
# frozen_string_literal: true

require "csv"
require "logger"

module Loady
class CsvLoader
Expand All @@ -19,12 +21,12 @@ def read(filename, options = {}, &block)
options[:headers] ||= options.delete(:skip_first_row)

CSV.foreach(filename, options) do |line|
readline line, options, &block
readline line, &block
end
@logger.info "Finished. Loaded #{ @success } rows. #{ @warning } skipped rows."
rescue CSV::MalformedCSVError => ex
@logger.error ex.message
@logger.error "Stopped Loading after #{ @success } rows. #{ @warning } skipped rows."
@logger.info "Finished. Loaded #{@success} rows. #{@warning} skipped rows."
rescue CSV::MalformedCSVError => e
@logger.error e.message
@logger.error "Stopped Loading after #{@success} rows. #{@warning} skipped rows."
end

class << self
Expand All @@ -35,7 +37,7 @@ def read(*args, &block)

private

def readline(line, options)
def readline(line)
@line_number += 1
row = if line.respond_to?(:to_hash)
AttributeArray.new(line.to_hash.values)
Expand All @@ -47,9 +49,9 @@ def readline(line, options)
yield row
@success += 1
end
rescue Exception => ex
rescue StandardError => e
@warning += 1
@logger.warn "#{ ex.to_s.gsub("line 1", "line #{ @line_number }") }\n#{ line }"
@logger.warn "#{e.to_s.gsub('line 1', "line #{@line_number}")}\n#{line}"
end

def default_logger
Expand Down
2 changes: 2 additions & 0 deletions lib/loady/memory_logger.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Loady
class MemoryLogger
attr_reader :messages
Expand Down
4 changes: 3 additions & 1 deletion lib/loady/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Loady
VERSION = "1.0.0".freeze
VERSION = "1.0.0"
end
10 changes: 4 additions & 6 deletions loady.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ require "./lib/loady/version"
Gem::Specification.new do |s|
s.name = "loady"
s.version = Loady::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Tee Parham"]
s.email = %w(tee@neighborland.com)
s.email = %w[tee@neighborland.com]
s.homepage = "https://github.com/teeparham/loady"
s.summary = "File loader with simple logging"
s.description = "File loader with simple logging"
s.license = "MIT"

s.files = Dir["LICENSE.txt", "README.md", "lib/**/*"]
s.executables = []
s.require_paths = %w(lib)
s.require_paths = %w[lib]

s.required_ruby_version = ">= 2.1.0"
s.required_ruby_version = ">= 2.4.0"

s.add_development_dependency "minitest", "~> 5.5"
s.add_development_dependency "mocha", "~> 1.1"
s.add_development_dependency "rake", "~> 12.0"
s.add_development_dependency "minitest", "~> 5.5"
end
22 changes: 12 additions & 10 deletions test/test_attribute_array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'test_helper'
# frozen_string_literal: true

require "test_helper"

class AttributeArrayTest < MiniTest::Spec
it "respond to to_attributes" do
Expand All @@ -13,20 +15,20 @@ class AttributeArrayTest < MiniTest::Spec

describe "#to_attributes" do
it "return named attributes" do
row = Loady::AttributeArray.new(['Bubbles ', '2000', ' King Kong '])
attrs = row.to_attributes [:name, :year, :mom]
row = Loady::AttributeArray.new(["Bubbles ", "2000", " King Kong "])
attrs = row.to_attributes %i[name year mom]
assert_equal attrs.size, 3
assert_equal attrs[:name], 'Bubbles'
assert_equal attrs[:year], '2000'
assert_equal attrs[:mom], 'King Kong'
assert_equal attrs[:name], "Bubbles"
assert_equal attrs[:year], "2000"
assert_equal attrs[:mom], "King Kong"
end

it "return named attributes when missing values" do
row = Loady::AttributeArray.new(['Bubbles ', '2000'])
attrs = row.to_attributes [:name, :year, :mom]
row = Loady::AttributeArray.new(["Bubbles ", "2000"])
attrs = row.to_attributes %i[name year mom]
assert_equal attrs.size, 3
assert_equal attrs[:name], 'Bubbles'
assert_equal attrs[:year], '2000'
assert_equal attrs[:name], "Bubbles"
assert_equal attrs[:year], "2000"
assert_nil attrs[:mom]
end
end
Expand Down
9 changes: 6 additions & 3 deletions test/test_csv_loader.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'test_helper'
# frozen_string_literal: true

require "test_helper"

class CsvLoaderTest < MiniTest::Spec
it "delegate Loady.csv to instance #read" do
Expand Down Expand Up @@ -38,7 +40,7 @@ class CsvLoaderTest < MiniTest::Spec
monkeys = []

Loady.read "test/csv/file2.csv", logger: logger do |row|
monkeys << row.to_attributes([:name, :year])
monkeys << row.to_attributes(%i[name year])
end

assert_equal 10, monkeys.count, "total rows read"
Expand All @@ -47,7 +49,8 @@ class CsvLoaderTest < MiniTest::Spec
assert_equal "King Kong", monkeys[9][:name], "last row name"
assert_equal "1933", monkeys[9][:year], "last row year"
assert_equal 2, logger.messages.size
assert_equal "Unclosed quoted field on line 13.", logger.messages.first
# error message changed from "on" to "in" in ruby 2.6
assert_equal "Unclosed quoted field in line 13.", logger.messages.first.sub(" on", " in")
assert_equal "Stopped Loading after 10 rows. 0 skipped rows.", logger.messages.last
end

Expand Down
12 changes: 7 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
if ENV['TRAVIS']
require 'coveralls'
# frozen_string_literal: true

if ENV["TRAVIS"]
require "coveralls"
Coveralls.wear!
end

require 'minitest/autorun'
require 'mocha/setup'
require 'loady'
require "minitest/autorun"
require "mocha/setup"
require "loady"
4 changes: 3 additions & 1 deletion test/test_memory_logger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'test_helper'
# frozen_string_literal: true

require "test_helper"

class MemoryLoggerTest < MiniTest::Spec
describe "memory logger" do
Expand Down

0 comments on commit 22e898d

Please sign in to comment.