Skip to content

Commit

Permalink
Merge pull request #16406 from dduugg/attrable
Browse files Browse the repository at this point in the history
Create dedicated Attrable mixin for attr_ methods
  • Loading branch information
MikeMcQuaid committed Jan 1, 2024
2 parents 8c3c691 + db97ff2 commit 705d256
Show file tree
Hide file tree
Showing 18 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typed: strict
# frozen_string_literal: true

module Predicable
module Attrable
sig { params(attrs: Symbol).void }
def attr_predicate(*attrs)
attrs.each do |attr|
Expand All @@ -10,4 +10,13 @@ def attr_predicate(*attrs)
end
end
end

sig { params(attrs: Symbol).void }
def attr_rw(*attrs)
attrs.each do |attr|
define_method attr do |val = nil|
val.nil? ? instance_variable_get(:"@#{attr}") : instance_variable_set(:"@#{attr}", val)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# typed: strict

module Predicable
include Kernel
module Attrable
requires_ancestor { Module }
end
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/artifact/abstract_artifact.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "active_support/core_ext/object/deep_dup"

module Cask
Expand All @@ -13,7 +14,7 @@ class AbstractArtifact
abstract!

include Comparable
extend Predicable
extend Attrable

def self.english_name
@english_name ||= T.must(name).sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1 \2')
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "cask/denylist"
require "cask/download"
require "digest"
Expand All @@ -17,7 +18,7 @@ module Cask
# @api private
class Audit
include ::Utils::Curl
extend Predicable
extend Attrable

attr_reader :cask, :download

Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "cask/cask_loader"
require "cask/config"
require "cask/dsl"
Expand All @@ -14,7 +15,7 @@ module Cask
# @api private
class Cask
extend Forwardable
extend Predicable
extend Attrable
extend APIHashable
include Metadata

Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "locale"
require "lazy_object"
require "livecheck"
Expand Down Expand Up @@ -101,7 +102,7 @@ class DSL
*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
Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/cask/dsl/caveats.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# typed: true
# frozen_string_literal: true

require "attrable"

module Cask
class DSL
# Class corresponding to the `caveats` stanza.
Expand All @@ -15,7 +17,7 @@ class DSL
#
# @api private
class Caveats < Base
extend Predicable
extend Attrable

attr_predicate :discontinued?

Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/installer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "formula_installer"
require "unpack_strategy"
require "utils/topological_hash"
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require "utils/bottles"

require "attrable"
require "formula"
require "cask/cask_loader"
require "set"
Expand Down Expand Up @@ -176,7 +177,7 @@ def stale_cask?(pathname, scrub)
end
end

extend Predicable
extend Attrable

PERIODIC_CLEAN_FILE = (HOMEBREW_CACHE/".cleaned").freeze

Expand Down
3 changes: 2 additions & 1 deletion Library/Homebrew/debrew.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "mutex_m"
require "ignorable"

Expand Down Expand Up @@ -74,7 +75,7 @@ def self.choose
@debugged_exceptions = Set.new

class << self
extend Predicable
extend Attrable
attr_predicate :active?
attr_reader :debugged_exceptions
end
Expand Down
20 changes: 1 addition & 19 deletions Library/Homebrew/extend/module.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "cache_store"
require "did_you_mean"
require "formula_support"
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -2916,7 +2917,7 @@ def stage(interactive: false, debug_symbols: false)

# The methods below define the formula DSL.
class << self
extend Predicable
extend Attrable

include BuildEnvironment::DSL
include OnSystem::MacOSAndLinux
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# @api private
class FormulaInstaller
include FormulaCellarChecks
extend Predicable
extend Attrable

attr_reader :formula, :bottle_tab_runtime_dependencies

Expand Down
1 change: 0 additions & 1 deletion Library/Homebrew/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ def auto_update_command?
require "extend/array"
require "git_repository"
require "extend/pathname"
require "extend/predicable"
require "cli/args"

require "PATH"
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/requirement.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "dependable"
require "dependency"
require "dependencies"
Expand Down Expand Up @@ -163,6 +164,7 @@ def which_all(cmd)

class << self
include BuildEnvironment::DSL
extend Attrable

attr_reader :env_proc, :build

Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true

require "attrable"
require "resource"
require "download_strategy"
require "checksum"
Expand Down Expand Up @@ -506,6 +507,7 @@ def root_url(val = nil, specs = {})
end

class BottleSpecification
extend Attrable
RELOCATABLE_CELLARS = [:any, :any_skip_relocation].freeze

attr_rw :rebuild
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/sorbet/rbi/hidden-definitions/hidden.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Library/Homebrew/system_command.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -28,7 +27,7 @@ def system_command!(command, **options)
end

include Context
extend Predicable
extend Attrable

def self.run(executable, **options)
new(executable, **options).run!
Expand Down

0 comments on commit 705d256

Please sign in to comment.