Skip to content

Commit

Permalink
Merge pull request #22 from lyrasis/use-rubocop
Browse files Browse the repository at this point in the history
Use rubocop
  • Loading branch information
kspurgin authored Jul 20, 2021
2 parents 08b9d8b + 2c2a673 commit 5c240f8
Show file tree
Hide file tree
Showing 48 changed files with 1,605 additions and 1,460 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AllCops:
TargetRubyVersion: 2.7.3
SuggestExtensions:
rubocop-rake: false
13 changes: 10 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
source "https://rubygems.org"
# frozen_string_literal: true

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'facets', require: false
source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
gem 'csv', '~> 3.0'
gem 'facets', require: false

# Specify your gem's dependencies in kiba-extend.gemspec
gemspec

group :development do
gem 'rubocop', require: false
gem 'rubocop-rspec', require: false
end
27 changes: 26 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
kiba-extend (1.15.0)
kiba-extend (1.15.1)
activesupport
kiba (>= 4.0.0)
kiba-common (>= 1.5.0)
Expand All @@ -16,6 +16,7 @@ GEM
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.2)
coderay (1.1.2)
concurrent-ruby (1.1.9)
csv (3.1.9)
Expand All @@ -28,10 +29,16 @@ GEM
kiba (>= 3.0.0, < 5)
method_source (0.9.2)
minitest (5.14.4)
parallel (1.20.1)
parser (3.0.2.0)
ast (~> 2.4.1)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rainbow (3.0.0)
rake (13.0.1)
regexp_parser (2.1.1)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand All @@ -45,8 +52,24 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rubocop (1.18.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.7.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.8.0)
parser (>= 3.0.1.1)
rubocop-rspec (2.4.0)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-progressbar (1.11.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
xxhash (0.4.0)
zeitwerk (2.4.2)

Expand All @@ -61,6 +84,8 @@ DEPENDENCIES
pry (~> 0.12.2)
rake (~> 13.0)
rspec (~> 3.0)
rubocop
rubocop-rspec

BUNDLED WITH
2.1.4
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
7 changes: 4 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "kiba/extend"
require 'bundler/setup'
require 'kiba/extend'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +11,5 @@ require "kiba/extend"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
48 changes: 25 additions & 23 deletions kiba-extend.gemspec
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "kiba/extend/version"
require 'kiba/extend/version'

Gem::Specification.new do |spec|
spec.name = "kiba-extend"
spec.name = 'kiba-extend'
spec.version = Kiba::Extend::VERSION
spec.authors = ["Kristina Spurgin"]
spec.email = ["[email protected]"]
spec.authors = ['Kristina Spurgin']
spec.email = ['[email protected]']

spec.summary = "Extensions for Kiba ETL"
spec.homepage = "https://github.com/lyrasis/kiba-extend"
spec.license = "MIT"
spec.summary = 'Extensions for Kiba ETL'
spec.homepage = 'https://github.com/lyrasis/kiba-extend'
spec.license = 'MIT'

spec.required_ruby_version = '>=2.7.3'
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/lyrasis/kiba-extend"
spec.metadata["changelog_uri"] = "https://github.com/lyrasis/kiba-extend"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/lyrasis/kiba-extend'
spec.metadata['changelog_uri'] = 'https://github.com/lyrasis/kiba-extend'
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
raise 'RubyGems 2.0 or newer is required to protect against ' \
'public gem pushes.'
end

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency 'activesupport'
spec.add_dependency 'kiba', '>= 4.0.0'
spec.add_dependency 'kiba-common', '>= 1.5.0'
spec.add_dependency 'xxhash'
spec.add_dependency 'activesupport'

spec.add_development_dependency "bundler", ">= 1.17"
spec.add_development_dependency "pry", "~> 0.12.2"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"

spec.add_development_dependency 'bundler', '>= 1.17'
spec.add_development_dependency 'pry', '~> 0.12.2'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
end
21 changes: 11 additions & 10 deletions lib/kiba/extend.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'kiba'
require 'kiba-common/sources/csv'
require 'kiba-common/destinations/csv'
Expand All @@ -6,7 +8,7 @@
require 'xxhash'
require 'facets/kernel/blank'

CSVOPT = {headers: true, header_converters: :symbol}
CSVOPT = { headers: true, header_converters: :symbol }.freeze
DELIM = ';'

# The Kiba ETL framework for Ruby.
Expand All @@ -18,7 +20,7 @@ module Kiba
# Provides a suite of abstract, reusable, well-tested data transformations for use in Kiba ETL pipelines
module Extend
autoload :VERSION, 'extend/version'

puts "kiba-extend version: #{Kiba::Extend::VERSION}"

require 'kiba/extend/fieldset'
Expand All @@ -42,26 +44,26 @@ module Extend
require 'kiba/extend/utils/lookup'

# strips, collapses multiple spaces, removes terminal commas, strips again
CSV::Converters[:stripplus] = lambda{ |s|
CSV::Converters[:stripplus] = lambda { |s|
begin
if s.nil?
nil
elsif s == 'NULL'
nil
else
s.strip
.gsub(/ +/, ' ')
.sub(/,$/, '')
.sub(/^%(LINEBREAK|CRLF)%/, '')
.sub(/%(LINEBREAK|CRLF)%$/, '')
.strip
.gsub(/ +/, ' ')
.sub(/,$/, '')
.sub(/^%(LINEBREAK|CRLF)%/, '')
.sub(/%(LINEBREAK|CRLF)%$/, '')
.strip
end
rescue ArgumentError
s
end
}

CSV::Converters[:nulltonil] = lambda{ |s|
CSV::Converters[:nulltonil] = lambda { |s|
begin
if s == 'NULL'
nil
Expand All @@ -72,6 +74,5 @@ module Extend
s
end
}

end
end
10 changes: 6 additions & 4 deletions lib/kiba/extend/destinations/csv.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# frozen_string_literal: true

require 'csv'

module Kiba
module Extend
module Destinations
class CSV
attr_reader :filename, :csv_options, :csv, :headers

def initialize(filename:, csv_options: nil, headers: nil, initial_headers: [])
@filename = filename
@csv_options = csv_options || {}
@headers = headers
@initial_headers = initial_headers
end

def write(row)
@csv ||= ::CSV.open(filename, 'wb', csv_options)
@headers ||= row.keys
order_headers
@headers_written ||= (csv << headers ; true)
@headers_written ||= (csv << headers; true)
csv << row.fetch_values(*@headers)
end

def close
csv&.close
end
Expand Down
27 changes: 13 additions & 14 deletions lib/kiba/extend/fieldset.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# frozen_string_literal: true

module Kiba
module Extend
class Fieldset
def initialize(fields)
@hash = {}
fields.each{ |field| @hash[field] = [] }
fields.each { |field| @hash[field] = [] }
end

def add_constant_values(field, value)
@hash[field] = []
value_ct.times{ @hash[field] << value }
value_ct.times { @hash[field] << value }
end

def fields
@hash.keys
end

def hash
@hash
end
attr_reader :hash

def join_values(delim)
@hash.transform_values!{ |vals| vals.join(delim) }
@hash.transform_values! { |vals| vals.join(delim) }
end

def populate(rows)
return if rows.empty?

rows.each{ |row| get_field_values(row) }
rows.each { |row| get_field_values(row) }
remove_valueless_rows
end

Expand All @@ -46,18 +46,17 @@ def get_field_values(row)

def remove_valueless_rows
valueless_indices.each do |index|
@hash.each{ |field, values| values.delete_at(index) }
@hash.each { |_field, values| values.delete_at(index) }
end
end

def valueless_indices
indices = []
@hash.values.first.each_with_index do |element, i|
indices << i if @hash.values.map{ |vals| vals[i] }.compact.empty?
@hash.values.first.each_with_index do |_element, i|
indices << i if @hash.values.map { |vals| vals[i] }.compact.empty?
end
indices.sort.reverse
end

end
end
end
3 changes: 3 additions & 0 deletions lib/kiba/extend/transforms/append.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Kiba
module Extend
module Transforms
Expand Down Expand Up @@ -28,6 +30,7 @@ def initialize(field:, value:)
def process(row)
fv = row.fetch(@field, nil)
return row if fv.blank?

row[@field] = "#{fv}#{@value}"
row
end
Expand Down
Loading

0 comments on commit 5c240f8

Please sign in to comment.