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:
Mike McQuaid 2023-01-11 19:18:29 +00:00 committed by GitHub
commit 0fcd750ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 15 deletions

View File

@ -9,20 +9,11 @@ module Cask
#
# @api private
class Container
VALID_KEYS = Set.new([
:type,
:nested,
]).freeze
attr_accessor :nested, :type
attr_accessor(*VALID_KEYS, :pairs)
def initialize(**pairs)
@pairs = pairs
pairs.each do |key, value|
raise "invalid container key: #{key.inspect}" unless VALID_KEYS.include?(key)
send(:"#{key}=", value)
end
def initialize(nested: nil, type: nil)
@nested = nested
@type = type
return if type.nil?
return unless UnpackStrategy.from_type(type).nil?
@ -30,12 +21,16 @@ module Cask
raise "invalid container type: #{type.inspect}"
end
def pairs
instance_variables.to_h { |ivar| [ivar[1..].to_sym, instance_variable_get(ivar)] }.compact
end
def to_yaml
@pairs.to_yaml
pairs.to_yaml
end
def to_s
@pairs.inspect
pairs.inspect
end
end
end

View 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