diff options
| author | Markus Reiter | 2017-06-26 07:30:28 +0200 |
|---|---|---|
| committer | Markus Reiter | 2017-06-28 09:25:31 +0200 |
| commit | 3b4ee58c491f164c2751c1c0f941568c35fb0ac6 (patch) | |
| tree | eb6690a2d0e872c3f7e317d5aeadb0eb03697195 | |
| parent | 4fb60d89886758884fae29566fc04de66837f0c6 (diff) | |
| download | brew-3b4ee58c491f164c2751c1c0f941568c35fb0ac6.tar.bz2 | |
Refactor using `Forwardable` and `DelegateClass`.
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/artifact/base.rb | 10 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cask.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/checkable.rb | 14 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/installer.rb | 19 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/url.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/utils.rb | 8 | ||||
| -rw-r--r-- | Library/Homebrew/caveats.rb | 6 | ||||
| -rw-r--r-- | Library/Homebrew/checksum.rb | 7 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/info.rb | 4 | ||||
| -rw-r--r-- | Library/Homebrew/cmd/update-report.rb | 6 | ||||
| -rw-r--r-- | Library/Homebrew/debrew.rb | 11 | ||||
| -rw-r--r-- | Library/Homebrew/dependencies.rb | 59 | ||||
| -rw-r--r-- | Library/Homebrew/dependency.rb | 5 | ||||
| -rw-r--r-- | Library/Homebrew/download_strategy.rb | 5 | ||||
| -rw-r--r-- | Library/Homebrew/extend/predicable.rb | 9 | ||||
| -rw-r--r-- | Library/Homebrew/formula.rb | 115 | ||||
| -rw-r--r-- | Library/Homebrew/formula_installer.rb | 11 | ||||
| -rw-r--r-- | Library/Homebrew/formula_support.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/global.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/resource.rb | 1 | ||||
| -rw-r--r-- | Library/Homebrew/software_spec.rb | 1 |
21 files changed, 96 insertions, 203 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/artifact/base.rb b/Library/Homebrew/cask/lib/hbc/artifact/base.rb index 2d9330b13..ae15552a4 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/base.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/base.rb @@ -1,6 +1,8 @@ module Hbc module Artifact class Base + extend Predicable + def self.artifact_name @artifact_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase end @@ -65,13 +67,7 @@ module Hbc {} end - def verbose? - @verbose - end - - def force? - @force - end + attr_predicate :force?, :verbose? def initialize(cask, command: SystemCommand, force: false, verbose: false) @cask = cask diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index e1cdb5dea..4a410f4c7 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -1,5 +1,3 @@ -require "forwardable" - require "hbc/dsl" require "hbc/metadata" diff --git a/Library/Homebrew/cask/lib/hbc/checkable.rb b/Library/Homebrew/cask/lib/hbc/checkable.rb index 03f052629..604dd3f89 100644 --- a/Library/Homebrew/cask/lib/hbc/checkable.rb +++ b/Library/Homebrew/cask/lib/hbc/checkable.rb @@ -1,29 +1,27 @@ module Hbc module Checkable def errors - Array(@errors) + @errors ||= [] end def warnings - Array(@warnings) + @warnings ||= [] end def add_error(message) - @errors ||= [] - @errors << message + errors << message end def add_warning(message) - @warnings ||= [] - @warnings << message + warnings << message end def errors? - Array(@errors).any? + errors.any? end def warnings? - Array(@warnings).any? + warnings.any? end def result diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index aa6d600f7..0477cbd22 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -6,6 +6,7 @@ require "hbc/verify" module Hbc class Installer + extend Predicable # TODO: it is unwise for Hbc::Staged to be a module, when we are # dealing with both staged and unstaged Casks here. This should # either be a class which is only sometimes instantiated, or there @@ -27,21 +28,7 @@ module Hbc @reinstall = false end - def skip_cask_deps? - @skip_cask_deps - end - - def force? - @force - end - - def binaries? - @binaries - end - - def verbose? - @verbose - end + attr_predicate :binaries?, :force?, :skip_cask_deps?, :require_sha?, :verbose? def self.print_caveats(cask) odebug "Printing caveats" @@ -75,7 +62,7 @@ module Hbc odebug "Hbc::Installer#fetch" satisfy_dependencies - verify_has_sha if @require_sha && !force? + verify_has_sha if require_sha? && !force? download verify end diff --git a/Library/Homebrew/cask/lib/hbc/url.rb b/Library/Homebrew/cask/lib/hbc/url.rb index 07b41fb80..15da2ced2 100644 --- a/Library/Homebrew/cask/lib/hbc/url.rb +++ b/Library/Homebrew/cask/lib/hbc/url.rb @@ -1,5 +1,3 @@ -require "forwardable" - module Hbc class URL FAKE_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10) https://caskroom.github.io".freeze diff --git a/Library/Homebrew/cask/lib/hbc/utils.rb b/Library/Homebrew/cask/lib/hbc/utils.rb index 59e85aaeb..f23ce059c 100644 --- a/Library/Homebrew/cask/lib/hbc/utils.rb +++ b/Library/Homebrew/cask/lib/hbc/utils.rb @@ -14,14 +14,14 @@ class Object end class Buffer < StringIO + extend Predicable + + attr_predicate :tty? + def initialize(tty = false) super() @tty = tty end - - def tty? - @tty - end end # global methods diff --git a/Library/Homebrew/caveats.rb b/Library/Homebrew/caveats.rb index ee09063fd..2d0ceeb48 100644 --- a/Library/Homebrew/caveats.rb +++ b/Library/Homebrew/caveats.rb @@ -1,4 +1,6 @@ class Caveats + extend Forwardable + attr_reader :f def initialize(f) @@ -25,9 +27,7 @@ class Caveats caveats.compact.join("\n") end - def empty? - caveats.empty? - end + delegate [:empty?, :to_s] => :caveats private diff --git a/Library/Homebrew/checksum.rb b/Library/Homebrew/checksum.rb index 1b095ea32..506885fce 100644 --- a/Library/Homebrew/checksum.rb +++ b/Library/Homebrew/checksum.rb @@ -1,6 +1,7 @@ class Checksum + extend Forwardable + attr_reader :hash_type, :hexdigest - alias to_s hexdigest TYPES = [:sha256].freeze @@ -9,9 +10,7 @@ class Checksum @hexdigest = hexdigest end - def empty? - hexdigest.empty? - end + delegate [:empty?, :to_s] => :@hexdigest def ==(other) hash_type == other.hash_type && hexdigest == other.hexdigest diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 98200a0b4..731156f95 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -169,8 +169,8 @@ module Homebrew Homebrew.dump_options_for_formula f end - c = Caveats.new(f) - ohai "Caveats", c.caveats unless c.empty? + caveats = Caveats.new(f) + ohai "Caveats", caveats.to_s unless caveats.empty? end def decorate_dependencies(dependencies) diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index aa1fd244d..374bbbd73 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -547,6 +547,8 @@ class Reporter end class ReporterHub + extend Forwardable + attr_reader :reporters def initialize @@ -564,9 +566,7 @@ class ReporterHub @hash.update(report) { |_key, oldval, newval| oldval.concat(newval) } end - def empty? - @hash.empty? - end + delegate :empty? => :@hash def dump # Key Legend: Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R) diff --git a/Library/Homebrew/debrew.rb b/Library/Homebrew/debrew.rb index 7ff2c0360..c2662c9ee 100644 --- a/Library/Homebrew/debrew.rb +++ b/Library/Homebrew/debrew.rb @@ -74,18 +74,13 @@ module Debrew end end - class << self - alias original_raise raise - end - @active = false @debugged_exceptions = Set.new - def self.active? - @active - end - class << self + extend Predicable + alias original_raise raise + attr_predicate :active? attr_reader :debugged_exceptions end diff --git a/Library/Homebrew/dependencies.rb b/Library/Homebrew/dependencies.rb index 951db078d..0ed0502d8 100644 --- a/Library/Homebrew/dependencies.rb +++ b/Library/Homebrew/dependencies.rb @@ -1,28 +1,11 @@ -class Dependencies - include Enumerable +require "delegate" - def initialize - @deps = [] +class Dependencies < DelegateClass(Array) + def initialize(*args) + super(args) end - def each(*args, &block) - @deps.each(*args, &block) - end - - def <<(o) - @deps << o - self - end - - def empty? - @deps.empty? - end - - def *(arg) - @deps * arg - end - - alias to_ary to_a + alias eql? == def optional select(&:optional?) @@ -44,40 +27,28 @@ class Dependencies build + required + recommended end - attr_reader :deps - protected :deps - - def ==(other) - deps == other.deps - end - alias eql? == - def inspect - "#<#{self.class.name}: #{to_a.inspect}>" + "#<#{self.class.name}: #{to_a}>" end end -class Requirements - include Enumerable - - def initialize - @reqs = Set.new - end - - def each(*args, &block) - @reqs.each(*args, &block) +class Requirements < DelegateClass(Set) + def initialize(*args) + super(Set.new(args)) end def <<(other) if other.is_a?(Comparable) - @reqs.grep(other.class) do |req| + grep(other.class) do |req| return self if req > other - @reqs.delete(req) + delete(req) end end - @reqs << other + super self end - alias to_ary to_a + def inspect + "#<#{self.class.name}: {#{to_a.join(", ")}}>" + end end diff --git a/Library/Homebrew/dependency.rb b/Library/Homebrew/dependency.rb index 4a452440a..d7d5ec59c 100644 --- a/Library/Homebrew/dependency.rb +++ b/Library/Homebrew/dependency.rb @@ -2,6 +2,7 @@ require "dependable" # A dependency on another Homebrew formula. class Dependency + extend Forwardable include Dependable attr_reader :name, :tags, :env_proc, :option_names @@ -34,9 +35,7 @@ class Dependency formula end - def installed? - to_formula.installed? - end + delegate installed?: :to_formula def satisfied?(inherited_options) installed? && missing_options(inherited_options).empty? diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 78f6c1e28..717334714 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -3,6 +3,7 @@ require "rexml/document" require "time" class AbstractDownloadStrategy + extend Forwardable include FileUtils attr_reader :meta, :name, :version, :resource @@ -181,9 +182,7 @@ class VCSDownloadStrategy < AbstractDownloadStrategy @clone end - def head? - version.head? - end + delegate head?: :version # Return last commit's unique identifier for the repository. # Return most recent modified timestamp unless overridden. diff --git a/Library/Homebrew/extend/predicable.rb b/Library/Homebrew/extend/predicable.rb new file mode 100644 index 000000000..d02fbb89b --- /dev/null +++ b/Library/Homebrew/extend/predicable.rb @@ -0,0 +1,9 @@ +module Predicable + def attr_predicate(*attrs) + attrs.each do |attr| + define_method attr do + instance_variable_get("@#{attr.to_s.sub(/\?$/, "")}") == true + end + end + end +end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 66993a348..0d000496b 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -45,6 +45,7 @@ class Formula include Utils::Inreplace include Utils::Shell extend Enumerable + extend Forwardable # @!method inreplace(paths, before = nil, after = nil) # Actually implemented in {Utils::Inreplace.inreplace}. @@ -314,37 +315,14 @@ class Formula active_spec == head end - # @private - def bottle_unneeded? - active_spec.bottle_unneeded? - end - - # @private - def bottle_disabled? - active_spec.bottle_disabled? - end - - # @private - def bottle_disable_reason - active_spec.bottle_disable_reason - end - - # Does the currently active {SoftwareSpec} have any bottle? - # @private - def bottle_defined? - active_spec.bottle_defined? - end - - # Does the currently active {SoftwareSpec} have an installable bottle? - # @private - def bottled? - active_spec.bottled? - end - - # @private - def bottle_specification - active_spec.bottle_specification - end + delegate [ + :bottle_unneeded?, + :bottle_disabled?, + :bottle_disable_reason, + :bottle_defined?, + :bottled?, + :bottle_specification, + ] => :active_spec # The Bottle object for the currently active {SoftwareSpec}. # @private @@ -353,24 +331,21 @@ class Formula end # The description of the software. + # @method desc # @see .desc - def desc - self.class.desc - end + delegate desc: :"self.class" # The homepage for the software. + # @method homepage # @see .homepage - def homepage - self.class.homepage - end + delegate homepage: :"self.class" # The version for the currently active {SoftwareSpec}. # The version is autodetected from the URL and/or tag so only needs to be # declared if it cannot be autodetected correctly. + # @method version # @see .version - def version - active_spec.version - end + delegate version: :active_spec def update_head_version return unless head? @@ -394,9 +369,8 @@ class Formula # Additional downloads can be defined as {#resource}s. # {Resource#stage} will create a temporary directory and yield to a block. # <pre>resource("additional_files").stage { bin.install "my/extra/tool" }</pre> - def resource(name) - active_spec.resource(name) - end + # @method resource + delegate resource: :active_spec # An old name for the formula def oldname @@ -416,68 +390,39 @@ class Formula end # The {Resource}s for the currently active {SoftwareSpec}. - def resources - active_spec.resources.values - end + # @method resources + def_delegator :"active_spec.resources", :values, :resources # The {Dependency}s for the currently active {SoftwareSpec}. - # @private - def deps - active_spec.deps - end + delegate deps: :active_spec # The {Requirement}s for the currently active {SoftwareSpec}. - # @private - def requirements - active_spec.requirements - end + delegate requirements: :active_spec # The cached download for the currently active {SoftwareSpec}. - # @private - def cached_download - active_spec.cached_download - end + delegate cached_download: :active_spec # Deletes the download for the currently active {SoftwareSpec}. - # @private - def clear_cache - active_spec.clear_cache - end + delegate clear_cache: :active_spec # The list of patches for the currently active {SoftwareSpec}. - # @private - def patchlist - active_spec.patches - end + def_delegator :active_spec, :patches, :patchlist # The options for the currently active {SoftwareSpec}. - # @private - def options - active_spec.options - end + delegate options: :active_spec # The deprecated options for the currently active {SoftwareSpec}. - # @private - def deprecated_options - active_spec.deprecated_options - end + delegate deprecated_options: :active_spec # The deprecated option flags for the currently active {SoftwareSpec}. - # @private - def deprecated_flags - active_spec.deprecated_flags - end + delegate deprecated_flags: :active_spec # If a named option is defined for the currently active {SoftwareSpec}. - def option_defined?(name) - active_spec.option_defined?(name) - end + # @method option_defined? + delegate option_defined?: :active_spec # All the {.fails_with} for the currently active {SoftwareSpec}. - # @private - def compiler_failures - active_spec.compiler_failures - end + delegate compiler_failures: :active_spec # If this {Formula} is installed. # This is actually just a check for if the {#installed_prefix} directory diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index a365c967a..27786e77e 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -17,6 +17,7 @@ require "development_tools" class FormulaInstaller include FormulaCellarChecks + extend Predicable def self.mode_attr_accessor(*names) attr_accessor(*names) @@ -559,11 +560,11 @@ class FormulaInstaller audit_installed if ARGV.homebrew_developer? && !formula.keg_only? - c = Caveats.new(formula) + caveats = Caveats.new(formula) - return if c.empty? + return if caveats.empty? @show_summary_heading = true - ohai "Caveats", c.caveats + ohai "Caveats", caveats.to_s end def finish @@ -879,9 +880,7 @@ class FormulaInstaller private - def hold_locks? - @hold_locks || false - end + attr_predicate :hold_locks? def lock return unless (@@locked ||= []).empty? diff --git a/Library/Homebrew/formula_support.rb b/Library/Homebrew/formula_support.rb index 4d963a55e..a4534291a 100644 --- a/Library/Homebrew/formula_support.rb +++ b/Library/Homebrew/formula_support.rb @@ -81,7 +81,7 @@ class BottleDisableReason end def to_s - if @type == :unneeded + if unneeded? "This formula doesn't require compiling." else @reason diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index f36b01874..9f79781b4 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -1,4 +1,6 @@ +require "forwardable" require "extend/module" +require "extend/predicable" require "extend/fileutils" require "extend/pathname" require "extend/git_repository" diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index c0e9dbada..1e55b69c5 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -1,7 +1,6 @@ require "download_strategy" require "checksum" require "version" -require "forwardable" # Resource is the fundamental representation of an external resource. The # primary formula download, along with other declared resources, are instances diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index eaae724a4..dd6cea8b2 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -1,4 +1,3 @@ -require "forwardable" require "resource" require "checksum" require "version" |
