Formulary: add ::map to map formula refs

This commit is contained in:
Rylan Polster 2021-06-14 12:01:01 -04:00
parent d3887df2a3
commit e9631d969c
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
2 changed files with 32 additions and 0 deletions

View File

@ -395,6 +395,8 @@ module Formulary
)
raise ArgumentError, "Formulae must have a ref!" unless ref
ref = @ref_mappings[ref] if @ref_mappings.present? && @ref_mappings.key?(ref)
cache_key = "#{ref}-#{spec}-#{alias_path}-#{from}"
if factory_cached? && cache[:formulary_factory] &&
cache[:formulary_factory][cache_key]
@ -411,6 +413,19 @@ module Formulary
formula
end
# Register a reference mapping. This mapping will be used by {Formulary::factory}
# to allow certain references to be substituted for another string before
# being retrived. For example, to map `foo` to the `bar` formula:
# <pre>Formulary.map "foo", to: "bar"
# Formulary.factory "bar" # returns the bar formula
# </pre>
# @param ref the string to map.
# @param :to the target reference to which `ref` should be mapped.
def self.map(ref, to:)
@ref_mappings ||= {}
@ref_mappings[ref] = to
end
# Return a {Formula} instance for the given rack.
#
# @param spec when nil, will auto resolve the formula's spec.

View File

@ -205,6 +205,23 @@ describe Formulary do
end
end
describe "::map" do
before do
formula_path.dirname.mkpath
formula_path.write formula_content
end
it "maps a reference to a new Formula" do
expect {
described_class.factory("foo")
}.to raise_error(FormulaUnavailableError)
described_class.map "foo", to: formula_name
expect(described_class.factory("foo")).to be_kind_of(Formula)
end
end
specify "::from_contents" do
expect(described_class.from_contents(formula_name, formula_path, formula_content)).to be_kind_of(Formula)
end