Refactor ConflictsWith to be a DelegateClass(Hash).

This commit is contained in:
Markus Reiter 2019-01-27 21:34:24 +01:00
parent 76b3c3dbe2
commit d22c2eca9e
4 changed files with 15 additions and 20 deletions

View File

@ -133,7 +133,7 @@ module Cask
end,
"caveats" => caveats,
"depends_on" => depends_on,
"conflicts_with" => conflicts_with.to_h,
"conflicts_with" => conflicts_with,
"container" => container,
"auto_updates" => auto_updates,
}

View File

@ -1,33 +1,28 @@
require "extend/hash_validator"
using HashValidator
module Cask
class DSL
class ConflictsWith
VALID_KEYS = Set.new [
class ConflictsWith < DelegateClass(Hash)
VALID_KEYS = [
:formula,
:cask,
:macos,
:arch,
:x11,
:java,
]
].freeze
attr_reader *VALID_KEYS
def initialize(**pairs)
pairs.assert_valid_keys!(*VALID_KEYS)
def initialize(pairs = {})
@pairs = pairs
super(Hash[pairs.map { |k, v| [k, Set.new([*v])] }])
VALID_KEYS.each do |key|
instance_variable_set("@#{key}", Set.new)
end
pairs.each do |key, value|
raise "invalid conflicts_with key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
instance_variable_set("@#{key}", instance_variable_get("@#{key}").merge([*value]))
end
self.default = Set.new
end
def to_h
Hash[VALID_KEYS.map { |key| [key, instance_variable_get("@#{key}").to_a] }]
def to_json(generator)
Hash[map { |k, v| [k, v.to_a] }].to_json(generator)
end
end
end

View File

@ -104,7 +104,7 @@ module Cask
def check_conflicts
return unless @cask.conflicts_with
@cask.conflicts_with.cask.each do |conflicting_cask|
@cask.conflicts_with[:cask].each do |conflicting_cask|
begin
conflicting_cask = CaskLoader.load(conflicting_cask)
if conflicting_cask.installed?

View File

@ -401,7 +401,7 @@ describe Cask::DSL, :cask do
let(:token) { "with-conflicts-with" }
it "allows conflicts_with stanza to be specified" do
expect(cask.conflicts_with.formula).not_to be nil
expect(cask.conflicts_with[:formula]).to be_empty
end
end