lib/finitio/system.rb in finitio-0.6.1 vs lib/finitio/system.rb in finitio-0.7.0.pre.rc1

- old
+ new

@@ -2,14 +2,22 @@ # # A System is a collection of named Finitio types. # class System - def initialize(types = {}) + def initialize(types = {}, imports = []) @types = types + @imports = imports end + attr_reader :types, :imports + private :types, :imports + + def add_import(system) + @imports << system + end + def add_type(type, name = nil, metadata = nil) type = factory.type(type, name, metadata) if @types.has_key?(type.name) raise Error, "Duplicate type name `#{type.name}`" @@ -17,22 +25,42 @@ @types[type.name] = type end def get_type(name) - @types[name] || @types[name.to_s] + fetch(name){|_| + fetch(name.to_s){ nil } + } end alias :[] :get_type def main self['Main'] end - def fetch(name, &bl) - @types.fetch(name, &bl) + def fetch(name, with_imports = true, &bl) + if with_imports + @types.fetch(name) do + _fetch(name, @imports, &bl) + end + else + @types.fetch(name, &bl) + end end + def _fetch(name, imports, &bl) + if imports.empty? + raise KeyError, %Q{key not found: "#{name}"} unless bl + bl.call(name) + else + imports.first.fetch(name, false) do + _fetch(name, imports[1..-1], &bl) + end + end + end + private :_fetch + def factory @factory ||= TypeFactory.new end TypeFactory::DSL_METHODS.each do |dsl_method| @@ -54,10 +82,10 @@ def inspect @types.each_pair.map{|k,v| "#{k} = #{v}" }.join("\n") end def dup - System.new(@types.dup) + System.new(@types.dup, @imports.dup) end end # class System end # module Finitio