2018-09-11 22:56:26 -05:00
|
|
|
module Unitwise
|
|
|
|
module Expression
|
|
|
|
class Decomposer
|
|
|
|
class << self
|
|
|
|
def reset(n = {})
|
|
|
|
old = {
|
|
|
|
parsers: @parsers,
|
|
|
|
transformer: @transformer,
|
|
|
|
cache: @cache
|
|
|
|
}
|
|
|
|
@parsers = n[:parsers]
|
|
|
|
@transformer = n[:transformer]
|
|
|
|
@cache = n[:cache]
|
|
|
|
old
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_custom_units(unit_list, &block)
|
|
|
|
|
2018-09-13 14:51:41 -05:00
|
|
|
if unit_list.empty?
|
|
|
|
return block.call
|
|
|
|
end
|
|
|
|
|
2018-09-11 22:56:26 -05:00
|
|
|
atoms = []
|
2018-09-12 17:17:15 -05:00
|
|
|
ret_val = nil
|
2018-09-11 22:56:26 -05:00
|
|
|
|
2018-09-12 17:17:15 -05:00
|
|
|
begin
|
|
|
|
unit_list.each do |u|
|
|
|
|
atom = Unitwise::Atom.new(u)
|
|
|
|
atom.validate!
|
|
|
|
Unitwise::Atom.all.push(atom)
|
|
|
|
atoms.push(atom)
|
|
|
|
end
|
|
|
|
rem = Unitwise::Expression::Decomposer.send(:reset)
|
2018-09-11 22:56:26 -05:00
|
|
|
|
2018-09-12 17:17:15 -05:00
|
|
|
ret_val = block.call
|
|
|
|
ensure
|
|
|
|
atoms.each do |a|
|
|
|
|
idx = Unitwise::Atom.all.index { |b| b.equal?(a) }
|
|
|
|
Unitwise::Atom.all.delete_at(idx)
|
|
|
|
# Unitwise::Atom.all.pop
|
|
|
|
end
|
|
|
|
Unitwise::Expression::Decomposer.send(:reset, rem)
|
2018-09-11 22:56:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
ret_val
|
|
|
|
end
|
|
|
|
end
|