Class: Copland::Implementation::SymbolSourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/copland/impl/symbol-source-manager.rb

Overview

The implementation of the SymbolSourceManager service. It forms the central repository of all symbols in Copland, and which clients may query the values of any defined symbols.

Constant Summary collapse

NOT_FOUND =

A unique value, used for identifying whether or not a value was found in a symbol source.

Object.new

Instance Method Summary collapse

Constructor Details

#initialize(sources) ⇒ SymbolSourceManager

Create a new SymbolSourceManager, using the given array of source definitions.



99
100
101
102
# File 'lib/copland/impl/symbol-source-manager.rb', line 99

def initialize( sources )
  @sources = sources.map { |entry| SymbolSourceDefinition.new( entry ) }
  @sources = Copland::Orderer.order( @sources ).map { |v| v.source }
end

Instance Method Details

#[](name) ⇒ Object

A convenience method that just delegates to #lookup.



124
125
126
# File 'lib/copland/impl/symbol-source-manager.rb', line 124

def []( name )
  lookup( name )
end

#has_symbol?(name) ⇒ Boolean

Returns true if the source contains a symbol with the given name.

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/copland/impl/symbol-source-manager.rb', line 105

def has_symbol?( name )
  found = @sources.find { |source| source.has_symbol?( name ) }
  return !found.nil?
end

#lookup(name, default = nil) ⇒ Object

Returns the value of the given symbol, or the value of default if no such symbol exists in source.



112
113
114
115
116
117
118
119
120
121
# File 'lib/copland/impl/symbol-source-manager.rb', line 112

def lookup( name, default=nil )
  @sources.each do |source|
    value = source.lookup( name, NOT_FOUND )
    if value != NOT_FOUND
      return value
    end
  end

  return default
end