Formulary: add ::map to map formula refs
This commit is contained in:
parent
d3887df2a3
commit
e9631d969c
@ -395,6 +395,8 @@ module Formulary
|
|||||||
)
|
)
|
||||||
raise ArgumentError, "Formulae must have a ref!" unless ref
|
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}"
|
cache_key = "#{ref}-#{spec}-#{alias_path}-#{from}"
|
||||||
if factory_cached? && cache[:formulary_factory] &&
|
if factory_cached? && cache[:formulary_factory] &&
|
||||||
cache[:formulary_factory][cache_key]
|
cache[:formulary_factory][cache_key]
|
||||||
@ -411,6 +413,19 @@ module Formulary
|
|||||||
formula
|
formula
|
||||||
end
|
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.
|
# Return a {Formula} instance for the given rack.
|
||||||
#
|
#
|
||||||
# @param spec when nil, will auto resolve the formula's spec.
|
# @param spec when nil, will auto resolve the formula's spec.
|
||||||
|
|||||||
@ -205,6 +205,23 @@ describe Formulary do
|
|||||||
end
|
end
|
||||||
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
|
specify "::from_contents" do
|
||||||
expect(described_class.from_contents(formula_name, formula_path, formula_content)).to be_kind_of(Formula)
|
expect(described_class.from_contents(formula_name, formula_path, formula_content)).to be_kind_of(Formula)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user