Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ConflictsWith to be a DelegateClass(Hash). #5628

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def to_h
end,
"caveats" => caveats,
"depends_on" => depends_on,
"conflicts_with" => conflicts_with.to_h,
"conflicts_with" => conflicts_with,
"container" => container,
"auto_updates" => auto_updates,
}
Expand Down
29 changes: 12 additions & 17 deletions Library/Homebrew/cask/dsl/conflicts_with.rb
Original file line number Diff line number Diff line change
@@ -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,
]

attr_reader *VALID_KEYS

def initialize(pairs = {})
@pairs = pairs
].freeze

VALID_KEYS.each do |key|
instance_variable_set("@#{key}", Set.new)
end
def initialize(**pairs)
pairs.assert_valid_keys!(*VALID_KEYS)

pairs.each do |key, value|
raise "invalid conflicts_with key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
super(Hash[pairs.map { |k, v| [k, Set.new([*v])] }])

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
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def install
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?
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/cask/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def caveats
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

Expand Down