Skip to content

Commit 89d1d6b

Browse files
authored
Merge pull request #19359 from Homebrew/no-attr_rw
refactor: inline use of attr_rw
2 parents b9eff75 + 7880490 commit 89d1d6b

15 files changed

+51
-136
lines changed

Library/Homebrew/attrable.rb

-9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,4 @@ def attr_predicate(*attrs)
1818
end
1919
end
2020
end
21-
22-
sig { params(attrs: Symbol).void }
23-
def attr_rw(*attrs)
24-
attrs.each do |attr|
25-
define_method attr do |val = nil|
26-
val.nil? ? instance_variable_get(:"@#{attr}") : instance_variable_set(:"@#{attr}", val)
27-
end
28-
end
29-
end
3021
end

Library/Homebrew/bottle_specification.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
# frozen_string_literal: true
33

44
class BottleSpecification
5-
extend Attrable
65
RELOCATABLE_CELLARS = [:any, :any_skip_relocation].freeze
76

8-
attr_rw :rebuild
97
attr_accessor :tap
108
attr_reader :collector, :root_url_specs, :repository
119

@@ -17,6 +15,11 @@ def initialize
1715
@root_url_specs = {}
1816
end
1917

18+
sig { params(val: Integer).returns(T.nilable(Integer)) }
19+
def rebuild(val = T.unsafe(nil))
20+
val.nil? ? @rebuild : @rebuild = val
21+
end
22+
2023
def root_url(var = nil, specs = {})
2124
if var.nil?
2225
@root_url ||= if (github_packages_url = GitHubPackages.root_url_if_match(Homebrew::EnvConfig.bottle_domain))

Library/Homebrew/formula.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -3374,9 +3374,11 @@ def freeze
33743374
# desc "Example formula"
33753375
# ```
33763376
#
3377-
# @!attribute [w] desc
33783377
# @api public
3379-
attr_rw :desc
3378+
sig { params(val: String).returns(T.nilable(String)) }
3379+
def desc(val = T.unsafe(nil))
3380+
val.nil? ? @desc : @desc = T.let(val, T.nilable(String))
3381+
end
33803382

33813383
# The SPDX ID of the open-source license that the formula uses.
33823384
# Shows when running `brew info`.
@@ -3524,9 +3526,11 @@ def network_access_allowed?(phase)
35243526
# homepage "https://www.example.com"
35253527
# ```
35263528
#
3527-
# @!attribute [w] homepage
35283529
# @api public
3529-
attr_rw :homepage
3530+
sig { params(val: String).returns(T.nilable(String)) }
3531+
def homepage(val = T.unsafe(nil))
3532+
val.nil? ? @homepage : @homepage = T.let(val, T.nilable(String))
3533+
end
35303534

35313535
# Checks whether a `livecheck` specification is defined or not.
35323536
#
@@ -3566,7 +3570,6 @@ def service?
35663570
# why they cannot use the bottle.
35673571
attr_accessor :pour_bottle_check_unsatisfied_reason
35683572

3569-
# @!attribute [w] revision
35703573
# Used for creating new Homebrew versions of software without new upstream
35713574
# versions. For example, if we bump the major version of a library that this
35723575
# {Formula} {.depends_on} then we may need to update the `revision` of this
@@ -3580,9 +3583,11 @@ def service?
35803583
# ```
35813584
#
35823585
# @api public
3583-
attr_rw :revision
3586+
sig { params(val: Integer).returns(T.nilable(Integer)) }
3587+
def revision(val = T.unsafe(nil))
3588+
val.nil? ? @revision : @revision = T.let(val, T.nilable(Integer))
3589+
end
35843590

3585-
# @!attribute [w] version_scheme
35863591
# Used for creating new Homebrew version schemes. For example, if we want
35873592
# to change version scheme from one to another, then we may need to update
35883593
# `version_scheme` of this {Formula} to be able to use new version scheme,
@@ -3598,7 +3603,10 @@ def service?
35983603
# ```
35993604
#
36003605
# @api public
3601-
attr_rw :version_scheme
3606+
sig { params(val: Integer).returns(T.nilable(Integer)) }
3607+
def version_scheme(val = T.unsafe(nil))
3608+
val.nil? ? @version_scheme : @version_scheme = T.let(val, T.nilable(Integer))
3609+
end
36023610

36033611
def spec_syms
36043612
[:stable, :head].freeze

Library/Homebrew/requirement.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,23 @@ def which_all(cmd)
179179

180180
class << self
181181
include BuildEnvironment::DSL
182-
extend Attrable
183182

184183
attr_reader :env_proc, :build
185184

186-
attr_rw :fatal, :cask, :download
185+
sig { params(val: String).returns(T.nilable(String)) }
186+
def cask(val = T.unsafe(nil))
187+
val.nil? ? @cask : @cask = val
188+
end
189+
190+
sig { params(val: String).returns(T.nilable(String)) }
191+
def download(val = T.unsafe(nil))
192+
val.nil? ? @download : @download = val
193+
end
194+
195+
sig { params(val: T::Boolean).returns(T.nilable(T::Boolean)) }
196+
def fatal(val = T.unsafe(nil))
197+
val.nil? ? @fatal : @fatal = val
198+
end
187199

188200
def satisfy(options = nil, &block)
189201
return @satisfied if options.nil? && !block

Library/Homebrew/sorbet/rbi/dsl/arch_requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/bottle_specification.rbi

-11
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/cask_dependent/requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/codesign_requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/formula.rbi

-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Library/Homebrew/sorbet/rbi/dsl/linux_requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/mac_os_requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/requirement.rbi

-19
This file was deleted.

Library/Homebrew/sorbet/rbi/dsl/xcode_requirement.rbi

-8
This file was deleted.

Library/Homebrew/sorbet/tapioca/compilers/attrables.rb

+8-19
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,14 @@ def decorate
2828

2929
sig { params(klass: RBI::Scope, method: T.any(Method, UnboundMethod), class_method: T::Boolean).void }
3030
def compile_attrable_method(klass, method, class_method: false)
31-
case method.arity
32-
when -1
33-
# attr_rw
34-
klass.create_method(
35-
method.name.to_s,
36-
parameters: [create_opt_param("arg", type: "T.untyped", default: "nil")],
37-
return_type: "T.untyped",
38-
class_method:,
39-
)
40-
when 0
41-
# attr_predicate
42-
klass.create_method(
43-
method.name.to_s,
44-
return_type: "T::Boolean",
45-
class_method:,
46-
)
47-
else
48-
raise "Unsupported arity for method #{method.name} - did `Attrable` change?"
49-
end
31+
raise "Unsupported arity for method #{method.name} - did `Attrable` change?" unless method.arity.zero?
32+
33+
# attr_predicate
34+
klass.create_method(
35+
method.name.to_s,
36+
return_type: "T::Boolean",
37+
class_method:,
38+
)
5039
end
5140
end
5241
end

Library/Homebrew/test/bottle_specification_spec.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@
5252
end
5353
end
5454

55-
%w[root_url rebuild].each do |method|
56-
specify "##{method}" do
57-
object = Object.new
58-
bottle_spec.public_send(method, object)
59-
expect(bottle_spec.public_send(method)).to eq(object)
60-
end
55+
specify "#rebuild" do
56+
bottle_spec.rebuild(1337)
57+
expect(bottle_spec.rebuild).to eq(1337)
58+
end
59+
60+
specify "#root_url" do
61+
bottle_spec.root_url("https://example.com")
62+
expect(bottle_spec.root_url).to eq("https://example.com")
6163
end
6264
end

0 commit comments

Comments
 (0)