Skip to content
Snippets Groups Projects
Commit 54aa4120 authored by Markus Reiter's avatar Markus Reiter
Browse files

Use `LazyObject` for `Hbc::URL`.

parent 428bc9c7
No related branches found
No related tags found
No related merge requests found
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
......
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
......@@ -5,13 +5,6 @@ module Hbc
extend Forwardable
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 = {})
@uri = URI(uri)
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment