Skip to content

Commit

Permalink
Update rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed Mar 17, 2022
1 parent c61473a commit ed3c041
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 136 deletions.
48 changes: 28 additions & 20 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
inherit_from: .rubocop_todo.yml

require: rubocop-rspec

AllCops:
TargetRubyVersion: 2.3
NewCops: enable
TargetRubyVersion: 2.5
Exclude:
- 'spec/dummy/**/*'

LineLength:
Enabled: false
Layout/LineLength:
Max: 150

FrozenStringLiteralComment:
Enabled: false
RSpec/ExampleLength:
Max: 200

ClassLength:
Enabled: false
Metrics/MethodLength:
Max: 60

RescueModifier:
Enabled: false
Metrics/BlockLength:
Max: 200

AccessorMethodName:
RSpec/FilePath:
Enabled: false

Documentation:
Enabled: false
RSpec/MultipleExpectations:
Max: 50

AbcSize:
Enabled: false
Metrics/PerceivedComplexity:
Max: 15

CyclomaticComplexity:
Enabled: false
Metrics/ClassLength:
Max: 300

PerceivedComplexity:
Metrics/AbcSize:
Enabled: false

MethodLength:
Metrics/CyclomaticComplexity:
Enabled: false

BlockNesting:
Style/Documentation:
Enabled: false

RSpec/DescribeClass:
Naming/FileName:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false
39 changes: 39 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-03-17 17:41:57 UTC using RuboCop version 1.26.0.
# 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: 2
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 252

# Offense count: 1
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Exclude:
- 'lib/activerecord-copy/temp_buffer.rb'

# Offense count: 2
# Configuration parameters: IgnoredMetadata.
RSpec/DescribeClass:
Exclude:
- '**/spec/features/**/*'
- '**/spec/requests/**/*'
- '**/spec/routing/**/*'
- '**/spec/system/**/*'
- '**/spec/views/**/*'
- 'spec/integration_spec.rb'
- 'spec/verify_data_formats_spec.rb'

# Offense count: 2
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, IgnoredMethods.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
EnforcedStyle: unannotated
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
13 changes: 9 additions & 4 deletions activerecord-copy.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
require 'English'

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'activerecord-copy/version'

Expand All @@ -13,10 +14,11 @@ Gem::Specification.new do |gem|
gem.homepage = 'https://github.com/lfittl/activerecord-copy'

gem.license = 'MIT'
gem.files = `git ls-files`.split($/)
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 2.5.0'

gem.add_dependency('activerecord', '>= 3.1')
gem.add_dependency('pg', '>= 1.3.0')
Expand All @@ -25,8 +27,11 @@ Gem::Specification.new do |gem|
gem.add_development_dependency('rspec-core', '>= 2.12.0')
gem.add_development_dependency('rspec-rails')

gem.add_development_dependency('rails', '>= 6.1.4.4')
gem.add_development_dependency('activerecord-postgis-adapter', '~> 7.1')
gem.add_development_dependency('dotenv-rails', '>= 2.7.6')
gem.add_development_dependency('rails', '>= 6.1.4.4')
gem.add_development_dependency('rgeo', '>= 2.4.0')
gem.add_development_dependency('rubocop', '~> 1.26')
gem.add_development_dependency('rubocop-rspec', '~> 2.9.0')
gem.metadata['rubygems_mfa_required'] = 'true'
end
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
db:
image: postgres/postgres
image: postgis/postgis
ports:
- "5577:5432"
environment:
Expand Down
7 changes: 4 additions & 3 deletions lib/activerecord-copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ def copy_from_client(columns, table_name: nil, &block)
table_name ||= quoted_table_name
connection = self.connection.raw_connection
column_types = columns.map do |c|
column = self.columns_hash[c.to_s]
column = columns_hash[c.to_s]
raise format('Could not find column %s on %s', c, self.table_name) if column.nil?

ColumnHelper.find_column_type(column)
end
sql = %{COPY #{table_name}("#{columns.join('","')}") FROM STDIN BINARY}
encoder = ActiveRecordCopy::EncodeForCopy.new(column_types: column_types, connection: connection)
connection.copy_data(sql) do
connection.copy_data(sql) do
encoder.process(&block)
end
end
end
end
end

ActiveRecord::Base.send(:include, ActiveRecordCopy::CopyFromClient)
ActiveRecord::Base.include ActiveRecordCopy::CopyFromClient
5 changes: 3 additions & 2 deletions lib/activerecord-copy/column_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module ActiveRecordCopy
class ColumnHelper
def self.find_column_type(column)
if column.type == :integer
if column.limit == 8
case column.limit
when 8
:bigint
elsif column.limit == 2
when 2
:smallint
else
:integer
Expand Down
53 changes: 23 additions & 30 deletions lib/activerecord-copy/encode_for_copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
require_relative 'mac_address'

module ActiveRecordCopy

class ConnectionWriter
def initialize(connection)
@connection = connection
Expand Down Expand Up @@ -37,11 +36,12 @@ def process(&_block)
end

def <<(row)
self.add(row)
add(row)
end

def add(row)
fail ArgumentError.new('Empty row added') if row.empty?
raise ArgumentError, 'Empty row added' if row.empty?

write(@io, @row_size_encoded)
row.each_with_index do |col, index|
encode_field(@io, col, index)
Expand Down Expand Up @@ -104,15 +104,15 @@ def write_simple_field(io, field, type)
pack_and_write_with_bufsize(io, [field], PACKED_FLOAT_64)
when :time
data = (field - field.beginning_of_day)
pack_and_write_with_bufsize(io, [data.to_i * 1000000], PACKED_UINT_64)
pack_and_write_with_bufsize(io, [data.to_i * 1_000_000], PACKED_UINT_64)
when :timestamp, :timestamptz
data = field.tv_sec * 1_000_000 + field.tv_usec - POSTGRES_EPOCH_TIME
data = (field.tv_sec * 1_000_000) + field.tv_usec - POSTGRES_EPOCH_TIME
pack_and_write_with_bufsize(io, [data.to_i], PACKED_UINT_64)
when :date
data = field.to_date - Date.new(2000, 1, 1)
pack_and_write_with_bufsize(io, [data.to_i], PACKED_UINT_32)
else
raise Exception, "Unsupported simple type: #{type}"
raise StandardError, "Unsupported simple type: #{type}"
end
end

Expand All @@ -125,7 +125,7 @@ def encode_field(io, field, index, depth = 0)
return
end

if field.is_a?(Array) && ![:json, :jsonb].include?(@column_types[index])
if field.is_a?(Array) && !%i[json jsonb].include?(@column_types[index])
encode_array(io, field, index)
return
end
Expand All @@ -136,9 +136,7 @@ def encode_field(io, field, index, depth = 0)
when :uuid
pack_and_write_with_bufsize(io, [field.delete('-')], PACKED_HEX_STRING)
when :inet, :cidr
if String === field
field = IPAddr.new(field)
end
field = IPAddr.new(field) if field.is_a?(String)
encode_ip_addr(io, field)
when :macaddr
write_with_bufsize(io, MacAddress.new(field).to_bytes)
Expand Down Expand Up @@ -175,7 +173,8 @@ def encode_based_on_input(io, field, index, depth)
when String
write_with_bufsize(io, field.encode(UTF_8_ENCODING))
when Hash
raise Exception, "Hash's can't contain hashes" if depth > 0
raise StandardError, "Hash's can't contain hashes" if depth.positive?

hash_io = TempBuffer.new
pack_and_write(hash_io, [field.size], PACKED_UINT_32)
field.each_pair do |key, val|
Expand All @@ -200,12 +199,12 @@ def encode_based_on_input(io, field, index, depth)
when Date
:daterange
else
raise Exception, "Unsupported range input type #{field.begin.class.name} for index #{index}"
raise StandardError, "Unsupported range input type #{field.begin.class.name} for index #{index}"
end

encode_range(io, field, range_type)
else
raise Exception, "Unsupported Format: #{field.class.name}"
raise StandardError, "Unsupported Format: #{field.class.name}"
end
end

Expand All @@ -214,9 +213,9 @@ def encode_array(io, field, index)
field.compact!
case field[0]
when String
pack_and_write(array_io, [1], PACKED_UINT_32)
pack_and_write(array_io, [0], PACKED_UINT_32)
if @column_types[index] == :uuid
pack_and_write(array_io, [1], PACKED_UINT_32) # unknown
pack_and_write(array_io, [0], PACKED_UINT_32) # unknown

pack_and_write(array_io, [UUID_TYPE_OID], PACKED_UINT_32)
pack_and_write(array_io, [field.size], PACKED_UINT_32)
Expand All @@ -226,8 +225,6 @@ def encode_array(io, field, index)
pack_and_write_with_bufsize(array_io, [val.delete('-')], PACKED_HEX_STRING)
end
else
pack_and_write(array_io, [1], PACKED_UINT_32) # unknown
pack_and_write(array_io, [0], PACKED_UINT_32) # unknown

type_oid = @column_types[index] == :text ? TEXT_TYPE_OID : VARCHAR_TYPE_OID
pack_and_write(array_io, [type_oid], PACKED_UINT_32)
Expand All @@ -254,7 +251,7 @@ def encode_array(io, field, index)
pack_and_write(io, [-1], PACKED_UINT_32)
return
else
raise Exception, 'Arrays support int or string only'
raise StandardError, 'Arrays support int or string only'
end

write_with_bufsize(io, array_io.string)
Expand Down Expand Up @@ -301,7 +298,7 @@ def encode_range(io, range, range_type)
when :daterange
:date
else
raise Exception, "Unsupported range type: #{range_type}"
raise StandardError, "Unsupported range type: #{range_type}"
end
flags = 0
flags |= RANGE_LB_INC # Ruby ranges always include the lower bound
Expand All @@ -310,12 +307,8 @@ def encode_range(io, range, range_type)
flags |= RANGE_UB_INF if range.end.respond_to?(:infinite?) && range.end.infinite?
tmp_io = TempBuffer.new
pack_and_write(tmp_io, [flags], PACKED_UINT_8)
if range.begin && (!range.begin.respond_to?(:infinite?) || !range.begin.infinite?)
write_simple_field(tmp_io, range.begin, field_data_type)
end
if range.end && (!range.end.respond_to?(:infinite?) || !range.end.infinite?)
write_simple_field(tmp_io, range.end, field_data_type)
end
write_simple_field(tmp_io, range.begin, field_data_type) if range.begin && (!range.begin.respond_to?(:infinite?) || !range.begin.infinite?)
write_simple_field(tmp_io, range.end, field_data_type) if range.end && (!range.end.respond_to?(:infinite?) || !range.end.infinite?)
write_with_bufsize(io, tmp_io.read)
end

Expand All @@ -330,14 +323,14 @@ def encode_jsonb(io, field)
write_with_bufsize(io, buf)
end

NUMERIC_NBASE = 10000
NUMERIC_NBASE = 10_000
def base10_to_base10000(intval)
digits = []
loop do
newintval = intval / NUMERIC_NBASE
digits << intval - newintval * NUMERIC_NBASE
digits << (intval - (newintval * NUMERIC_NBASE))
intval = newintval
break if intval == 0
break if intval.zero?
end
digits
end
Expand All @@ -351,15 +344,15 @@ def encode_numeric(io, field)
dscale = digits_base10.size - weight_base10

int_part, frac_part = float_str.split('.')
frac_part += '0' * (NUMERIC_DEC_DIGITS - frac_part.size % NUMERIC_DEC_DIGITS) # Add trailing zeroes so digit calculations are correct
frac_part += '0' * (NUMERIC_DEC_DIGITS - (frac_part.size % NUMERIC_DEC_DIGITS)) # Add trailing zeroes so digit calculations are correct

digits_before_decpoint = base10_to_base10000(int_part.to_i)
digits_after_decpoint = base10_to_base10000(frac_part.to_i).reverse

weight = digits_before_decpoint.size - 1
digits = digits_before_decpoint + digits_after_decpoint

pack_and_write(io, [2 * 4 + 2 * digits.size], PACKED_UINT_32) # Field data size
pack_and_write(io, [(2 * 4) + (2 * digits.size)], PACKED_UINT_32) # Field data size
pack_and_write(io, [digits.size], PACKED_UINT_16) # ndigits
pack_and_write(io, [weight], PACKED_UINT_16) # weight
pack_and_write(io, [sign], PACKED_UINT_16) # sign
Expand Down
7 changes: 3 additions & 4 deletions lib/activerecord-copy/mac_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ module ActiveRecordCopy
class MacAddress
def initialize(str)
str = str.strip
if !self.class.validate_strict(str)
raise ArgumentError.new("Invalid MAC address: #{str}")
end
raise ArgumentError, "Invalid MAC address: #{str}" unless self.class.validate_strict(str)

@bytes = str.split(/[-,:]/).map { |s| s.to_i(16) }
end

def to_s
@bytes.map { |h| h.hex }.join(":")
@bytes.map(&:hex).join(':')
end

def to_bytes
Expand Down
3 changes: 1 addition & 2 deletions lib/activerecord-copy/temp_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def write(st)
@st << st.force_encoding(ASCII_8BIT_ENCODING)
end

def rewind
end
def rewind; end

def reopen
@st = ''.force_encoding(ASCII_8BIT_ENCODING)
Expand Down
Loading

0 comments on commit ed3c041

Please sign in to comment.