Replace Options.coerce with an alternate constructor

This commit is contained in:
Jack Nagel 2014-08-12 23:55:28 -05:00
parent 0a2be32d80
commit 25395c6de6
7 changed files with 24 additions and 33 deletions

View File

@ -24,6 +24,6 @@ module Dependable
end end
def options def options
Options.coerce(tags - RESERVED_TAGS) Options.create(tags - RESERVED_TAGS)
end end
end end

View File

@ -465,7 +465,7 @@ class FormulaInstaller
end end
def build_argv def build_argv
opts = Options.coerce(sanitized_ARGV_options) opts = Options.create(sanitized_ARGV_options)
opts.concat(options) opts.concat(options)
opts opts
end end

View File

@ -53,6 +53,19 @@ class Options
attr_reader :options attr_reader :options
protected :options protected :options
def self.create(array)
options = new
array.each do |e|
case e
when /^-[^-]+$/
e[1..-1].split(//).each { |o| options << Option.new(o) }
else
options << Option.new(e)
end
end
options
end
def initialize(*args) def initialize(*args)
@options = Set.new(*args) @options = Set.new(*args)
end end
@ -113,22 +126,4 @@ class Options
def inspect def inspect
"#<#{self.class.name}: #{to_a.inspect}>" "#<#{self.class.name}: #{to_a.inspect}>"
end end
def self.coerce(arg)
case arg
when Array
opts = new
arg.each do |a|
case a
when /^-[^-]+$/
a[1..-1].split(//).each { |o| opts << Option.new(o) }
else
opts << Option.new(a)
end
end
opts
else
raise TypeError, "Cannot convert #{arg.inspect} to Options"
end
end
end end

View File

@ -34,7 +34,7 @@ class SoftwareSpec
@bottle_specification = BottleSpecification.new @bottle_specification = BottleSpecification.new
@patches = [] @patches = []
@options = Options.new @options = Options.new
@build = BuildOptions.new(Options.coerce(ARGV.options_only), options) @build = BuildOptions.new(Options.create(ARGV.options_only), options)
end end
def owner= owner def owner= owner

View File

@ -106,11 +106,11 @@ class Tab < OpenStruct
end end
def used_options def used_options
Options.coerce(super) Options.create(super)
end end
def unused_options def unused_options
Options.coerce(super) Options.create(super)
end end
def cxxstdlib def cxxstdlib

View File

@ -4,8 +4,8 @@ require "options"
class BuildOptionsTests < Homebrew::TestCase class BuildOptionsTests < Homebrew::TestCase
def setup def setup
args = Options.coerce(%w(--with-foo --with-bar --without-qux)) args = Options.create(%w(--with-foo --with-bar --without-qux))
opts = Options.coerce(%w(--with-foo --with-bar --without-baz --without-qux)) opts = Options.create(%w(--with-foo --with-bar --without-baz --without-qux))
@build = BuildOptions.new(args, opts) @build = BuildOptions.new(args, opts)
end end

View File

@ -132,22 +132,18 @@ class OptionsTests < Homebrew::TestCase
assert_equal [foo, bar, baz].sort, (@options | options).to_a.sort assert_equal [foo, bar, baz].sort, (@options | options).to_a.sort
end end
def test_coerce_with_array def test_create_with_array
array = %w{--foo --bar} array = %w{--foo --bar}
option1 = Option.new("foo") option1 = Option.new("foo")
option2 = Option.new("bar") option2 = Option.new("bar")
assert_equal [option1, option2].sort, Options.coerce(array).to_a.sort assert_equal [option1, option2].sort, Options.create(array).to_a.sort
end end
def test_coerce_raises_for_inappropriate_types def test_create_splits_multiple_switches_with_single_dash
assert_raises(TypeError) { Options.coerce(1) }
end
def test_coerce_splits_multiple_switches_with_single_dash
array = %w{-vd} array = %w{-vd}
verbose = Option.new("-v") verbose = Option.new("-v")
debug = Option.new("-d") debug = Option.new("-d")
assert_equal [verbose, debug].sort, Options.coerce(array).to_a.sort assert_equal [verbose, debug].sort, Options.create(array).to_a.sort
end end
def test_copies_do_not_share_underlying_collection def test_copies_do_not_share_underlying_collection