Merge pull request #4572 from reitermarkus/refactor-hbc/url
Refactor `Hbc::URL`.
This commit is contained in:
commit
701f86ddeb
@ -1,4 +1,5 @@
|
|||||||
require "locale"
|
require "locale"
|
||||||
|
require "lazy_object"
|
||||||
|
|
||||||
require "hbc/artifact"
|
require "hbc/artifact"
|
||||||
|
|
||||||
@ -14,7 +15,6 @@ require "hbc/dsl/depends_on"
|
|||||||
require "hbc/dsl/gpg"
|
require "hbc/dsl/gpg"
|
||||||
require "hbc/dsl/postflight"
|
require "hbc/dsl/postflight"
|
||||||
require "hbc/dsl/preflight"
|
require "hbc/dsl/preflight"
|
||||||
require "hbc/dsl/stanza_proxy"
|
|
||||||
require "hbc/dsl/uninstall_postflight"
|
require "hbc/dsl/uninstall_postflight"
|
||||||
require "hbc/dsl/uninstall_preflight"
|
require "hbc/dsl/uninstall_preflight"
|
||||||
require "hbc/dsl/version"
|
require "hbc/dsl/version"
|
||||||
@ -155,10 +155,12 @@ module Hbc
|
|||||||
@language_blocks.keys.flatten
|
@language_blocks.keys.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def url(*args, &block)
|
def url(*args)
|
||||||
set_unique_stanza(:url, args.empty? && !block_given?) do
|
set_unique_stanza(:url, args.empty? && !block_given?) do
|
||||||
begin
|
if block_given?
|
||||||
URL.from(*args, &block)
|
LazyObject.new { URL.new(*yield) }
|
||||||
|
else
|
||||||
|
URL.new(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -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
|
|
||||||
@ -1,27 +1,24 @@
|
|||||||
module Hbc
|
class URL
|
||||||
class URL
|
ATTRIBUTES = [
|
||||||
attr_reader :using, :revision, :trust_cert, :uri, :cookies, :referer, :data, :user_agent
|
:using,
|
||||||
|
:tag, :branch, :revisions, :revision,
|
||||||
|
:trust_cert, :cookies, :referer, :user_agent,
|
||||||
|
:data
|
||||||
|
].freeze
|
||||||
|
|
||||||
|
attr_reader :uri
|
||||||
|
attr_reader(*ATTRIBUTES)
|
||||||
|
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
def_delegators :uri, :path, :scheme, :to_s
|
def_delegators :uri, :path, :scheme, :to_s
|
||||||
|
|
||||||
def self.from(*args, &block)
|
|
||||||
if block_given?
|
|
||||||
Hbc::DSL::StanzaProxy.once(self) { new(*block.call) }
|
|
||||||
else
|
|
||||||
new(*args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(uri, options = {})
|
def initialize(uri, options = {})
|
||||||
@uri = URI(uri)
|
@uri = URI(uri)
|
||||||
@user_agent = options.fetch(:user_agent, :default)
|
@user_agent = :default
|
||||||
@cookies = options[:cookies]
|
|
||||||
@referer = options[:referer]
|
ATTRIBUTES.each do |attribute|
|
||||||
@using = options[:using]
|
next unless options.key?(attribute)
|
||||||
@revision = options[:revision]
|
instance_variable_set("@#{attribute}", options[attribute])
|
||||||
@trust_cert = options[:trust_cert]
|
|
||||||
@data = options[:data]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
14
Library/Homebrew/lazy_object.rb
Normal file
14
Library/Homebrew/lazy_object.rb
Normal file
@ -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
|
||||||
@ -3,7 +3,7 @@ describe "download strategies", :cask do
|
|||||||
let(:url_options) { {} }
|
let(:url_options) { {} }
|
||||||
let(:cask) {
|
let(:cask) {
|
||||||
instance_double(Hbc::Cask, token: "some-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")
|
version: "1.2.3.4")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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
|
|
||||||
35
Library/Homebrew/test/lazy_object_spec.rb
Normal file
35
Library/Homebrew/test/lazy_object_spec.rb
Normal file
@ -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
|
||||||
Loading…
x
Reference in New Issue
Block a user