Skip to content

Commit

Permalink
cleanup rubocop config and a bit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 16, 2024
1 parent 6375368 commit db8b5c4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 47 deletions.
31 changes: 2 additions & 29 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ Layout/CaseIndentation:
Layout/EndAlignment:
EnforcedStyleAlignWith: variable

Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Layout/LineLength:
Max: 120

Layout/SpaceBeforeBlockBraces:
EnforcedStyle: no_space
EnforcedStyleForEmptyBraces: no_space

Lint/PercentStringArray: # broken in rubocop 0.55.0
Enabled: false

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
Expand All @@ -41,39 +35,18 @@ Metrics/MethodLength:
Naming/MethodParameterName:
Enabled: false

Style/AccessorGrouping:
Enabled: false

Style/Alias:
EnforcedStyle: prefer_alias_method

Style/EmptyCaseCondition:
Enabled: false

Style/Encoding:
Enabled: false

Style/FileRead:
Style/ArgumentsForwarding:
Enabled: false

Style/HashEachMethods:
Enabled: true

Style/HashTransformKeys:
Enabled: false

Style/HashTransformValues:
Enabled: false

Style/IfUnlessModifier:
Style/EmptyCaseCondition:
Enabled: false

Style/NumericPredicate:
Enabled: false

Style/ParallelAssignment:
Enabled: false

Style/SafeNavigation:
Enabled: false

Expand Down
20 changes: 6 additions & 14 deletions lib/image_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,13 @@ def size_of_gif(ir)
end

def size_of_mng(ir)
unless ir[12, 4] == 'MHDR'
raise FormatError, 'MHDR not in place for MNG'
end
raise FormatError, 'MHDR not in place for MNG' unless ir[12, 4] == 'MHDR'

ir.unpack(16, 8, 'NN')
end

def size_of_png(ir)
unless ir[12, 4] == 'IHDR'
raise FormatError, 'IHDR not in place for PNG'
end
raise FormatError, 'IHDR not in place for PNG' unless ir[12, 4] == 'IHDR'

ir.unpack(16, 8, 'NN')
end
Expand All @@ -178,14 +174,12 @@ def size_of_jpeg(ir)
loop do
offset += 1 until [nil, section_marker].include? ir[offset, 1]
offset += 1 until section_marker != ir[offset + 1, 1]
raise FormatError, 'EOF in JPEG' if ir[offset, 1].nil?
raise FormatError, 'EOF in JPEG' unless ir[offset, 1]

code, length = ir.unpack(offset, 4, 'xCn')
offset += 4

if JPEG_CODE_CHECK.include?(code)
return ir.unpack(offset + 1, 4, 'nn').reverse
end
return ir.unpack(offset + 1, 4, 'nn').reverse if JPEG_CODE_CHECK.include?(code)

offset += length - 2
end
Expand Down Expand Up @@ -250,9 +244,7 @@ def size_of_xbm(ir)
def size_of_xpm(ir)
length = 1024
until (data = ir[0, length]) =~ /"\s*(\d+)\s+(\d+)(\s+\d+\s+\d+){1,2}\s*"/m
if data.length != length
raise FormatError, 'XPM size not found'
end
raise FormatError, 'XPM size not found' if data.length != length

length += 1024
end
Expand Down Expand Up @@ -281,7 +273,7 @@ def size_of_tiff(ir)
tag, type = ifd.unpack(endian2b * 2)
offset += 12

next if packspec[type].nil?
next unless packspec[type]

value = ifd[8, 4].unpack(packspec[type])[0]
case tag
Expand Down
2 changes: 1 addition & 1 deletion spec/image_size/chunky_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def chunk_size
{
'empty string' => '',
'a bit of data' => 'foo bar baz',
'a lot of data' => File.open('GPL', 'rb', &:read),
'a lot of data' => File.binread('GPL'),
}.each do |data_description, data|
{
'default' => test_reader.new(data),
Expand Down
9 changes: 6 additions & 3 deletions spec/image_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ def supported_formats
describe "for #{path}" do
let(:name){ File.basename(path) }
let(:attributes) do
match = /(\d+)x(\d+)\.([^.]+)$/.match(name)
width, height, format = match[1].to_i, match[2].to_i, match[3].to_sym if match
if (match = /(\d+)x(\d+)\.([^.]+)$/.match(name))
width = match[1].to_i
height = match[2].to_i
format = match[3].to_sym
end
size = format && [width, height]
media_types = ImageSize::MEDIA_TYPES[format] || []
media_type = format && media_types.first.to_s
Expand All @@ -78,7 +81,7 @@ def supported_formats
media_types: media_types,
}
end
let(:file_data){ File.open(path, 'rb', &:read) }
let(:file_data){ File.binread(path) }
let(:file_size){ file_data.length }

before do
Expand Down

0 comments on commit db8b5c4

Please sign in to comment.