Refactor HashValidator.
This commit is contained in:
parent
679eda3155
commit
bb29150096
@ -1,6 +1,7 @@
|
|||||||
require "hbc/artifact/moved"
|
require "hbc/artifact/moved"
|
||||||
|
|
||||||
require "hbc/utils/hash_validator"
|
require "extend/hash_validator"
|
||||||
|
using HashValidator
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
module Artifact
|
module Artifact
|
||||||
@ -20,7 +21,7 @@ module Hbc
|
|||||||
raise CaskInvalidError.new(cask.token, "target required for #{english_name} '#{source_string}'")
|
raise CaskInvalidError.new(cask.token, "target required for #{english_name} '#{source_string}'")
|
||||||
end
|
end
|
||||||
|
|
||||||
target_hash.extend(HashValidator).assert_valid_keys(:target)
|
target_hash.assert_valid_keys!(:target)
|
||||||
|
|
||||||
new(cask, source_string, **target_hash)
|
new(cask, source_string, **target_hash)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
require "hbc/artifact/abstract_artifact"
|
require "hbc/artifact/abstract_artifact"
|
||||||
|
|
||||||
|
require "extend/hash_validator"
|
||||||
|
using HashValidator
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
module Artifact
|
module Artifact
|
||||||
class Installer < AbstractArtifact
|
class Installer < AbstractArtifact
|
||||||
@ -60,7 +63,7 @@ module Hbc
|
|||||||
raise CaskInvalidError.new(cask, "invalid 'installer' stanza: Only one of #{VALID_KEYS.inspect} is permitted.")
|
raise CaskInvalidError.new(cask, "invalid 'installer' stanza: Only one of #{VALID_KEYS.inspect} is permitted.")
|
||||||
end
|
end
|
||||||
|
|
||||||
args.extend(HashValidator).assert_valid_keys(*VALID_KEYS)
|
args.assert_valid_keys!(*VALID_KEYS)
|
||||||
new(cask, **args)
|
new(cask, **args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
|
require "vendor/plist/plist"
|
||||||
|
|
||||||
require "hbc/artifact/abstract_artifact"
|
require "hbc/artifact/abstract_artifact"
|
||||||
|
|
||||||
require "hbc/utils/hash_validator"
|
require "extend/hash_validator"
|
||||||
|
using HashValidator
|
||||||
require "vendor/plist/plist"
|
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
module Artifact
|
module Artifact
|
||||||
@ -10,9 +11,7 @@ module Hbc
|
|||||||
attr_reader :pkg_relative_path
|
attr_reader :pkg_relative_path
|
||||||
|
|
||||||
def self.from_args(cask, path, **stanza_options)
|
def self.from_args(cask, path, **stanza_options)
|
||||||
stanza_options.extend(HashValidator).assert_valid_keys(
|
stanza_options.assert_valid_keys!(:allow_untrusted, :choices)
|
||||||
:allow_untrusted, :choices
|
|
||||||
)
|
|
||||||
new(cask, path, **stanza_options)
|
new(cask, path, **stanza_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
require "hbc/artifact/abstract_artifact"
|
require "hbc/artifact/abstract_artifact"
|
||||||
|
|
||||||
require "hbc/utils/hash_validator"
|
require "extend/hash_validator"
|
||||||
|
using HashValidator
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
module Artifact
|
module Artifact
|
||||||
@ -10,7 +11,7 @@ module Hbc
|
|||||||
|
|
||||||
if target_hash
|
if target_hash
|
||||||
raise CaskInvalidError unless target_hash.respond_to?(:keys)
|
raise CaskInvalidError unless target_hash.respond_to?(:keys)
|
||||||
target_hash.extend(HashValidator).assert_valid_keys(:target)
|
target_hash.assert_valid_keys!(:target)
|
||||||
end
|
end
|
||||||
|
|
||||||
target_hash ||= {}
|
target_hash ||= {}
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "vendor/plist/plist"
|
|||||||
require "shellwords"
|
require "shellwords"
|
||||||
|
|
||||||
require "extend/io"
|
require "extend/io"
|
||||||
|
require "extend/hash_validator"
|
||||||
require "hbc/utils/hash_validator"
|
using HashValidator
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
class SystemCommand
|
class SystemCommand
|
||||||
@ -45,7 +45,7 @@ module Hbc
|
|||||||
@print_stdout = print_stdout
|
@print_stdout = print_stdout
|
||||||
@print_stderr = print_stderr
|
@print_stderr = print_stderr
|
||||||
@must_succeed = must_succeed
|
@must_succeed = must_succeed
|
||||||
options.extend(HashValidator).assert_valid_keys(:chdir)
|
options.assert_valid_keys!(:chdir)
|
||||||
@options = options
|
@options = options
|
||||||
@env = env
|
@env = env
|
||||||
|
|
||||||
|
|||||||
@ -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
|
|
||||||
9
Library/Homebrew/extend/hash_validator.rb
Normal file
9
Library/Homebrew/extend/hash_validator.rb
Normal 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
|
||||||
Loading…
x
Reference in New Issue
Block a user