Create dedicated Attrable mixin for attr_ methods

This commit is contained in:
Douglas Eichelberger 2023-12-28 11:45:18 -08:00
parent e794b99ea5
commit ffd761d2e9
19 changed files with 62 additions and 50 deletions

View File

@ -0,0 +1,32 @@
# typed: strict
# frozen_string_literal: true
module Attrable
sig { params(attrs: Symbol).void }
def attr_predicate(*attrs)
attrs.each do |attr|
define_method attr do
instance_variable_get("@#{attr.to_s.sub(/\?$/, "")}") == true
end
end
end
sig { params(attrs: Symbol).void }
def attr_rw(*attrs)
attrs.each do |attr|
module_eval <<-EOS, __FILE__, __LINE__+1
def #{attr}(val=nil) # def prefix(val=nil)
if val.nil? # if val.nil?
if instance_variable_defined?(:@#{attr}) # if instance_variable_defined?(:@prefix)
return @#{attr} # return @prefix
else # else
return nil # return nil
end # end
end # end
#
@#{attr} = val # @prefix = val
end # end
EOS
end
end
end

View File

@ -1,6 +1,5 @@
# typed: strict
module Predicable
include Kernel
module Attrable
requires_ancestor { Module }
end

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "active_support/core_ext/object/deep_dup"
module Cask
@ -13,7 +14,7 @@ module Cask
abstract!
include Comparable
extend Predicable
extend Attrable
def self.english_name
@english_name ||= T.must(name).sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1 \2')

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "cask/denylist"
require "cask/download"
require "digest"
@ -17,7 +18,7 @@ module Cask
# @api private
class Audit
include ::Utils::Curl
extend Predicable
extend Attrable
attr_reader :cask, :download

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
@ -14,7 +15,7 @@ module Cask
# @api private
class Cask
extend Forwardable
extend Predicable
extend Attrable
extend APIHashable
include Metadata

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "locale"
require "lazy_object"
require "livecheck"
@ -101,7 +102,7 @@ module Cask
*ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] },
]).freeze
extend Predicable
extend Attrable
include OnSystem::MacOSOnly
attr_reader :cask, :token, :deprecation_date, :deprecation_reason, :disable_date, :disable_reason

View File

@ -1,6 +1,8 @@
# typed: true
# frozen_string_literal: true
require "attrable"
module Cask
class DSL
# Class corresponding to the `caveats` stanza.
@ -15,7 +17,7 @@ module Cask
#
# @api private
class Caveats < Base
extend Predicable
extend Attrable
attr_predicate :discontinued?

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "formula_installer"
require "unpack_strategy"
require "utils/topological_hash"
@ -17,7 +18,7 @@ module Cask
#
# @api private
class Installer
extend Predicable
extend Attrable
def initialize(cask, command: SystemCommand, force: false, adopt: false,
skip_cask_deps: false, binaries: true, verbose: false,

View File

@ -3,6 +3,7 @@
require "utils/bottles"
require "attrable"
require "formula"
require "cask/cask_loader"
require "set"
@ -176,7 +177,7 @@ module Homebrew
end
end
extend Predicable
extend Attrable
PERIODIC_CLEAN_FILE = (HOMEBREW_CACHE/".cleaned").freeze

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "mutex_m"
require "ignorable"
@ -74,7 +75,7 @@ module Debrew
@debugged_exceptions = Set.new
class << self
extend Predicable
extend Attrable
attr_predicate :active?
attr_reader :debugged_exceptions
end

View File

@ -1,24 +1,6 @@
# typed: true
# typed: strict
# frozen_string_literal: true
class Module
include T::Sig
def attr_rw(*attrs)
attrs.each do |attr|
module_eval <<-EOS, __FILE__, __LINE__+1
def #{attr}(val=nil) # def prefix(val=nil)
if val.nil? # if val.nil?
if instance_variable_defined?(:@#{attr}) # if instance_variable_defined?(:@prefix)
return @#{attr} # return @prefix
else # else
return nil # return nil
end # end
end # end
#
@#{attr} = val # @prefix = val
end # end
EOS
end
end
end

View File

@ -1,13 +0,0 @@
# typed: strict
# frozen_string_literal: true
module Predicable
sig { params(attrs: Symbol).void }
def attr_predicate(*attrs)
attrs.each do |attr|
define_method attr do
instance_variable_get("@#{attr.to_s.sub(/\?$/, "")}") == true
end
end
end
end

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "cache_store"
require "did_you_mean"
require "formula_support"
@ -66,7 +67,7 @@ class Formula
include Homebrew::Livecheck::Constants
extend Forwardable
extend Cachable
extend Predicable
extend Attrable
extend APIHashable
# The name of this {Formula}.
@ -2916,7 +2917,7 @@ class Formula
# The methods below define the formula DSL.
class << self
extend Predicable
extend Attrable
include BuildEnvironment::DSL
include OnSystem::MacOSAndLinux

View File

@ -28,7 +28,7 @@ require "service"
# @api private
class FormulaInstaller
include FormulaCellarChecks
extend Predicable
extend Attrable
attr_reader :formula, :bottle_tab_runtime_dependencies

View File

@ -135,7 +135,6 @@ require "context"
require "extend/array"
require "git_repository"
require "extend/pathname"
require "extend/predicable"
require "cli/args"
require "PATH"

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "dependable"
require "dependency"
require "dependencies"
@ -163,6 +164,7 @@ class Requirement
class << self
include BuildEnvironment::DSL
extend Attrable
attr_reader :env_proc, :build

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "resource"
require "download_strategy"
require "checksum"
@ -506,6 +507,7 @@ class Bottle
end
class BottleSpecification
extend Attrable
RELOCATABLE_CELLARS = [:any, :any_skip_relocation].freeze
attr_rw :rebuild

View File

@ -7219,7 +7219,7 @@ class Parlour::Types::Type
extend ::T::InterfaceWrapper::Helpers
end
module Predicable
module Attrable
extend ::T::Private::Methods::MethodHooks
extend ::T::Private::Methods::SingletonMethodHooks
end

View File

@ -1,13 +1,12 @@
# typed: true
# frozen_string_literal: true
require "attrable"
require "open3"
require "plist"
require "shellwords"
require "extend/io"
require "extend/predicable"
require "extend/time"
# Class for running sub-processes and capturing their output and exit status.
@ -28,7 +27,7 @@ class SystemCommand
end
include Context
extend Predicable
extend Attrable
def self.run(executable, **options)
new(executable, **options).run!