Formulary: allow loading formula from contents
This commit is contained in:
parent
cbb91c5516
commit
ff0f6598ce
@ -15,22 +15,26 @@ class Formulary
|
|||||||
FORMULAE.fetch(path)
|
FORMULAE.fetch(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.load_formula(name, path)
|
def self.load_formula(name, path, contents, namespace)
|
||||||
mod = Module.new
|
mod = Module.new
|
||||||
const_set("FormulaNamespace#{Digest::MD5.hexdigest(path.to_s)}", mod)
|
const_set(namespace, mod)
|
||||||
contents = path.open("r") { |f| set_encoding(f).read }
|
|
||||||
mod.module_eval(contents, path)
|
mod.module_eval(contents, path)
|
||||||
class_name = class_s(name)
|
class_name = class_s(name)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
klass = mod.const_get(class_name)
|
mod.const_get(class_name)
|
||||||
rescue NameError => e
|
rescue NameError => e
|
||||||
raise FormulaUnavailableError, name, e.backtrace
|
raise FormulaUnavailableError, name, e.backtrace
|
||||||
else
|
|
||||||
FORMULAE[path] = klass
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.load_formula_from_path(name, path)
|
||||||
|
contents = path.open("r") { |f| set_encoding(f).read }
|
||||||
|
namespace = "FormulaNamespace#{Digest::MD5.hexdigest(path.to_s)}"
|
||||||
|
klass = load_formula(name, path, contents, namespace)
|
||||||
|
FORMULAE[path] = klass
|
||||||
|
end
|
||||||
|
|
||||||
if IO.method_defined?(:set_encoding)
|
if IO.method_defined?(:set_encoding)
|
||||||
def self.set_encoding(io)
|
def self.set_encoding(io)
|
||||||
io.set_encoding(Encoding::UTF_8)
|
io.set_encoding(Encoding::UTF_8)
|
||||||
@ -76,7 +80,7 @@ class Formulary
|
|||||||
def load_file
|
def load_file
|
||||||
STDERR.puts "#{$0} (#{self.class.name}): loading #{path}" if ARGV.debug?
|
STDERR.puts "#{$0} (#{self.class.name}): loading #{path}" if ARGV.debug?
|
||||||
raise FormulaUnavailableError.new(name) unless path.file?
|
raise FormulaUnavailableError.new(name) unless path.file?
|
||||||
Formulary.load_formula(name, path)
|
Formulary.load_formula_from_path(name, path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user