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
|
||||
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
|
||||
|
||||
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