Class: Finitio::System
- Inherits:
-
Object
- Object
- Finitio::System
- Defined in:
- lib/finitio/system.rb
Overview
A System is a collection of named Finitio types.
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #add_import(system) ⇒ Object
- #add_type(type, name = nil, metadata = nil) ⇒ Object
- #check_and_warn(logger = nil) ⇒ Object
- #dress(*args, &bl) ⇒ Object
- #dup ⇒ Object
- #factory ⇒ Object
- #fetch(name, with_imports = true, &bl) ⇒ Object
- #get_type(name) ⇒ Object (also: #[])
-
#initialize(types = {}, imports = []) ⇒ System
constructor
A new instance of System.
- #inspect ⇒ Object
- #main ⇒ Object
- #parse(source) ⇒ Object
- #resolve_proxies(recurse = true) ⇒ Object
Constructor Details
#initialize(types = {}, imports = []) ⇒ System
Returns a new instance of System.
7 8 9 10 |
# File 'lib/finitio/system.rb', line 7 def initialize(types = {}, imports = []) @types = types @imports = imports end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
12 13 14 |
# File 'lib/finitio/system.rb', line 12 def types @types end |
Instance Method Details
#add_import(system) ⇒ Object
15 16 17 |
# File 'lib/finitio/system.rb', line 15 def add_import(system) @imports << system end |
#add_type(type, name = nil, metadata = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/finitio/system.rb', line 19 def add_type(type, name = nil, = nil) type = factory.type(type, name, ) if @types.has_key?(type.name) raise Error, "Duplicate type name `#{type.name}`" end @types[type.name] = type end |
#check_and_warn(logger = nil) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/finitio/system.rb', line 110 def check_and_warn(logger = nil) logger ||= begin require 'logger' Logger.new(STDERR) end each_type do |t| next unless t.named? each_import do |i| next unless found = i.get_type(t.name) if found == t logger.info "Duplicate type def `#{t.name}`" break else logger.warn "Type erasure `#{t.name}`" break end end end self end |
#dress(*args, &bl) ⇒ Object
82 83 84 85 |
# File 'lib/finitio/system.rb', line 82 def dress(*args, &bl) raise Error, "No main type." unless main main.dress(*args, &bl) end |
#dup ⇒ Object
106 107 108 |
# File 'lib/finitio/system.rb', line 106 def dup System.new(@types.dup, @imports.dup) end |
#factory ⇒ Object
72 73 74 |
# File 'lib/finitio/system.rb', line 72 def factory @factory ||= TypeFactory.new end |
#fetch(name, with_imports = true, &bl) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/finitio/system.rb', line 50 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 |
#get_type(name) ⇒ Object Also known as: []
39 40 41 42 43 |
# File 'lib/finitio/system.rb', line 39 def get_type(name) fetch(name){|_| fetch(name.to_s){ nil } } end |
#inspect ⇒ Object
102 103 104 |
# File 'lib/finitio/system.rb', line 102 def inspect @types.each_pair.map{|k,v| "#{k} = #{v}" }.join("\n") end |
#main ⇒ Object
46 47 48 |
# File 'lib/finitio/system.rb', line 46 def main self['Main'] end |
#parse(source) ⇒ Object
87 88 89 90 |
# File 'lib/finitio/system.rb', line 87 def parse(source) require_relative "syntax" Syntax.compile(source, self.dup) end |
#resolve_proxies(recurse = true) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/finitio/system.rb', line 92 def resolve_proxies(recurse = true) rebuilt = {} scope = FetchScope.new(self, rebuilt) types.each_with_object(rebuilt) do |(name,type),memo| rebuilt[name] = type.resolve_proxies(scope) end resolved = System.new(rebuilt, imports) recurse ? resolved.resolve_proxies(false) : resolved end |