Merge pull request #4572 from reitermarkus/refactor-hbc/url

Refactor `Hbc::URL`.
This commit is contained in:
Markus Reiter 2018-07-30 22:25:26 +02:00 committed by GitHub
commit 701f86ddeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 108 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View 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

View File

@ -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")
}

View File

@ -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

View 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