Skip to content

Commit fae5c0e

Browse files
author
Jon Rogers
committed
Cleanup/refactor
Problem -------- Running `rubocop` indentified a lot of little issues for cleanup. In doing this cleanup, there was some refactoring done that changes the overall API. Deprecation warnings have been added. Solution -------- * Add `Commands` class to pull some command running helpers out of the main module * Don't mix in ImageMagick::Fonts (which removes `MojoMagick.get_fonts` method) * Add deprecation warnings for missing methods * rubocop cleanup * make test methods shorter and better named * Move from `Parser` to `FontParser` and add deprecations * Sequester deprecated methods for easy removal later * Add rubocop check to circle * Update readme to reflect moves
1 parent 91c6cf5 commit fae5c0e

File tree

17 files changed

+318
-251
lines changed

17 files changed

+318
-251
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ jobs:
2222
- run:
2323
name: audit
2424
command: bundle exec bundle-audit update && bundle exec bundle-audit check
25+
- ruby/rubocop-check

.rubocop.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ inherit_from:
55
- ./.rubocop-common.yml
66
- ./.rubocop-performance.yml
77

8-
Metrics/MethodLength:
9-
Exclude:
10-
- "**/*test.rb"
8+
Metrics/CyclomaticComplexity:
9+
Max: 10
10+
Metrics/PerceivedComplexity:
11+
Max: 10

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
source "https://rubygems.org"
22

33
group :test, :development do
4-
gem 'irb', require: false
5-
gem 'simplecov', require: false
4+
gem "irb", require: false
5+
gem "simplecov", require: false
66
end
77

88
gemspec

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ complex commands.
139139

140140
### Get a list of available fonts
141141

142-
fonts = MojoMagick::get_fonts
142+
fonts = MojoMagick::available_fonts
143143

144144
fonts.first
145145
=> #<MojoMagick::Font:0x000001015a8b90 @name="AvantGarde-Book", @family="AvantGarde", @style="Normal", @stretch="Normal", @weight="400", @glyphs="/usr/local/share/ghostscript/fonts/a010013l.pfb">

lib/image_magick/fonts.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
module ImageMagick
2-
module Fonts
3-
def get_fonts
4-
@parser ||= MojoMagick::Util::Parser.new
5-
raw_fonts = begin
6-
raw_command("identify", "-list font")
7-
rescue Exception => e
8-
puts e
9-
puts "Failed to execute font list with raw_command - trying straight up execute"
10-
`convert -list font`
11-
end
12-
@parser.parse_fonts(raw_fonts)
2+
class Fonts
3+
def self.all
4+
raw_fonts = MojoMagick::Commands.raw_command("identify", "-list", "font")
5+
MojoMagick::Util::FontParser.new(raw_fonts).parse
136
end
147
end
158
end

lib/initializers/hash.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/mojo_magick.rb

Lines changed: 85 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
cwd = File.dirname(__FILE__)
2-
require "open3"
3-
initializers_dir = File.expand_path(File.join(cwd, "initializers"))
4-
Dir.glob(File.join(initializers_dir, "*.rb")).sort.each { |f| require f }
5-
require File.join(cwd, "mojo_magick/util/parser")
6-
require File.join(cwd, "mojo_magick/errors")
7-
require File.join(cwd, "mojo_magick/command_status")
8-
require File.join(cwd, "image_magick/fonts")
9-
require File.join(cwd, "mojo_magick/opt_builder")
10-
require File.join(cwd, "mojo_magick/font")
111
require "tempfile"
2+
require "open3"
3+
require_relative "./mojo_magick/util/font_parser"
4+
require_relative "./mojo_magick/errors"
5+
require_relative "./mojo_magick/command_status"
6+
require_relative "./mojo_magick/commands"
7+
require_relative "./image_magick/fonts"
8+
require_relative "./mojo_magick/opt_builder"
9+
require_relative "./mojo_magick/font"
1210

1311
# MojoMagick is a stateless set of module methods which present a convient interface
1412
# for accessing common tasks for ImageMagick command line library.
@@ -34,7 +32,7 @@
3432
#
3533
# Equivalent to:
3634
#
37-
# MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0\
35+
# MojoMagick::Commands.raw_command('convert', 'source.jpg -crop 250x250+0+0\
3836
# +repage -strip -set comment "my favorite file" dest.jpg')
3937
#
4038
# Example #mogrify usage:
@@ -43,7 +41,7 @@
4341
#
4442
# Equivalent to:
4543
#
46-
# MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
44+
# MojoMagick::Commands.raw_command('mogrify', '-shave 10x10 image.jpg')
4745
#
4846
# Example showing some additional options:
4947
#
@@ -60,34 +58,40 @@
6058
# bang (!) can be appended to command names to use the '+' versions
6159
# instead of '-' versions.
6260
#
63-
module MojoMagick
64-
extend ImageMagick::Fonts
6561

66-
def self.windows?
67-
!(RUBY_PLATFORM =~ /win32/).nil?
62+
module MojoMagickDeprecations
63+
# rubocop:disable Naming/AccessorMethodName
64+
def get_fonts
65+
warn "DEPRECATION WARNING: #{__method__} is deprecated and will be removed with the next minor version release."\
66+
" Please use `available_fonts` instead"
67+
MojoMagick.available_fonts
6868
end
6969

70-
def self.execute(command, *args)
71-
execute = "#{command} #{args}"
72-
out, outerr, status = Open3.capture3(command, *args.map(&:to_s))
73-
CommandStatus.new execute, out, outerr, status
74-
rescue Exception => e
75-
raise MojoError, "#{e.class}: #{e.message}"
70+
# rubocop:enable Naming/AccessorMethodName
71+
### Moved to `Commands`
72+
def execute!(*args)
73+
warn "DEPRECATION WARNING: #{__method__} is deprecated and will be removed with the next minor version release."\
74+
" Please use `MojoMagick::Commands.execute!` instead"
75+
MojoMagick::Commands.send(:execute!, *args)
7676
end
7777

78-
def self.execute!(command, *args)
79-
status = execute(command, *args)
80-
unless status.success?
81-
err_msg = "MojoMagick command failed: #{command}."
82-
raise(MojoFailed, "#{err_msg} (Exit status: #{status.exit_code})\n" +
83-
" Command: #{status.command}\n" +
84-
" Error: #{status.error}")
85-
end
86-
status.return_value
78+
def execute(*args)
79+
warn "DEPRECATION WARNING: #{__method__} is deprecated and will be removed with the next minor version release."\
80+
" Please use `MojoMagick::Commands.execute!` instead"
81+
MojoMagick::Commands.send(:execute, *args)
82+
end
83+
84+
def raw_command(*args)
85+
warn "DEPRECATION WARNING: #{__method__} is deprecated and will be removed with the next minor version release."\
86+
" Please use `MojoMagick::Commands.execute!` instead"
87+
MojoMagick::Commands.raw_command(*args)
8788
end
89+
end
8890

89-
def self.raw_command(*args)
90-
execute!(*args)
91+
module MojoMagick
92+
extend MojoMagickDeprecations
93+
def self.windows?
94+
!RUBY_PLATFORM.include(win32)
9195
end
9296

9397
def self.shrink(source_file, dest_file, options)
@@ -105,44 +109,49 @@ def self.expand(source_file, dest_file, options)
105109
# resizes an image and returns the filename written to
106110
# options:
107111
# :width / :height => scale to these dimensions
108-
# :scale => pass scale options such as ">" to force shrink scaling only or "!" to force absolute width/height scaling (do not preserve aspect ratio)
112+
# :scale => pass scale options such as ">" to force shrink scaling only or
113+
# "!" to force absolute width/height scaling (do not preserve aspect ratio)
109114
# :percent => scale image to this percentage (do not specify :width/:height in this case)
110115
def self.resize(source_file, dest_file, options)
111-
scale_options = []
112-
scale_options << ">" unless options[:shrink_only].nil?
113-
scale_options << "<" unless options[:expand_only].nil?
114-
scale_options << "!" unless options[:absolute_aspect].nil?
115-
scale_options << "^" unless options[:fill].nil?
116-
scale_options = scale_options.join
117-
116+
scale_options = extract_scale_options(options)
117+
geometry = extract_geometry_options(options)
118118
extras = []
119-
if !options[:width].nil? && !options[:height].nil?
120-
geometry = "#{options[:width]}X#{options[:height]}"
121-
elsif !options[:percent].nil?
122-
geometry = "#{options[:percent]}%"
123-
else
124-
raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
125-
end
126119
if !options[:fill].nil? && !options[:crop].nil?
127120
extras << "-gravity"
128121
extras << "Center"
129122
extras << "-extent"
130123
extras << geometry.to_s
131124
end
132-
raw_command("convert",
133-
source_file,
134-
"-resize", "#{geometry}#{scale_options}",
135-
*extras, dest_file)
125+
Commands.raw_command("convert",
126+
source_file,
127+
"-resize", "#{geometry}#{scale_options}",
128+
*extras, dest_file)
136129
dest_file
137130
end
138131

132+
def self.convert(source = nil, dest = nil)
133+
opts = OptBuilder.new
134+
opts.file source if source
135+
yield opts
136+
opts.file dest if dest
137+
138+
Commands.raw_command("convert", *opts.to_a)
139+
end
140+
141+
def self.mogrify(dest = nil)
142+
opts = OptBuilder.new
143+
yield opts
144+
opts.file dest if dest
145+
Commands.raw_command("mogrify", *opts.to_a)
146+
end
147+
139148
def self.available_fonts
140149
# returns width, height of image if available, nil if not
141150
Font.all
142151
end
143152

144153
def self.get_format(source_file, format_string)
145-
raw_command("identify", "-format", format_string, source_file)
154+
Commands.raw_command("identify", "-format", format_string, source_file)
146155
end
147156

148157
# returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT})
@@ -161,22 +170,6 @@ def self.get_image_size(source_file)
161170
{ width: width, height: height }
162171
end
163172

164-
def self.convert(source = nil, dest = nil)
165-
opts = OptBuilder.new
166-
opts.file source if source
167-
yield opts
168-
opts.file dest if dest
169-
170-
raw_command("convert", *opts.to_a)
171-
end
172-
173-
def self.mogrify(dest = nil)
174-
opts = OptBuilder.new
175-
yield opts
176-
opts.file dest if dest
177-
raw_command("mogrify", *opts.to_a)
178-
end
179-
180173
def self.tempfile(*opts)
181174
data = opts[0]
182175
rest = opts[1]
@@ -188,4 +181,27 @@ def self.tempfile(*opts)
188181
ensure
189182
file.close
190183
end
184+
185+
class << self
186+
private
187+
188+
def extract_geometry_options(options)
189+
if !options[:width].nil? && !options[:height].nil?
190+
"#{options[:width]}X#{options[:height]}"
191+
elsif !options[:percent].nil?
192+
"#{options[:percent]}%"
193+
else
194+
raise MojoMagickError, "Resize requires width and height or percentage: #{options.inspect}"
195+
end
196+
end
197+
198+
def extract_scale_options(options)
199+
[].tap { |scale_options|
200+
scale_options << ">" unless options[:shrink_only].nil?
201+
scale_options << "<" unless options[:expand_only].nil?
202+
scale_options << "!" unless options[:absolute_aspect].nil?
203+
scale_options << "^" unless options[:fill].nil?
204+
}.join
205+
end
206+
end
191207
end

lib/mojo_magick/commands.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require_relative "opt_builder"
2+
3+
module MojoMagick
4+
class Commands
5+
def self.raw_command(*args)
6+
execute!(*args)
7+
end
8+
9+
class << self
10+
private
11+
12+
def execute(command, *args)
13+
execute = "#{command} #{args}"
14+
out, outerr, status = Open3.capture3(command, *args.map(&:to_s))
15+
CommandStatus.new execute, out, outerr, status
16+
rescue StandardError => e
17+
raise MojoError, "#{e.class}: #{e.message}"
18+
end
19+
20+
def execute!(command, *args)
21+
status = execute(command, *args)
22+
unless status.success?
23+
err_msg = "MojoMagick command failed: #{command}."
24+
raise(MojoFailed, "#{err_msg} (Exit status: #{status.exit_code})\n" \
25+
" Command: #{status.command}\n" \
26+
" Error: #{status.error}")
27+
end
28+
status.return_value
29+
end
30+
end
31+
end
32+
end

lib/mojo_magick/font.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ def valid?
77
end
88

99
def initialize(property_hash = {})
10-
property_hash.symbolize_keys!
1110
%i[name family style stretch weight glyphs].each do |f|
1211
setter = "#{f}="
1312
send(setter, property_hash[f])
1413
end
1514
end
1615

1716
def self.all
18-
ImageMagick::Font.all.map { |font_info| Font.new(font_info) }
17+
ImageMagick::Fonts.all
1918
end
2019
end
2120
end

lib/mojo_magick/opt_builder.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def image_block(&block)
5858
end
5959

6060
# Generic commands. Arguments will be formatted if necessary
61+
# rubocop:disable Lint/MissingSuper, Style/MissingRespondToMissing
6162
def method_missing(command, *args)
6263
@opts << if command.to_s[-1, 1] == "!"
6364
"+#{command.to_s.chop}"
@@ -68,10 +69,7 @@ def method_missing(command, *args)
6869
self
6970
end
7071

71-
def to_s
72-
to_a.join " "
73-
end
74-
72+
# rubocop:enable Lint/MissingSuper, Style/MissingRespondToMissing
7573
def to_a
7674
@opts.flatten
7775
end

0 commit comments

Comments
 (0)