Refactor HashValidator.

This commit is contained in:
Markus Reiter 2018-07-16 22:46:02 +02:00
parent 679eda3155
commit bb29150096
7 changed files with 27 additions and 21 deletions

View File

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

View File

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

View File

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

View File

@ -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 ||= {}

View File

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

View File

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

View File

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