Skip to content

Commit

Permalink
Added support for multi-parameter options
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Gessler committed Oct 17, 2014
1 parent 2cfb4c7 commit c7694aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
18 changes: 14 additions & 4 deletions lib/mini_magick/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ def <<(arg)
self
end

##
# Merges a list of raw options.
#
# @return [self]
#
def merge!(new_args)
new_args.each { |arg| self << arg }
self
end

##
# Changes the last operator to its "plus" form.
#
Expand All @@ -142,9 +152,9 @@ def <<(arg)
#
# @return [self]
#
def +(value = nil)
def +(*values)
args[-1] = args[-1].sub(/^-/, '+')
args << value.to_s if value
self.merge!(values)
self
end

Expand Down Expand Up @@ -196,9 +206,9 @@ def reload_methods
#
def option(*options)
options.each do |option|
define_method(option[1..-1].gsub('-', '_')) do |value = nil|
define_method(option[1..-1].gsub('-', '_')) do |*values|
self << option
self << value.to_s if value
self.merge!(values)
self
end
end
Expand Down
17 changes: 13 additions & 4 deletions spec/lib/mini_magick/tool_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,26 @@

describe "#<<" do
it "adds argument to the args list" do
subject << "foo" << "bar"
expect(subject.args).to eq %W[foo bar]
subject << "foo" << "bar" << 123
expect(subject.args).to eq %W[foo bar 123]
end
end

describe "#merge!" do
it "adds arguments to the args list" do
subject << "pre-existing"
subject.merge! ["foo", 123]
expect(subject.args).to eq %W[pre-existing foo 123]
end
end

describe "#+" do
it "switches the last option to + form" do
subject.help
subject.help.+
subject.debug.+ 8
expect(subject.args).to eq %W[-help +help +debug 8]
subject.debug.+ "foo"
subject.debug.+ 8, "bar"
expect(subject.args).to eq %W[-help +help +debug foo +debug 8 bar]
end
end

Expand Down

0 comments on commit c7694aa

Please sign in to comment.