Skip to content

Commit

Permalink
use ruby 3.x patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
infused committed Dec 27, 2023
1 parent 232015a commit ed325d4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ require:
- rubocop-rspec
- rubocop-performance

AllCops:
SuggestExtensions: true

Layout/CaseIndentation:
Enabled: false

Expand Down Expand Up @@ -87,6 +90,9 @@ Naming/FileName:
Naming/MethodParameterName:
Enabled: false

Naming/PredicateName:
Enabled: false

RSpec/AnyInstance:
Enabled: false

Expand Down
14 changes: 8 additions & 6 deletions lib/dbf/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class NameError < StandardError
end

attr_reader :table, :name, :type, :length, :decimal

def_delegator :type_cast_class, :type_cast

# rubocop:disable Style/MutableConstant
TYPE_CAST_CLASS = {
N: ColumnType::Number,
I: ColumnType::SignedLong,
Expand All @@ -24,6 +26,7 @@ class NameError < StandardError
G: ColumnType::General,
'+'.to_sym => ColumnType::SignedLong2
}
# rubocop:enable Style/MutableConstant
TYPE_CAST_CLASS.default = ColumnType::String
TYPE_CAST_CLASS.freeze

Expand Down Expand Up @@ -58,7 +61,7 @@ def memo?
#
# @return [Hash]
def to_hash
{name: name, type: type, length: length, decimal: decimal}
{name:, type:, length:, decimal:}
end

# Underscored name
Expand All @@ -68,18 +71,17 @@ def to_hash
#
# @return [String]
def underscored_name
@underscored_name ||= begin
name.gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
end
@underscored_name ||= name.gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase

end

private

def clean(value) # :nodoc:
value.strip.gsub("\x00", '').gsub(/[^\x20-\x7E]/, '')
value.strip.delete("\x00").gsub(/[^\x20-\x7E]/, '')
end

def encode(value, strip_output = false) # :nodoc:
def encode(value, strip_output: false) # :nodoc:
return value unless value.respond_to?(:encoding)

output = @encoding ? encode_string(value) : value
Expand Down
2 changes: 1 addition & 1 deletion lib/dbf/database/foxpro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def process_field(record, data)
end

def table_field_hash(name)
{name: name, fields: []}
{name:, fields: []}
end
end

Expand Down
9 changes: 2 additions & 7 deletions lib/dbf/header.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
module DBF
class Header
attr_reader :version
attr_reader :record_count
attr_reader :header_length
attr_reader :record_length
attr_reader :encoding_key
attr_reader :encoding
attr_reader :version, :record_count, :header_length, :record_length, :encoding_key, :encoding

def initialize(data)
@data = data
unpack_header
end

def unpack_header
@version = @data.unpack('H2').first
@version = @data.unpack1('H2')

case @version
when '02'
Expand Down
2 changes: 1 addition & 1 deletion lib/dbf/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def to_a
private

def column_names # :nodoc:
@column_names ||= @columns.map { |column| column.name }
@column_names ||= @columns.map(&:name)
end

def get_data(column) # :nodoc:
Expand Down
8 changes: 4 additions & 4 deletions lib/dbf/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def find(command, options = {}, &block)
end
end


# @return [TrueClass, FalseClass]
def has_memo_file?
!!@memo
Expand Down Expand Up @@ -278,13 +279,12 @@ def header # :nodoc:
end

def memo_class # :nodoc:
@memo_class ||= begin
if foxpro?
Memo::Foxpro
@memo_class ||= if foxpro?
Memo::Foxpro
else
version == '83' ? Memo::Dbase3 : Memo::Dbase4
end
end

end

def memo_search_path(io) # :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion spec/dbf/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

describe 'if other attributes match' do
let(:attributes) { {x: 1, y: 2} }
let(:other) { instance_double('DBF::Record', attributes: attributes) }
let(:other) { instance_double('DBF::Record', attributes:) }

before do
allow(record).to receive(:attributes).and_return(attributes)
Expand Down

0 comments on commit ed325d4

Please sign in to comment.