diff --git a/Library/Homebrew/cask/lib/hbc/dsl.rb b/Library/Homebrew/cask/lib/hbc/dsl.rb index cee83b1347..07d0c6ae1b 100644 --- a/Library/Homebrew/cask/lib/hbc/dsl.rb +++ b/Library/Homebrew/cask/lib/hbc/dsl.rb @@ -1,4 +1,5 @@ require "locale" +require "lazy_object" require "hbc/artifact" @@ -14,7 +15,6 @@ require "hbc/dsl/depends_on" require "hbc/dsl/gpg" require "hbc/dsl/postflight" require "hbc/dsl/preflight" -require "hbc/dsl/stanza_proxy" require "hbc/dsl/uninstall_postflight" require "hbc/dsl/uninstall_preflight" require "hbc/dsl/version" @@ -155,10 +155,12 @@ module Hbc @language_blocks.keys.flatten end - def url(*args, &block) + def url(*args) set_unique_stanza(:url, args.empty? && !block_given?) do - begin - URL.from(*args, &block) + if block_given? + LazyObject.new { URL.new(*yield) } + else + URL.new(*args) end end end diff --git a/Library/Homebrew/cask/lib/hbc/dsl/stanza_proxy.rb b/Library/Homebrew/cask/lib/hbc/dsl/stanza_proxy.rb deleted file mode 100644 index 9e6368362e..0000000000 --- a/Library/Homebrew/cask/lib/hbc/dsl/stanza_proxy.rb +++ /dev/null @@ -1,49 +0,0 @@ -module Hbc - class DSL - class StanzaProxy - attr_reader :type - - def self.once(type) - resolved = nil - new(type) { resolved ||= yield } - end - - def initialize(type, &resolver) - @type = type - @resolver = resolver - end - - def proxy? - true - end - - def to_s - @resolver.call.to_s - end - - # Serialization for dumpcask - def encode_with(coder) - coder["type"] = type - coder["resolved"] = @resolver.call - end - - def method_missing(method, *args) - if method != :to_ary - @resolver.call.send(method, *args) - else - super - end - end - - def respond_to?(method, include_private = false) - return true if [:encode_with, :proxy?, :to_s, :type].include?(method) - return false if method == :to_ary - @resolver.call.respond_to?(method, include_private) - end - - def respond_to_missing?(*) - true - end - end - end -end diff --git a/Library/Homebrew/cask/lib/hbc/url.rb b/Library/Homebrew/cask/lib/hbc/url.rb index 35020c5db3..cf33855e1a 100644 --- a/Library/Homebrew/cask/lib/hbc/url.rb +++ b/Library/Homebrew/cask/lib/hbc/url.rb @@ -1,27 +1,24 @@ -module Hbc - class URL - attr_reader :using, :revision, :trust_cert, :uri, :cookies, :referer, :data, :user_agent +class URL + ATTRIBUTES = [ + :using, + :tag, :branch, :revisions, :revision, + :trust_cert, :cookies, :referer, :user_agent, + :data + ].freeze - extend Forwardable - def_delegators :uri, :path, :scheme, :to_s + attr_reader :uri + attr_reader(*ATTRIBUTES) - def self.from(*args, &block) - if block_given? - Hbc::DSL::StanzaProxy.once(self) { new(*block.call) } - else - new(*args) - end - end + extend Forwardable + def_delegators :uri, :path, :scheme, :to_s - def initialize(uri, options = {}) - @uri = URI(uri) - @user_agent = options.fetch(:user_agent, :default) - @cookies = options[:cookies] - @referer = options[:referer] - @using = options[:using] - @revision = options[:revision] - @trust_cert = options[:trust_cert] - @data = options[:data] + def initialize(uri, options = {}) + @uri = URI(uri) + @user_agent = :default + + ATTRIBUTES.each do |attribute| + next unless options.key?(attribute) + instance_variable_set("@#{attribute}", options[attribute]) end end end diff --git a/Library/Homebrew/lazy_object.rb b/Library/Homebrew/lazy_object.rb new file mode 100644 index 0000000000..add5a339d8 --- /dev/null +++ b/Library/Homebrew/lazy_object.rb @@ -0,0 +1,14 @@ +class LazyObject < Delegator + def initialize(&callable) + super(callable) + end + + def __getobj__ + return @__delegate__ if defined?(@__delegate__) + @__delegate__ = @__callable__.call + end + + def __setobj__(callable) + @__callable__ = callable + end +end diff --git a/Library/Homebrew/test/cask/download_strategy_spec.rb b/Library/Homebrew/test/cask/download_strategy_spec.rb index 0769bb0679..747d034707 100644 --- a/Library/Homebrew/test/cask/download_strategy_spec.rb +++ b/Library/Homebrew/test/cask/download_strategy_spec.rb @@ -3,7 +3,7 @@ describe "download strategies", :cask do let(:url_options) { {} } let(:cask) { instance_double(Hbc::Cask, token: "some-cask", - url: Hbc::URL.new(url, url_options), + url: URL.new(url, **url_options), version: "1.2.3.4") } diff --git a/Library/Homebrew/test/cask/dsl/stanza_proxy_spec.rb b/Library/Homebrew/test/cask/dsl/stanza_proxy_spec.rb deleted file mode 100644 index 8c87d4706f..0000000000 --- a/Library/Homebrew/test/cask/dsl/stanza_proxy_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -describe Hbc::DSL::StanzaProxy, :cask do - subject { stanza_proxy } - - let(:stanza_proxy) { - described_class.new(Array) { [:foo, :bar, :cake] } - } - - it { is_expected.to be_a_proxy } - it { is_expected.to respond_to(:pop) } - its(:pop) { is_expected.to eq(:cake) } - its(:type) { is_expected.to eq(Array) } - its(:to_s) { is_expected.to eq("[:foo, :bar, :cake]") } - - describe "when initialized" do - let(:initializing) { - proc { |b| described_class.new(Array, &b) } - } - - it "does not evaluate the block" do - expect(&initializing).not_to yield_control - end - end - - describe "when receiving a message" do - let(:receiving_a_message) { - proc { |b| described_class.new(Array, &b).to_s } - } - - it "evaluates the block" do - expect(&receiving_a_message).to yield_with_no_args - end - end -end diff --git a/Library/Homebrew/test/lazy_object_spec.rb b/Library/Homebrew/test/lazy_object_spec.rb new file mode 100644 index 0000000000..77e3f23f80 --- /dev/null +++ b/Library/Homebrew/test/lazy_object_spec.rb @@ -0,0 +1,35 @@ +require "lazy_object" + +describe LazyObject do + describe "#initialize" do + it "does not evaluate the block" do + expect { |block| + described_class.new(&block) + }.not_to yield_control + end + end + + describe "when receiving a message" do + it "evaluates the block" do + expect(described_class.new { 42 }.to_s).to eq "42" + end + end + + describe "#!" do + it "delegates to the underlying object" do + expect(!(described_class.new { false })).to be true + end + end + + describe "#!=" do + it "delegates to the underlying object" do + expect(described_class.new { 42 }).not_to eq 13 + end + end + + describe "#==" do + it "delegates to the underlying object" do + expect(described_class.new { 42 }).to eq 42 + end + end +end