Livecheck::Options: Add #merge!
This commit is contained in:
parent
c1b9136805
commit
0bb18b33b2
@ -56,6 +56,33 @@ module Homebrew
|
||||
Options.new(**new_options)
|
||||
end
|
||||
|
||||
# Merges values from `other` into `self` and returns `self`.
|
||||
#
|
||||
# `nil` values are removed from `other` before merging if it is an
|
||||
# `Options` object, as these are unitiailized values. This ensures that
|
||||
# existing values in `self` aren't unexpectedly overwritten with defaults.
|
||||
sig { params(other: T.any(Options, T::Hash[Symbol, T.untyped])).returns(Options) }
|
||||
def merge!(other)
|
||||
return self if other.empty?
|
||||
|
||||
if other.is_a?(Options)
|
||||
return self if self == other
|
||||
|
||||
other.instance_variables.each do |ivar|
|
||||
next if (v = T.let(other.instance_variable_get(ivar), Object)).nil?
|
||||
|
||||
instance_variable_set(ivar, v)
|
||||
end
|
||||
else
|
||||
other.each do |k, v|
|
||||
cmd = :"#{k}="
|
||||
send(cmd, v) if respond_to?(cmd)
|
||||
end
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
sig { params(other: Object).returns(T::Boolean) }
|
||||
def ==(other)
|
||||
return false unless other.is_a?(Options)
|
||||
|
@ -27,6 +27,10 @@ RSpec.describe Homebrew::Livecheck::Options do
|
||||
end
|
||||
let(:merged_hash) { args.merge(other_args) }
|
||||
|
||||
let(:base_options) { options.new(**args) }
|
||||
let(:other_options) { options.new(**other_args) }
|
||||
let(:merged_options) { options.new(**merged_hash) }
|
||||
|
||||
describe "#url_options" do
|
||||
it "returns a Hash of the options that are provided as arguments to the `url` DSL method" do
|
||||
expect(options.new.url_options).to eq({
|
||||
@ -68,6 +72,40 @@ RSpec.describe Homebrew::Livecheck::Options do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#merge!" do
|
||||
it "merges values from `other` into `self` and returns `self`" do
|
||||
o1 = options.new(**args)
|
||||
expect(o1.merge!(other_options)).to eq(merged_options)
|
||||
expect(o1).to eq(merged_options)
|
||||
|
||||
o2 = options.new(**args)
|
||||
expect(o2.merge!(other_args)).to eq(merged_options)
|
||||
expect(o2).to eq(merged_options)
|
||||
|
||||
o3 = options.new(**args)
|
||||
expect(o3.merge!(base_options)).to eq(base_options)
|
||||
expect(o3).to eq(base_options)
|
||||
|
||||
o4 = options.new(**args)
|
||||
expect(o4.merge!(args)).to eq(base_options)
|
||||
expect(o4).to eq(base_options)
|
||||
|
||||
o5 = options.new(**args)
|
||||
expect(o5.merge!(options.new)).to eq(base_options)
|
||||
expect(o5).to eq(base_options)
|
||||
|
||||
o6 = options.new(**args)
|
||||
expect(o6.merge!({})).to eq(base_options)
|
||||
expect(o6).to eq(base_options)
|
||||
end
|
||||
|
||||
it "skips over hash values without a corresponding Options value" do
|
||||
o1 = options.new(**args)
|
||||
expect(o1.merge!({ nonexistent: true })).to eq(base_options)
|
||||
expect(o1).to eq(base_options)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#==" do
|
||||
it "returns true if all instance variables are the same" do
|
||||
obj_with_args1 = options.new(**args)
|
||||
|
Loading…
x
Reference in New Issue
Block a user