Merge pull request #14354 from dduugg/container-pairs
Make Cask::DSL::Container#pairs a derived property (and fix YARD warning)
This commit is contained in:
commit
0fcd750ded
@ -9,20 +9,11 @@ module Cask
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
class Container
|
class Container
|
||||||
VALID_KEYS = Set.new([
|
attr_accessor :nested, :type
|
||||||
:type,
|
|
||||||
:nested,
|
|
||||||
]).freeze
|
|
||||||
|
|
||||||
attr_accessor(*VALID_KEYS, :pairs)
|
def initialize(nested: nil, type: nil)
|
||||||
|
@nested = nested
|
||||||
def initialize(**pairs)
|
@type = type
|
||||||
@pairs = pairs
|
|
||||||
pairs.each do |key, value|
|
|
||||||
raise "invalid container key: #{key.inspect}" unless VALID_KEYS.include?(key)
|
|
||||||
|
|
||||||
send(:"#{key}=", value)
|
|
||||||
end
|
|
||||||
|
|
||||||
return if type.nil?
|
return if type.nil?
|
||||||
return unless UnpackStrategy.from_type(type).nil?
|
return unless UnpackStrategy.from_type(type).nil?
|
||||||
@ -30,12 +21,16 @@ module Cask
|
|||||||
raise "invalid container type: #{type.inspect}"
|
raise "invalid container type: #{type.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pairs
|
||||||
|
instance_variables.to_h { |ivar| [ivar[1..].to_sym, instance_variable_get(ivar)] }.compact
|
||||||
|
end
|
||||||
|
|
||||||
def to_yaml
|
def to_yaml
|
||||||
@pairs.to_yaml
|
pairs.to_yaml
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
@pairs.inspect
|
pairs.inspect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
36
Library/Homebrew/test/cask/dsl/container_spec.rb
Normal file
36
Library/Homebrew/test/cask/dsl/container_spec.rb
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# typed: false
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "test/cask/dsl/shared_examples/base"
|
||||||
|
|
||||||
|
describe Cask::DSL::Container do
|
||||||
|
subject(:container) { described_class.new(**params) }
|
||||||
|
|
||||||
|
describe "#pairs" do
|
||||||
|
let(:params) { { nested: "NestedApp.dmg" } }
|
||||||
|
|
||||||
|
it "returns the attributes as a hash" do
|
||||||
|
expect(container.pairs).to eq(nested: "NestedApp.dmg")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#to_s" do
|
||||||
|
let(:params) { { nested: "NestedApp.dmg", type: :naked } }
|
||||||
|
|
||||||
|
it "returns the stringified attributes" do
|
||||||
|
expect(container.to_s).to eq('{:nested=>"NestedApp.dmg", :type=>:naked}')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#to_yaml" do
|
||||||
|
let(:params) { { nested: "NestedApp.dmg", type: :naked } }
|
||||||
|
|
||||||
|
it "returns the attributes in YAML format" do
|
||||||
|
expect(container.to_yaml).to eq(<<~YAML)
|
||||||
|
---
|
||||||
|
:nested: NestedApp.dmg
|
||||||
|
:type: :naked
|
||||||
|
YAML
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user