diff --git a/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb b/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb index b7df4b0bd0..f61e997019 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb @@ -1,6 +1,7 @@ require "hbc/artifact/moved" -require "hbc/utils/hash_validator" +require "extend/hash_validator" +using HashValidator module Hbc module Artifact @@ -20,7 +21,7 @@ module Hbc raise CaskInvalidError.new(cask.token, "target required for #{english_name} '#{source_string}'") end - target_hash.extend(HashValidator).assert_valid_keys(:target) + target_hash.assert_valid_keys!(:target) new(cask, source_string, **target_hash) end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/installer.rb b/Library/Homebrew/cask/lib/hbc/artifact/installer.rb index a3a84164e0..269e122f54 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/installer.rb @@ -1,5 +1,8 @@ require "hbc/artifact/abstract_artifact" +require "extend/hash_validator" +using HashValidator + module Hbc module Artifact class Installer < AbstractArtifact @@ -60,7 +63,7 @@ module Hbc raise CaskInvalidError.new(cask, "invalid 'installer' stanza: Only one of #{VALID_KEYS.inspect} is permitted.") end - args.extend(HashValidator).assert_valid_keys(*VALID_KEYS) + args.assert_valid_keys!(*VALID_KEYS) new(cask, **args) end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index 0fc9eac2bb..81b6e4d116 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -1,8 +1,9 @@ +require "vendor/plist/plist" + require "hbc/artifact/abstract_artifact" -require "hbc/utils/hash_validator" - -require "vendor/plist/plist" +require "extend/hash_validator" +using HashValidator module Hbc module Artifact @@ -10,9 +11,7 @@ module Hbc attr_reader :pkg_relative_path def self.from_args(cask, path, **stanza_options) - stanza_options.extend(HashValidator).assert_valid_keys( - :allow_untrusted, :choices - ) + stanza_options.assert_valid_keys!(:allow_untrusted, :choices) new(cask, path, **stanza_options) end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb b/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb index 9195d889a7..b9d897983b 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb @@ -1,6 +1,7 @@ require "hbc/artifact/abstract_artifact" -require "hbc/utils/hash_validator" +require "extend/hash_validator" +using HashValidator module Hbc module Artifact @@ -10,7 +11,7 @@ module Hbc if target_hash raise CaskInvalidError unless target_hash.respond_to?(:keys) - target_hash.extend(HashValidator).assert_valid_keys(:target) + target_hash.assert_valid_keys!(:target) end target_hash ||= {} diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index e1cbb3122a..a443e8d38b 100644 --- a/Library/Homebrew/cask/lib/hbc/system_command.rb +++ b/Library/Homebrew/cask/lib/hbc/system_command.rb @@ -3,8 +3,8 @@ require "vendor/plist/plist" require "shellwords" require "extend/io" - -require "hbc/utils/hash_validator" +require "extend/hash_validator" +using HashValidator module Hbc class SystemCommand @@ -45,7 +45,7 @@ module Hbc @print_stdout = print_stdout @print_stderr = print_stderr @must_succeed = must_succeed - options.extend(HashValidator).assert_valid_keys(:chdir) + options.assert_valid_keys!(:chdir) @options = options @env = env diff --git a/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb b/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb deleted file mode 100644 index f43bf173f4..0000000000 --- a/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -module HashValidator - def assert_valid_keys(*valid_keys) - unknown_keys = keys - valid_keys - return if unknown_keys.empty? - raise %Q(Unknown keys: #{unknown_keys.inspect}. Running "brew update" will likely fix it.) - end -end diff --git a/Library/Homebrew/extend/hash_validator.rb b/Library/Homebrew/extend/hash_validator.rb new file mode 100644 index 0000000000..c5a046f9b6 --- /dev/null +++ b/Library/Homebrew/extend/hash_validator.rb @@ -0,0 +1,9 @@ +module HashValidator + refine Hash do + def assert_valid_keys!(*valid_keys) + unknown_keys = keys - valid_keys + return if unknown_keys.empty? + raise ArgumentError, "invalid keys: #{unknown_keys.map(&:inspect).join(", ")}" + end + end +end